A hostname is a name that is given to a computer and used to identify the computer in a network. In this tutorial, we learn about hostname command in Linux with practical examples.
- A Linux machine.
- Access to the terminal.
- Basic knowledge of using Linux command line.
The hostname command is commonly used to display Linux computer name or its DNS domain name. You can use it to set a hostname for the computer. This only set the hostname temporarily until the next reboot.
Historically /etc/hostname file was supposed to only contain the hostname and not the full canonical FQDN. Nowadays most software is able to cope with a full FQDN here. This file is read at boot time by the system initialization scripts to set the hostname. /etc/hosts Usually, this is where one sets the domain name by aliasing the host name to the FQDN.
The basic syntax for the Linux hostname command:
hostname [options] [File] [new_hostname]
Hostname command options
Some of the useful options for Linux hostname command.
|Displays alias names of the host system.|
|enumerates all configured network addresses on all configured network interfaces, and translates them to DNS domain names. (fqdn fully qualified domain)|
|set default hostname if none is available|
|Display DNS domain name.|
|Displays long hostname (FQDN). A FQDN consists of a short host name and the DNS domain name|
|read hostname or NIS domain name from the given file|
|Displays all configured addresses for the hostname|
|establishes all configured network interfaces and displays all addresses for the host|
|Displays short hostname.|
|Print version information on standard output and exit successfully|
|Displays the Network Information System NIS/YP domain name|
Hostname Command Examples
The following examples will show how you can use the hostname command with the options.
1. Display Linux Hostname
To display hostname for your Linux machine, the following program displays it:
2. Display IP address
Situations for needing your Linux machine IP address might arise, to display your IP, use the following command:
And if you need to see all your IP addresses for the machine, use the following command:
Alternatively use ip addr command which gives more information for all addresses.
3. Display DNS domain name
For displaying your current host DNS domain name, the following hostname command gets the job done:
In this case, the machine didn’t return any value because a local domain is not set
And if it's the NIS domain name you would like to see, use the following command:
The NIS also returned an error value because a local domain name is not set.
4. Display short hostname
To display your Linux machine short hostname, use the following command:
5. Display all Fully Qualified Domain Names (FQDNs)
To display all your Linux machine FQDNs, the following command will achieve that:
Nevertheless, if you want to only display the machine FQDN, use the following command:
6. Change Linux hostname
Maybe you already have a hostname set for your linux machine, but you wish to change it to a new hostname, the following command can be used:
sudo hostname new-hostname
Only the super-user can change a hostname, which explains the “sudo” command appended to the hostname command. Remember changing hostname using hostname will only keep it temporary.
7. Set a default hostname
To set a default system hostname (default hostname localhost) for your linux machine if you don't have any, the following hostname command will get the job done:
The machine returned a hostname because a hostname has been set.
If you need your machine to read a hostname or a NIS domain name from a specified file, use the following command:
8. Display Linux alias name
If an alias name is set for your Linux machine, the following hostname command will display it:
Command returns a blank line, which means no alias name is set for this system.
9. Hostname help
If you need help understanding more about the hostname command, you can use the help command to figure it out. To use the help command, type the following:
In this tutorial, we learned about Linux hostname command and its options.
Thanks for reading please leave your feedback and suggestions in the comment section.