hostname command in Linux with examples
Last Updated :
19 Jul, 2024
hostname command in Linux is used to obtain the DNS (Domain Name System) name and set the system’s hostname or NIS (Network Information System) domain name. A hostname is a name given to a computer and attached to the network. Its main purpose is to uniquely identify over a network.
Syntax of the `hostname` command in Linux
hostname -[option] [file]
Example: We obtain the system hostname by just typing the hostname without any attributes.
.webp)
hostname
Options |
Description |
Syntax |
-a |
This option is used to get the alias name of the host system (if any). It will return an empty line if no alias name is set. This option enumerates all configured addresses on all network interfaces.
|
hostname -a
|
-A |
This option is used to get all FQDNs (Fully Qualified Domain Name) of the host system. It enumerates all configured addresses on all network interfaces. An output may display the same entries repetitively.
|
hostname -A
|
-b |
Used to always set a hostname. Default name is used if none specified.
|
hostname -b
|
-d |
This option is used to get the Domain if local domains are set. It will not return anything (not even a blank line) if no local domain is set.
|
hostname -d
|
-f |
This option is used to get the Fully Qualified Domain Name (FQDN). It contains short hostname and DNS domain name.
|
hostname -f
|
-F |
This option is used to set the hostname specified in a file. Can be performed by the superuser(root) only.
|
sudo hostname -F filename
|
-i |
This option is used to get the IP (network) addresses. This option works only if the hostname is resolvable.
|
hostname -i
|
-I |
This option is used to get all IP(network) addresses. The option doesn’t depend on resolvability of hostname.
|
hostname -I
|
-s |
This option is used to get the hostname in short. The short hostname is the section of hostname before the first period/dot(.). If the hostname has no period, the full hostname is displayed.
|
hostname -s
|
-V |
Gives version number as output.
|
hostname -V
|
Outputs of Options available in `hostname` command
1) `-a` Option in `hostname` command in Linux
Display all aliases of the host.
Syntax:
hostname -a
.webp)
hostname -a
2) `-A` Option in `hostname` command in Linux
Syntax:
hostname -A
.webp)
hostname -A
3) `-b` Option in `hostname` command in Linux
Syntax:
hostname -b
.webp)
hostname -b
4) `-d` Option in `hostname` command in Linux
Display the domain name of the host if any.
Syntax:
hostname -d
.webp)
hostname -d
5) `-f` Option in `hostname` command in Linux
Display the fully qualified domain name (FQDN) of the host.
Syntax:
hostname -f
.webp)
hostname -f
6) `-F` Option in `hostname` command in Linux
This option is used to set the hostname specified in a file. Can be performed by the superuser(root) only.
Syntax:
sudo hostname -F filename
.webp)
sudo hostname -F filename
7) `-i` Option in `hostname` command in Linux
Display the IP address of the host
Syntax:
hostname -i
.webp)
hostname -i
8) `-I` Option in `hostname` command in Linux
This option is used to get all IP (network) addresses. The option doesn’t depend on resolvability of hostname.
hostname -I
.webp)
hostname -I
9) `-s` Option in `hostname` command in Linux
Display the short hostname of the host.
Syntax:
hostname -s
.webp)
hostname -s
10) `-V` Option in `hostname` command in Linux
Display the version information about the hostname
Syntax:
hostname -V

