set hostname in linux

This one is going to be pretty simple, its changing the hostname of your linux machine whether its your home PC or server. We will go over 2 different ways to do it, the old school way that still works and the new way using hostnamectl. We will also go over adding a domain to your host.

Old School Way

1. The old school way to change the hostname is to edit the file /etc/hostname. Simply add your hostname of choice which will be the only line on that file.

sudo vim /etc/hostname

2. Replace whatever is currently in the text file with the new hostname and save.


3. Now lets edit the /etc/hosts file and add our hostname using the following format.

sudo vim /etc/hosts debian debian

4. Reboot linux to apply changes.

5. Run the  command to verify the new hostname.

debian@debian:~$ hostname

New School Way

1. Run the following command to set your new hostname.

sudo hostnamectl set-hostname debian

2. Logout and log back in and run the following command to verify the hostname.

debian@debian:~$ hostname


1. Edit the /etc/hosts file and append the domain name to the second field.

sudo vim /etc/hosts debian.linuxman.local debian

2. Reboot linux and check the FQDN using the following command.

debian@debian:~$ hostname -f

Check only the domain:

debian@desktop:~$ hostname -d

Done! Simple!

If you are interested in learning more about Linux, i highly recommend this book on Linux Administration