19

I want to create a shell script that can change the hostname of my Ubuntu permanently. Whenever I use the hostname New_hostname command, it returns to the original hostname after I restart the machine.

I found out that the only way I can change this permanently is by modifying the file in /etc/hostname and save it. Is there some way I can do this using a shell script only? I also have a password.

fuesika
  • 3,280
  • 6
  • 26
  • 34
user1548960
  • 357
  • 3
  • 8
  • 16
  • `usermod --login new_name old_name`. But don't, it would mess up your homedir and quite some other stuff. – Noctua Jan 18 '14 at 10:55

8 Answers8

20

The hostnamectl combines setting the hostname via the hostname command and editing /etc/hostname. Unfortunately, editing /etc/hosts still has to be done separately.

hostnamectl set-hostname <new-hostname>
Fizer Khan
  • 88,237
  • 28
  • 143
  • 153
  • what is the use of setting hostname in production When in production, I have seen some people do hostnamectl set-hostname later to put it in `nano etc/hosts` 99% tutorials omit this tho, what is the use of this? Found this link on the matter but still don't quite understand it. https://learn.akamai.com/en-us/webhelp/adaptive-media-delivery/adaptive-media-delivery-implementation-guide/GUID-0EA5FB59-3184-4BE3-A522-E85572DDA8CA.html – Mr-Programs Jan 27 '19 at 21:43
15

Type

echo "myNewHostName" > /etc/hostname

in any shell with root access near you..

You may also want to take a look at the file /etc/hosts, cf. http://pricklytech.wordpress.com/2013/04/24/ubuntu-change-hostname-permanently-using-the-command-line/.

fuesika
  • 3,280
  • 6
  • 26
  • 34
11

In Ubuntu 18.04 LTS

Hostname changing via SSH is reverted after reboot in Ubuntu 18.04. Make permanent change as following way.

1. Edit /etc/cloud/cloud.cfg

sudo nano /etc/cloud/cloud.cfg

Set preserve_hostname to true

preserve_hostname: true

2. Run hostnamectl

hostnamectl set-hostname new-host-name

3. Reboot

sudo reboot
KBIIX
  • 873
  • 9
  • 14
2

Change hostname permanently without reboot

/etc/hosts

127.0.0.1    persistent_host_name

/etc/hostname

persistent_host_name

Apply changes Immediately

$ sudo hostname persistent_host_name

Check changes

$ hostname
persistent_host_name
JBaczuk
  • 13,886
  • 10
  • 58
  • 86
2

Change Hostname on Ubuntu 18.04

Definition

A hostname is a label that identifies a machine on the network. You shouldn’t use the same hostname on two different machines on a same network.

Prerequisites

  • User should have a sudo privileges

Change the Hostname

Change the hostname using hostnamectl command. If you want to change the hostname to new_hostname

sudo hostnamectl set-hostname new_hostname

It will not change the hostname directly. You want to preserve the changes permanently then you have to edit cloud.cfg file

sudo nano /etc/cloud/cloud.cfg

# This will cause the set+update hostname module to not operate (if true)
preserve_hostname: true

Save the file and close your editor.

Verify your Changes

You can verify your changes using command hostnamectl it will show new_hostname under Static hostname

PS: Source Link

Anand Tripathi
  • 14,556
  • 1
  • 47
  • 52
1

Typically, you would need to change it in these files:

/etc/hostname
/etc/hosts

If you are using some advanced printers, also here:

/etc/printcap

This is why I would recommend doing it manually - but search the old hostnames first. To find all occurrences in /etc:

sudo grep -iRI "_OLDHOSTNAME_" /etc 2>/dev/null

Then change the _OLDHOSTNAME_ in every occurrence. Done.

crysman
  • 167
  • 1
  • 11
1

To chaneg the Hostname permanet in ubuntu machine

Go to :

 #vim /etc/hostname

Type the hostname inside the file you want to set for the machine

Then save and the file

After saving the document run this command

 # hostname -F /etc/hostname

Then edit the /etc/hosts file

 #vim /etc/hosts

type the ip hostname inside the file

Then Logout of of the machine and relogin into the machine

Javeed Shakeel
  • 2,926
  • 2
  • 31
  • 40
1

If you just want to change host name, because its getting displayed as a command prompt in the terminal. Then you can replace \h in PS1 with "desired_host_name" in ~/.bashrc

Like in ~/.bashrc put this line at end of file:

export PS2="continue-> ";
export PS1="\u@3050:~$ ";
Andre Leon Rangel
  • 1,659
  • 1
  • 16
  • 28