hostname -V
11) How to set hostname in Linux
Set the hostname of the system temporarily.
Syntax:
sudo hostname NEW_HOSTNAME
.webp)
sudo hostname geeksforgeeks
Replace NEW_HOSTNAME with the new hostname you want to set. (Here we have changed it to “geeksforgeeks”)
Set the hostname of the system permanently.
To set the hostame of our system permanently we have to go inside “/etc/hostname” using text editor and change hostname to the hostname we want.
.webp)
here we can change hostname permanently.
Conclusion
This command `hostname` in Linux seems so simple but at the same time it is a very powerful command that allows users to view or set the hostname of the system. It is important for users to know the `hostname` command to manage their Linux system effectively. One can easily understand by this article, as we have discussed many options and also their output, options like `-a`, `-A`, `-b`, `-d`, `-f`, `-F`, `-i`, `-I`, `-s` and `-V`.
hostname command in Linux with examples – FAQs
What is the hostname command used for in Linux?
The hostname
command in Linux is used for showing or setting the system’s hostname. The hostname is a label that is assigned to a device (a server, a computer, or any other device) connected to a network, which is used to identify the device in various forms of electronic communication such as the World Wide Web, email, or in user interfaces.
How to display the current hostname?
To display the current hostname of your system, you can use the hostname
command without any additional options:
hostname
This command will output the current hostname to your terminal window.
How to set a new hostname?
To temporarily change the hostname of your Linux system, you can use the hostname
command followed by the new hostname. For example, to change the hostname to new-hostname
, you would use:
sudo hostname new-hostname
This change will not persist across reboots. It only affects the current session.
How to make the hostname change permanent?
To permanently change the hostname on most modern Linux distributions (like Ubuntu, Debian, CentOS), you need to edit the hostname file and possibly other configuration files:
1. Edit the /etc/hostname
file: This file contains only the hostname of the system.Replace the current hostname with your new hostname in this file.
sudo nano /etc/hostname
2. Edit the /etc/hosts
file: This file maps hostnames to IP addresses. Add a new entry for your hostname or modify the existing one.Find the line starting with 127.0.0.1
or 127.0.1.1
and replace the old hostname with the new one.
sudo nano /etc/hosts
3. Use hostnamectl (systemd systems): If your system uses systemd
, you can use hostnamectl
to change the hostname permanently:
sudo hostnamectl set-hostname new-hostname
After these changes, reboot your system to apply them.
Can hostname be used to display the FQDN?
Yes, the hostname
command can display the Fully Qualified Domain Name (FQDN) of the host. The FQDN is the complete domain name for a specific computer, or host, on the Internet. To show the FQDN, you can use the -f
option:
hostname -f
This command will display the full domain name, which includes the hostname and all domain levels. For the hostname -f
command to return the FQDN, the hostname must be correctly set in the /etc/hosts
or in the DNS system, associating the hostname with its full domain counterpart.
Similar Reads
gs command in Linux with Examples
gs command invokes Ghostscript, which is an interpreter of Adobe Systems PostScript and Portable Document Format(PDF) languages. After executing Ghostscript it reads further input from the standard input stream until it encounters 'quit' command. Syntax: gs [ options ] [ files ] ... Options: Below a
2 min read
gunzip command in Linux with examples
gunzip command is used to compress or expand a file or a list of files in Linux. It accepts all the files having extension as .gz , .z , _z , -gz , -z , .Z , .taz or .tgz and replace the compressed file with the original file by default. The files after uncompression retain its actual extension. Syn
4 min read
halt command in Linux with examples
This command in Linux is used to instruct the hardware to stop all the CPU functions. Basically, it reboots or stops the system. If the system is in runlevel 0 or 6 or using the command with --force option, it results in rebooting of the system otherwise it results in shutdown. Syntax: halt [OPTION]
2 min read
hash command in Linux with examples
The `hash` is a command in Linux that helps in managing the hash table. It facilitates with mapping the names of executable files to their locations on disk. It helps in speeds uping the commands execution by remembering paths of frequently used commands. It uses the `hash` to view hashed commands (
5 min read
hdparm command in Linux with Examples
"hdparm" (i.e., hard disk parameter) is one of the command line programs for Linux that is used to handle disk devices and hard disks. With the help of this command, you can get statistics about the hard disk, alter writing intervals, acoustic management, and DMA settings. It can also set parameters
4 min read
Head command in Linux with examples
It is the complementary of Tail command. The head command, as the name implies, print the top N number of data of the given input. By default, it prints the first 10 lines of the specified files. If more than one file name is provided then data from each file is preceded by its file name. Syntax:
4 min read
help Command in Linux with examples
If youâre new to the Linux operating system and struggling with command-line utilities, the help command is one of the first tools you should learn. As its name suggests, the 'help' command provides detailed information about built-in shell commands, making it an essential resource for beginners and
5 min read
hexdump command in Linux with examples
The 'hexdump' command in Linux is a versatile utility used to display file content or data from standard input in a human-readable format. It is invaluable for programmers and system administrators for debugging binary data, analyzing file structures, and verifying data integrity. Here we will get a
6 min read
How to Display Command History in Linux | history Command
The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In t
4 min read
host command in Linux with examples
host command in Linux system is used for DNS (Domain Name System) lookup operations. In simple words, this command is used to find the IP address of a particular domain name or if you want to find out the domain name of a particular IP address the host command becomes handy. You can also find more s
2 min read