Skip to content
Default image

Linux hostname command

August 26, 2022

Let’s imagine the following scenario: you are connected through ssh on multiple systems and at one point you get “lost” in the terminals and you are not sure on what system you work.

When you access a system, in the terminal you are usually prompted with user@hostname:~$ but there are some situations where the terminal only shows $. This is another situation where this command helps to figure the system name you are on.

In this case, the hostname command will come in handy.

hostname is used to display the system name assigned to a Linux machine. It can also be used to set the system hostname or Network Information System name.

A hostname is a name given to a computer that uniquely identifies it over a network.

The syntax for this command is:

hostname [OPTIONS]

If you want to change the hostname, the syntax is:

hostname [OPTIONS] [new_host_name]

To see all the options that can be used with this command you can check the manual page or run the man hostname command or hostname –help in your terminal.

If we use the command without any option it will print the hostname of the system (as an alternative you can use the uname -n command).

Now let’s have a look at a couple of options that we can use with the hostname command.

hostname -i

-i (–ip-address) will show the IP address of the system

hostname -I

Using -I(–all-ip-addresses) – capital i – will display all the IP addresses of the computer.

hostname -a

The -a(–alias) flag is used to display alias names assigned to the machine. If no alias is set, it will print a blank line

hostname -f

The -f(–fqdn) option will show the Fully Qualified Domain Name (FQDN) of the machine.

hostname -d

-d (–domain) will display the name of the DNS domain. The same result can be achived by using the dnsdomainname command.

Change hostname

You can change the hostname of your Linux system by running the following command as root or using sudo. Please keep in mind that this command will not print any output upon execution.

sudo hostname new_host_name

Another way to change the hostname is to use the -F(–file) option. Like in the above example, this command bust be run by the superuser (root).

The new hostname set by using the above command will be in place until you reboot the system.

In order to make a permanent change to the hostname you need to make changes to the /etc/hosts and /etc/hostname files or use the hostnamectl command – sudo hostnamectl set-hostname [new name] .

This is it for this post. Thank you for reading.

Remember that you can always check the manual to see how to use a command and don’t be afraid to google it.