LFCS – Network Hostname

Jarret B

Well-Known Member
Staff member
Joined
May 22, 2017
Messages
340
Reaction score
367
Credits
11,754
We have gone over hostnames in previous articles, but we are going to cover it again. We’ll also be covering hostname resolution for CentOS7 and Ubuntu 18.04. Trust me, this one was difficult to get resolved (no pun intended).

A hostname is a simple name used to represent a device on a network. It is easier to remember the hostname than an IP Address.

On a local network, names can be something like ‘Server’, ‘system1’, ‘laptop2’, etc. On a local network, the Domain Name of the Fully Qualified Domain Name (FQDN) is ‘.local’. The FQDN could be ‘Server1.local’, ‘system1.local’, etc.

On the Internet, you have a FQDN such as ‘www.linux.org’. The hostname is ‘www’ and the domain name is ‘linux.org’.

Hostname

As stated, the hostname is a name of a device to allow you to easily remember the device name. Network Printers can have a hostname as well. It makes it easy to PING a device to determine if it is online. A device offline cannot return a response to a PING command.

The best way to see the current hostname is to issue the command ‘hostname’ in a terminal. The simple response is your hostname.

To change your hostname requires a little more effort.

NOTE: Two systems can have the same hostname, but without using the IP Address, you will only be able to access one by name.

Changing the hostname requires the use of a program called ‘hostnamectl’. Running ‘hostnamectl’ with no parameters is like running ‘hostname’, except you get a lot more information (as shown in Figure 1).

Figure 1.JPG

FIGURE 1

The hostname is ‘Server3’ and is a virtualized CentOS 7 machine.

We can change the hostname on a CentOS or Ubuntu system using the command:

hostnamectl set-hostname <new-hostname>

The value is changed on the system permanently. You can use ‘hostname’ to set the name of the system, but it is only valid until the system reboots or the terminal is closed in which we changed the name. It listed the temporary hostname as ‘Transient’ when using ‘hostnamectl’ to view the name. It displayed the permanent hostname as ‘Static hostname’.

NOTE: You cannot use the ‘Transient hostname’ for replacing the IP Address in a command. Only the ‘Static hostname’ is used for that purpose.

There is also a ‘Pretty Name’. This name has no restrictions on length, whereas the others have a maximum length of 64 characters. The ‘Pretty Name’ can use any valid UTF-8 character while it limited the Static and Transient names to the alphabet, numbers, period (.), underscore (_) and hyphens (-).

To set the ‘Pretty Name’, use the command:

hostnamectl set-hostname “<pretty name>” --pretty

Be sure to place the ‘Pretty Name’ in double-quotes. Spaces are allowed and sometimes you can think of the ‘Pretty Name’ more as a description. An example is ‘DLNA-Server in the Living Room’.

You may wonder the reason for hostnames. The issue is that to remember the IP Address of each system on a network can be difficult. Issues also arise when using Dynamic Host Control Protocol (DHCP) and system IP Addresses can change.

mDNS on CentOS7

Installing Multicast DNS (mDNS) on CentOS7 is not a complicated matter. There are a few steps, but it is easy to do.

You need to check the status of the ‘avahi’ service with the command:

systemctl status avahi-daemon.service

If you get a response that service doesn’t exist, then you need to install it:

sudo apt install -y avahi

You can check the status again and make sure it is running as well as ‘enabled’. When you enable a service, it is set to auto-start when the system boots up. If the service does not say it is enabled, then run the command:

sudo systemctl enable avahi-daemon.service

The next step is to check for the repository ‘epel’ in the repolist. Perform the command:

sudo yum repolist

It should show the list of repositories. If you see an entry ‘epel / x86_64’ then the repository is already included. If the repository is not listed, then do the line:

sudo yum install epel-release

The new repository should now be included in the list and you can install the last package you need to make mDNS work for CentOS7.

sudo yum install nss-mdns -y

The last thing we need to do is to include the entry for mDNS in the Firewall. The following command will fix it:

sudo firewall-cmd --zone=public --permanent --add-service=mdns

Everything should be set, but it is a good practice here to sometimes reboot the system for everything to take effect and start properly.

mDNS on Ubuntu 18.04

Similar to CentOS7, we need to verify the ‘avahi’ service with the command:

systemctl status avahi-daemon.service

Make sure it is started and enabled. The commands to perform these functions are:

sudo systemctl start avahi-daemon.service
sudo systemctl enable avahi-daemon.service


For Ubuntu 18.04, we need to edit a file and change a single line. The file is ‘/etc/systemd/resolved.conf’. Find the line that contains ‘#MulticastDNS=no’ and change is to ‘MulticastDNS=yes’.

To restart the service for the changes to take effect, use the command:

sudo systemctl restart systemd-networkd

As before, everything should work, but a reboot can help make sure all services are started and all files reloaded.

Testing mDNS

Get the hostnames of systems on your network and go to a system and ping the other systems by their hostname.

ping <hostname>.local

Use the ‘Static Hostname’ and not the Transient or Pretty hostnames. Also, be sure to add the ‘.local’ to the hostname when you access it by name and not IP Address.

I know I use the ‘ping’ command as an example, but other programs, such as ssh, can use hostnames. Any program where you would specify an IP Address, simply use the hostname.

It can be possible you have some systems on Wi-Fi and others hard-wired, and that the router may block the mDNS resolution requests. Some routers allow the requests to cross the different mediums. If name resolution should fail, you may check that they are on two different mediums. Of course, they must be on the same subnet.

Conclusion

Using mDNS can be very handy in a network. Hostnames are much simpler to remember than the IP Addresses.

By using mDNS, you can make sure that the Linux systems can all access each other by hostname. If you are adding Windows and/or Apple systems on the network, then you need to enable Zero Configuration or Bonjour to allow these systems to use mDNS.
 

Members online


Latest posts

Top