Changing Your Host Name on Linux

Here’s how to change your host name on Linux.

But First …

⚠️ Editing configuration files is exactly the main reason for learning Vim. Do not proceed until you are reasonably proficient using it or make sure you have a mentor close by to help you.

⚠️ Other UNIX and Linux distributions may need you to change things in other places. These instructions are confirmed to work on Linux Mint specifically.

Step by Step

The main place the hostname is maintained is in /etc/hostname and /etc/hosts.

⚠️ In the following steps I will be using the sk name, but remember to use your own.

Open /etc/hostname with vim (vi) and change the word to what you want for your computer name. Only use lowercase alphabet and numbers and don’t start with a number (even if these are technically allowed). Remember people have to remember and type this in. This is the very reason we are changing it. Avoid dashes - and underscores _ since they are harder for people to type and read.

sudo vi /etc/hostname

Make sure there is no trailing spaces after the name.

Save it (esc ZZ).

You can cat it out to check it.

cat /etc/hostname

If it did not change it is probably because you forgot to edit the file as root (you didn’t use sudo).

Now we need to change the entries in the /etc/hosts file.

sudo vi /etc/hosts

Look for every place that the old name exists and change it to your new name like you did in /etc/hostname. It will probably just be the one

When done it will look something like this.

127.0.0.1   localhost
127.0.1.1   sk

# The following lines are desirable 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

Good job! Your hostname will now be the new name after you restart.

The last thing is to update the hostname right now while your computer is running. This prevents you from having to restart your device.

hostname sk

Just type the hostname command to confirm that it was changed.

hostname
sk

Now you can reset your current Bash shell so it uses the new host name.

exec bash

You should now see your new name in your terminal prompt.

You may still need to restart any mDNS service that you might have running to get the new name.

[Restarting a Systemd Service]