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.
See Your Hostname
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.
Setting Your Hostname
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.
On a computer running systemd, you can run the following command to change your computer's hostname:
sudo hostnamectl set-hostname newname
Systemd will take care of resetting everything for you.
Old School Method
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."
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:
127.0.0.1 localhost 127.0.1.1 hostname
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.
sudo vim /etc/hosts
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.
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.
127.0.0.1 localhost 127.0.1.1 hostname 192.168.1.110 localserver
When you're done, restart networking on your computer. It should be as simple as restarting the network service.
sudo systemctl restart networking
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.
Our latest tutorials delivered straight to your inbox