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.

debian

3. 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

4. Reboot linux to apply changes.

5. 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

1. 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

2. 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!


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *