The following are the steps you need to execute to change the hostname of your device in Linux.
Find out your hostname
To find what is your current hostname, run the
hostnamecommand without any parameters.
$ hostname localhost.localdomain
Another way of finding the hostname of your machine is as follows -
$ sysctl kernel.hostname localhost.localdomain
Here is yet another way of determining the hostname of your device.
$ cat /etc/sysconfig/network NETWORKING=yes NETWORKING_IPV6=no HOSTNAME=localhost.localdomain
Changing the hostname
Edit /etc/hosts file
The following is the content of my
$ cat /etc/hosts 127.0.0.1 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6
Lets assume my machine IP is
22.214.171.124, and I need to assign it hostname
To do this, first I have to edit
/etc/hostsand append a single line as follows.
$ cat /etc/hosts 127.0.0.1 localhost.localdomain localhost ::1 localhost6.localdomain6 localhost6 126.96.36.199 my212.box21.com my212
Next I have to make my changes permanent.
To change the hostname of your machine, you should log in as super user (root).
Make the changes permanent
Edit the file
/etc/sysconfig/networkand change the value of
HOSTNAMEvariable as follows.
NETWORKING=yes NETWORKING_IPV6=no HOSTNAME=my212.box21.com
Now in the next reboot, my machine will reflect the new hostname.