What is Hostname in Linux and How Can You Change It?

When you visit a website, you type that website’s URL into your browser. That URL is made up of regular words that you can easily understand, like “maketecheasier.com.” Behind the scenes, a DNS server associates that name with a server’s IP address. This system exists because remembering IP addresses is awful, and no one would do it. Hostnames work the same way, only on a local network.

When you give your computer a hostname, it’s a regular word that you understand and can easily type in to identify that computer on your network. Just like a URL, you can substitute that hostname where you would regularly use an IP address. That includes browsing local web pages and using SSH. Unless you’re using a local DNS server, it takes a bit of setting up to make it run smoothly.

View Hostname Linux

Viewing your hostname is very easy. Open a terminal and run the hostname command. It’ll display your computer’s current hostname in the terminal window.

You probably set your computer’s hostname when you installed the operating system, but you can change it any time you choose. Historically, there have been several different ways to set a Linux computer’s hostname, but systemd has greatly simplified the process.

Systemd

On a computer running systemd, you can run the following command to change your computer’s hostname:

Systemd will take care of resetting everything for you.

Old School Method

Hostname File Linux

The most universal traditional method involves modifying two files and restarting your computer. The first file is the “hostname” file. Surprising, right? It’s a plain test file with a single word in it. The system reads that word and sets it as the hostname, so put whatever you like there to make it the system’s hostname. It’s located at “/etc/hostname.”

Linux Hosts File

Next, there’s the “hosts” file. That file tells your system which hostnames to associate with IP addresses. You need to tell it to associate your hostname with the local computer. This one is located at “/etc/hosts;” open it once locate it.

There are two lines there that you need to concern yourself with, and it should look something like this:

Change only the second one that has your current hostname. When you’re done, save, exit, and reboot your computer.

The “hosts” file acts sort of like an ultra-simplified DNS server. It’s the file that lets your computer match up a hostname with an IP address on your network. You can find the file at “/etc/hosts.” Open it once you locate it.

The file is divided into two sections. The top handles the IPv4 addresses that you’re used to, and the bottom is IPv6. If you followed the traditional method, you set your own hostname at the top.

Linux Add Host To File

To add the hostnames of other devices on your network, create new lines below the two for your computer, and follow the same format. First, include the local IP of the device. Then, specify its hostname.

When you’re done, restart networking on your computer. It should be as simple as restarting the network service.

Now you’re able to access your new entry via its hostname. Try getting in with SSH.

If you were able to connect, everything is set up and working properly. You now have a solid handle on what hostnames are in Linux and how to make use of them to simplify access to your networked devices.

Leave a Reply

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.