How to Use the hostname Command in Linux
The Linux hostname command is used to view or change a system’s domain and hostname. It can also check a computer’s IP address.
In this tutorial, we will cover all the ways you can use the hostname command on Linux and how to change your computer’s hostname on a Linux system.
- A system running Linux
- Access to the command line / terminal window
hostname Command Syntax
The hostname command uses the following format:
Use the [options] parameter to add more specific instructions to the hostname command. Without it, the default output shows your computer’s hostname:
Use the [new_hostname] parameter when you want to change your computer’s hostname.
hostname Command Examples
The hostname command has a number of options you can use for more specific outputs:
- -a , —alias : Displays the alias name of the host.
- -A , —all-fqdns : Displays every FQDN (Fully Qualified Domain Name) of the computer.
- -b , —boot : Always set a hostname.
- -d , —domain : Display DNS domain name.
- -f , —fqdn , —long : Display the FQDN.
- -F , —file : Check a file to recover and display the hostname.
- -h , —help : Print the help message as the output.
- -i , —ip-address : Display the computer’s IP address.
- -I , —all-ip-addresses : Display all of the computer’s network addresses.
- -s , —short : Display the short version of the hostname.
- -v , —verbose : Expand all output to verbose.
- -y , —yp , —nis : Display the NIS domain name.
Display Hostname
Using the hostname command without any additional options displays the computer’s hostname:
Display Short Hostname
Use the -s or —short option to display the short version of the hostname, which is cut off at the first dot:
Display Alias
Use the -a or —alias option to display the alias (substitute hostname) of the host if one is set. There are very few cases where this option is useful, and its use is no longer recommended.
Search for Hostnames
Use the -F or —file option to search a specific file (such as hostname or hosts) and change your computer’s hostname to match the content of the file:
Note: When using the -F or —file options, also specify the path to the file you want to check.
Force Default Hostname
If you don’t want to have a specific file containing the hostname, or want to leave that file empty, use the -b or —boot option. This makes your computer use the default hostname (localhost) until you set a different one:
Display Domain Name
Use the -d or —domain option to display the name of your DNS domain:
Display FQDN
If you want to display a system’s FQDN, use the -f , —fqdn , or —long option:
A FQDN contains the short hostname and the DNS domain name.
Display All FQDNs
To display each FQDN of your machine, use the -A or —all-fqdns option:
This option lists out all of your network addresses and their DNS domain names. It skips any addresses that it can’t translate.
Note: Different network addresses may translate to same DNS domain names. This can result in duplicate output entries when using -A or —all-fqdns options.
Display NIS Domain
If you’re using NIS (Network Information Service), you can check your NIS domain name with the -y , —yp , or —nis option:
Change NIS Domain Name
Use this command with the following format to change your NIS domain name:
Display Related Network Addresses
The hostname command also lets your display network addresses tied to a hostname. To do this, use the -i or —ip-address option:
This command only works if the hostname can be resolved.
Display All Network Addresses
Use the -I or —all-ip-addresses option to display all of the host’s network addresses. Unlike -i , this option doesn’t depend on hostname resolution:
Change Hostname Until Reboot
You can change your computer’s hostname with the hostname command. To do this, use the following format:
Any changes to the hostname you make using this command will only last until the next reboot.
Change Hostname Permanently
To change the hostname permanently, use a text editor like Nano to make changes to the hostname and hosts files:
You can also use the hostnamectl command to permanently change the hostname:
Additional hostname Options
Use the -V or —version option to print out the version of the hostname software package on your Linux system:
If you need help with any of the commands, use the -h or —help option to print out a help message:
After following this guide, you now know how to use the hostname command in Linux and all the options that can extend its use.
How to Display and Set Hostname in Linux?
Hostname is a unique name for a computer and computer network node in a network. It is also known as a Nodename, computer name or Sitename where it is a label that is assigned to a device that is connected to the computer network. Hostname has simple names consisting of words and phrases or they may be well-structured.
Example:
- In the URL www.geeksforgeeks.org, the hostname is www.
- Mail servers are often named after their function or protocol used and receive the corresponding hostname like mail, pop3, IMAP, etc.
Rules of Hostname
- Host name will be a single word or phrase with no space.
- A Hostname consists of only a combination of letters, numbers, periods, or hyphen.
- Maximum length of the hostname will be 253 characters.
- DNS(Domain Name Space) may be appended to the hostname.
- The hostname cannot have an underscore.
How to display Hostname in Linux?
Step 1) Firstly open your terminal on Linux.
Step 2) Command ”Hostname” is used in order to display hostname in Linux operating system as shown below:
Also, without typing the command hostname you can identify your Linux hostname.
In the above image see jagroop@jagroop-SVE1513CYNB so after @ you can identify your hostname which is:
How to display Domain Name?
Command “Domainname” is used to return the domain name of the Linux System. If your Linux device domain name is set then you will get “none” message as output. Else you will get your Domain name. In my Linux system, I don’t have any domain name so it will result in none as shown:
How to display hostname with more detailed information?
In order to display hostname with more detailed information we will use either below command:
Or, In order to display hostname with more detailed information various types of commands are used, and these commands we will get by using command “hostname -h” which means that we want help regarding hostname. In the below output, all the commands applicable for hostname is shown as:
For example: I want to know the IP address of my hostname so, will choose the option -i which is implemented as:
How to set a hostname?
Step 1) Change the hostname using “hostnamectl”command using the syntax:
sudo hostnamectl set-hostname geeksforgeeks
When we run the above syntax, the system will ask password and execute it as shown:
Step 2) After step 1, restart your Linux system
Step 3) After restart, open the terminal.
Step 4) Now type command “hostname”, you will get your changed hostname output as:
How To Find Hostname In Linux
A Hostname is an unique alphanumeric label assigned to a Linux system in order to identify it on the network. It can also contain a few special characters such as hyphen (-), period (.), and underscore (_). A typical hostname consists of up to 253 characters. Generally, the hostname is stored in /etc/hostname file in most Linux distributions. In this brief guide, we will learn about various commands to find hostname in Linux operating systems.
1. Find Hostname in Linux with hostname command
The hostname command is used to display a Linux system’s DNS name and Hostname. It also allows the users to change or set the Hostname.
To find Hostname in Linux, run hostname command without any options:
Find Hostname in Linux with hostname command
As you see in the output, my Linux system’s hostanme is ostechnix.
If you want to show the FQDN (Fully Qualified Domain Name), run:
2. Display Hostname with hostnamectl command in Linux
The hostnamectl command is used to get and set the system hostname and related settings in Linux.
To display the hostname of your Linux machine, run hostnamectl command without any options:
Sample output:
Display Hostname with hostnamectl command in Linux
Unlike the hostname command, hostnamectl displays a few more details of your system such as type of the system (laptop or desktop), Boot & machine ID, Operating system and Kernel version, Architecture etc.
To display only the Hostname, use —static flag:
3. Check Linux Hostname with nmcli command
The nmcli is a command line tool to control NetworkManager and report the network status. With nmcli, we can create, display, edit, delete, activate, and deactivate network connections, as well as control and display network device status.
To check the hostame of a Linux system with nmcli tool, run this command:
4. View Hostname using uname command
The uname is a command line tool to print all system information such as hostname, Kernel release, Kernel version, operating system, and processor architecture etc.
To view hostname with uname command, run it with -n option:
5. Get Hostname by reading /etc/hostname file
As stated already, the hostname is stored in /etc/hostname file in most Linux distributions. To get the hostname of a Linux system, simply display the contents of /etc/hostname file:
These are the most commonly used commands to find hostname in Linux. There are also a two uncommon ways to check Linux hostname. They have been given below for your reference.
6. Print Hostname in Linux with sysctl command
The sysctl command is used to list and modify kernel parameters at runtime.
We can print a Linux system’s hostname using sysctl command line below:
7. Determine Hostname in Linux with ProcFS
Proc file system (or shortly procfs) is a virtual file system maintained by the Linux kernel. It is also sometimes referred to as a process information pseudo-file system. It doesn’t contain ‘real’ files but runtime system information such as system memory, devices mounted, hardware configuration etc. All these information are available under a special directory named /proc in Linux.
To determine the hostname of Linux with prcfs, run:
You know now different methods to view a Linux system’s hostname. How will you change it? The following guide explains how to set a new hostname or change the existing hostname, either from command line or from Settings section in graphical mode.
- How To Set Or Change Hostname On Linux
Hope this helps.
Related read:
- How To Find IP Address In Linux
Senthilkumar Palani (aka SK) is the Founder and Editor in chief of OSTechNix. He is a Linux/Unix enthusiast and FOSS supporter. He lives in Tamilnadu, India.
Find Hostname from IP Linux
Similarly, computers support labels or names that help give them a unique identity in a network. That is where a hostname comes in. A hostname is a set of alphanumerical characters unique to a computer network, allowing devices to communicate with each other.
Typically, the underlying technology that identifies a computer or a device in a network is a set of numbers known as IP addresses. These are then mapped to a specific name resolved to the IP address of the machine.
In this tutorial, however, I will show you various ways that you can use to get the hostname associated with a specific IP address. The methods discussed in this tutorial will work in almost all Linux and Unix-Like systems.
If you are looking for detailed instructions on how systems such as Domain Names work, check out the resource on the link provided below:
With that out of the way, let us dive in.
Method 1: Ping
The simplest method to get the hostname from an IP address is to use ping. Ping is a simple yet powerful command-line utility that uses ECHO packets to communicate with a host.
NOTE: The following command only works in Windows machines. For Linux, check the next method.
To get a hostname from an IP address with ping, use the command below:
Here is the output from the above command:
The above command is not always reliable; it often only works if the hostname is available in the hosts file.
Learn more here:
Method 2: Host Command
The second and common method for getting the hostname from the IP address in Linux is the host command. This simple tool is part of the dnsutil package.