How To Change A Hostname In Linux

Table of Contents

This one is going to be pretty simple, its how to change a hostname in linux, 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

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
				
			

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

				
					debian
				
			

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

				
					sudo vim /etc/hosts

172.16.32.5 debian debian
				
			

You can also add the FQDN of your host.

				
					172.16.32.5 debian.linuxman.co debian
				
			

Reboot linux to apply changes.

Run the hostname command to verify the new hostname.

				
					debian@debian:~$ hostname
debian
				
			

For FQDN:

				
					debian@debian:~$ hostname -f
debian.linuxman.co
				
			

New School Way

Run the following command to set your new hostname.

				
					sudo hostnamectl set-hostname debian
				
			

Likewise, you can setup the FQDN as well.

				
					sudo hostnamectl set-hostname debian.linuxman.co
				
			

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

				
					debian@debian:~$ hostname
debian
				
			

For FQDN:

				
					debian@debian:~$ hostname -f
debian.linuxman.co
				
			

Done! Simple!

Meet the Author

Leave a Reply