In computer networking, a hostname (archaically nodename) is a label that is assigned to a device connected to a computer network and that is used to identify the device in various forms of electronic communication such as the World Wide Web, e-mail or Usenet. – Wikipedia
Sometimes you could have the need to change the hostname of your linux box. If you are running a Debian Distro, take a look at the following two ways to change the hostname:
Solution 1 – hostname command
The hostname command shows or sets the system’s host name. The hostname command will return the current hostname
root@lamp ~# hostname
lamp
Passing a string to the hostname will change the current hostname
root@lamp ~# hostname pippo
root@lamp ~# hostname
pippo
root@lamp ~#
Has you can see the bash shows root@lamp instaead of root@pippo. This happens because the bash does not update itself. If you open a new terminal you should see
root@pippo ~#
as the prompt.
This solution if fast enough, but when you restart your linux box the hostname change is lost.
To change permanently the hostname run solution 2
Solution 2 – change /etc/hosts file
Open the /etc/hosts files and change all the occurrencies of “lamp” with your new hostname (“pippo” in my case)
127.0.0.1 site.com localhost
#127.0.1.1 lamp
127.0.1.1 pippo
#Required for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters
ff02::3 ip6-allhosts
then open /etc/hostname file and change “lamp” in “pippo”
Last, run
# /etc/init.d/hostname.sh start
Now when you reboot your linux box, the new name will be applied.
Gg1