hostname

botond published 2024/02/10, Sat - 08:40 time

Content

 

Introductory

A hostname command is a basic tool in Linux systems that allows you to query or set the computer name. In Linux systems, every machine has a unique name, which we call "hostname". This name allows devices to be identified and accessed within the network. The hostname command can be used to view or change the settings of the current hostname. This command is particularly useful when diagnosing network problems and configuring servers and other network devices.

The command can also be used to perform query and setting operations. In this short description, we will review them.

 

 

Query operations

With the query options of the hostname command, we can get useful network information, let's see what they are.

Use without parameters

If the hostname command is called without parameters, it returns the valid names:

hostname

Use without parameters

In this case, it will return the default hostname, so this may vary depending on your settings.

Alias ​​query

An "alias" name is an alternative name used on network devices and servers that allows devices or services to be accessed under multiple names. Aliases are often used in network settings where a machine has to serve several different services and we want to access each service with a different name.

The operation of alias names is simple: A Domain Name System (DNS) or the local / Etc / hosts multiple names can be assigned to a machine by configuring the file. It means the machine to your IP address several different Domain name or hostname, allowing users to use different names to access different services on the same machine.

The usefulness of alias names can be seen in several ways:

  • Separation of services: You can run several different services on a single physical or virtual machine and assign a different alias name to each of them. This helps to logically separate services and make them easier to manage.
  • Easier access and memorability: With the help of aliases, we can make services easier to access and remember. For example, we can assign a www alias to a web server and a mail alias to a mail server.
  • Flexible network settings: By using aliases, we can manage network settings more flexibly, for example, we can easily redirect traffic from one service to another without users having to change their configuration.

Using a -a, --alias is done with switches:

hostname -a

hostname command - Alias ​​name query

Here, for the sake of the example, I directly set two names so that the operation of the command is clearly visible. So the /etc/hosts file looks like this for this example:

Hosts file content

 

 

Domain name query

To query the domain name or DNS domain name, we use the -d, --domain one of the switches:

hostname -d

Domain name query

hostname is a Linux command manual page draws attention to not using the domainname command to query the domain name. The domainname command is actually used to display NIS (Network Information Service, formerly Yellow Pages) domain names, which is a client-server directory service protocol for distributing system configuration data such as user and host names among computers on a computer network. A NIS domain name is therefore not the same as a DNS domain name and is usually used for other purposes.

Query FQDN

Az FQDN name consists of the short hostname and the DNS domain name. To query it, we use the -f, --fqdn, --long one of the switches:

hostname -f
hostname --fqdn
hostname --long

Query FQDN

IP address query

To query the IP address, we use the -i, --ip-address one of the switches:

hostname -i
hostname --ip-address

IP address query

Short hostname query

The short hostname is the part of the full hostname cut up to the first dot. to query it, we use the -s, --short one of the switches:

hostname -s
hostname --short

Short hostname query

In general, the short hostname option gives the same output as if we had called the hostname command without the switch. However, there are special cases where the system is configured to use the fully qualified host name (FQDN) as the default. In this case, when used without a switch, it returns the full name, while a -s switch only returns the short name in this case as well. So, if we need the short hostname in a script, for example, we use the -s switch to make sure we get the short name.

 

 

NIS domain name query

NIS (Network Information Service) to query a domain name, we use a -y, --yp, --nice one of the switches:

hostname -y
hostname --yp
hostname --nis

NIS domain name query

Since I don't have such a system set up here, it gives this output.

 

Setting operations

The hostname command also has setting functions that can be used to temporarily change the computer's hostname. These settings only affect the current session, so if the computer is restarted, these settings will be lost. We review them in this chapter.

To make the settings below root authorization is required.

Hostname setting with direct parameter

We don't give any switches to the hosname command, just a name, then it sets that hostname:

sudo hostname debian11-proba

Hostname setting with direct parameter

Here, I query the hostname before and after the setting to see how it works.

So here it is important to remember that this setting will only remain in effect until the computer is restarted. When the machine restarts, the system a / etc / hostname reads from file and resets the hostname.

Set hostname from file

The hostname command also allows you to set the hostname from a file. This method helps keep system configuration flexible and easy to manage, especially in larger IT environments or situations where machines change frequently or use dynamic IP addresses and hostnames. In an automated environment, this feature allows easy setting of machine unique identifiers, which can be important for network communication, resource management, and security policies.

To configure from a file, we use the -F, --fillet one of the switches. Do not confuse the previously mentioned small here -f with a switch that performs a different function.

In the example below, I will use the /etc/hostname file, if this file is already present, and on the other hand, I will also restore the computer's original hostname:

sudo hostname -F /etc/hostname

Set hostname from file

So the machine's original hostname was restored. After the setting, I also checked the new name and the contents of the file.

 

 

Conclusion

The hostname command is an extremely easy-to-use, but even more useful tool for querying or setting the names of Linux computers. With its help, we can perform related network work manually or even automatically. However, it is important to keep in mind that in order to permanently set hostnames, other tools such as / etc / hostname and the / Etc / hosts simultaneous modification of files. In addition, it is recommended to consider security considerations when changing the hostname, especially in a network environment where incorrect configuration may lead to security risks. Modern network infrastructures often use more advanced name management solutions, such as DNS or LDAP services, which enable more flexible and secure network integration. Knowing them and using them properly is essential for efficient and secure network communication.