The Wayback Machine - https://web.archive.org/web/20240925162453/https://www.geeksforgeeks.org/hostname-command-in-linux-with-examples/
Open In App

hostname command in Linux with examples

Last Updated : 19 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.  

hostname

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
hostname -a

hostname -a

2) `-A` Option in `hostname` command in Linux

 Syntax:

hostname -A
hostname -A

hostname -A

3) `-b` Option in `hostname` command in Linux

 Syntax:

hostname -b
hostname -b

hostname -b

4) `-d` Option in `hostname` command in Linux

 Display the domain name of the host if any.

Syntax:

hostname -d
hostname -d

hostname -d

5) `-f` Option in `hostname` command in Linux 

Display the fully qualified domain name (FQDN) of the host.

 Syntax:

hostname -f
hostname -f

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

 

sudo hostname -F filename

sudo hostname -F filename

7) `-i` Option in `hostname` command in Linux

 Display the IP address of the host

Syntax:

hostname -i
hostname -i

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
hostname -I

hostname -I

9) `-s` Option in `hostname` command in Linux 

Display the short hostname of the host.

 Syntax:

hostname -s
hostname -s

hostname -s

10) `-V` Option in `hostname` command in Linux

Display the version information about the hostname 

 Syntax:

hostname -V
hostname -V

hostname -V

11) How to set hostname in Linux 

Set the hostname of the system temporarily.

Syntax:

sudo hostname NEW_HOSTNAME
sudo hostname geeksforgeeks

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.

here we can change hostname permanently

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.



Previous Article
Next Article

Similar Reads

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.geeks
3 min read
Ipdump - Generate a Report for hostname Ipaddress URL or domain in Kali Linux
Ipdump is free and open-source available on GitHub. This tool can retrieve IP or Domain Geolocation, Whois information, closed and open ports details. Features of Ipdump:Ipdump can be used to retrieve Geolocation information.Ipdump can be used to fetch the SSL certification of a domain.Ipdump can be used to get WHOIS information of IP addresses and
2 min read
How to Set Hostname Permanently in Linux
Setting a hostname is a fundamental step in configuring a Linux system. The hostname acts as an identifier for your machine on a network, making it easier to manage and communicate with other devices. In this article, we'll explore the importance of having a well-defined hostname and the process of setting it permanently in a Linux environment. A h
6 min read
How to Change Hostname on RHEL 9 (Red Hat Enterprise Linux 9)
Changing the hostname on RHEL 9 (Red Hat Enterprise Linux 9) is a fundamental task that can be accomplished with a few simple steps. In this detailed guide, we will walk you through each aspect of the process, ensuring that even beginners can understand and execute the procedure successfully. Table of Content Step 1: Check the Current HostnameStep
4 min read
How to Change Kali Linux hostname?
Changing the hostname in Kali Linux involves modifying system configuration files to update the label assigned to your device on a network. The hostname serves as a unique identifier for the device, simplifying communication by using a readable name instead of an IP address. Adjusting the hostname can be useful for customization, security, or organ
5 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 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 this comprehensive guide, we will explore the vario
4 min read
How to Fix "Ssh: Connect To Host 'Hostname' Port 22: Connection Timed Out"
SSH is a secure network protocol that allows encrypted communication, remote access and control of network computers. It has a strong encryption method for securing server access and unsecured networks. But many users face connection issues and one of the common problem is ssh: "connect to host hostname port 22: connection timed out". This article
6 min read
select command in Linux with examples
select command in Linux is used to create a numbered menu from which a user can select an option. If the user enters a valid option then it executes the set of command written in select block and then ask again to enter a number, if a wrong option is entered it does nothing. If user enters nothing and simply press 'enter' the option menu gets print
1 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
rcp Command in Linux with examples
There comes a time while using LINUX when there is a need to copy some information stored in a file to another computer. This can be done simply using rcp command line utility . Obviously there exists some other methods to complete the above mentioned task which are more secure (like scp or rsync) but this command lets you do this in the simple way
4 min read
fmt command in Linux with examples
fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specified, writing to standard output. Here's the syntax
4 min read
unexpand command in Linux with Examples
To convert the leading spaces and tabs into tabs, there exists a command line utility called unexpand command. The unexpand command by default convert each spaces into tabs writing the produced output to the standard output. Here's the syntax of unexpand command : Syntax : $unexpand [OPTION]... [FILE]... where, OPTION refers to the options compatib
2 min read
uniq command in Linux with examples
The linux uniq command is basically used to remove all repeated lines in a file. This command is used when a line is repeated multiple times and replace these multiple lines with one line. This command is designed to work on sorted files. Purpose : Remove repetitious lines from the sorted 'input-file' and send unique lines to the 'output-file'. If
2 min read
anacron command in linux with examples
anacron command is used to execute commands periodically with a frequency specified in days. Its main advantage over cron is that it can be used on a machine which is not running continuously. In cron if a machine is not running on time of a scheduled job then it will skip it, but anacron is a bit different as it first checks for timestamp of the j
3 min read
for command in Linux with Examples
for command in Linux is used to repeatedly execute a set of command for every element present in the list. Syntax: for NAME [in WORDS ... ] ; do COMMANDS; done Example: Options: help for : It displays help information.
1 min read
autoheader command in Linux with Examples
autoheader command in Linux is used to create a template file of C "#define" or any other template header for configure to use. If the user will give autoheader an argument, it reads the standard input instead of reading configure.ac and also writes the header file to the standard output. This command scans the configure.ac file and figures out whi
1 min read
expect command in Linux with Examples
expect command or scripting language works with scripts that expect user inputs. It automates the task by providing inputs. // We can install expect command using following if not installed // On Ubuntu $sudo apt install expect // On Redhat based systems $ yum install expect First we write a script that will be needing inputs from users and then we
2 min read
function command in Linux with examples
The function is a command in Linux that is used to create functions or methods. It is used to perform a specific task or a set of instructions. It allows users to create shortcuts for lengthy tasks making the command-line experience more efficient and convenient. The function can be created in the user's current shell session or saved in the shell
2 min read
fg command in Linux with examples
fg command in linux used to put a background job in foreground. Syntax: fg [job_spec] job_spec may be: %n : Refer to job number n. %str : Refer to a job which was started by a command beginning with str. %?str : Refer to a job which was started by a command containing str. %% or %+ : Refer to the current job. fg and bg will operate on this job if n
1 min read
bg command in Linux with Examples
bg command in linux is used to place foreground jobs in background. Syntax: bg [job_spec ...] job_spec may be: %n : Refer to job number n. %str : Refer to a job which was started by a command beginning with str. %?str : Refer to a job which was started by a command containing str. %% or %+ : Refer to the current job. fg and bg will operate on this
1 min read
Clear Command in Linux with Examples
clear is a standard Unix computer operating system command that is used to clear the terminal screen. This command first looks for a terminal type in the environment and after that, it figures out the terminfo database for how to clear the screen. And this command will ignore any command-line parameters that may be present. Also, the clear command
2 min read
Continue Command in Linux with examples
continue is a command which is used to skip the current iteration in for, while, and until loop. It is used in scripting languages and shell scripts to control the flow of executions. It takes one more parameter [N], if N is mentioned then it continues from the nth enclosing loop. The syntax for the `continue` command in Linuxcontinue or continue [
2 min read
hostid command in Linux with examples
hostid is a command in Linux that is used to display the Host's ID in hexadecimal format. It provides a quick and straightforward way to retrieve the host ID, allowing administrators to associate it with software licenses or perform system-specific operations. Syntax of `hostid` command in Linuxhostid [OPTION] Options provided by hostedhostid --hel
1 min read
Dirname Command in Linux with Examples
dirname is a command in Linux that is used to remove the trailing forward slashes "/" from the NAME and prints the remaining portion. If the argument NAME does not contain the forward slash "/" then it simply prints dot ".". In other words, we can say that the `dirname` command is a useful tool for extracting the directory portion from a given path
2 min read
sdiff command in Linux with Examples
sdiff command in linux is used to compare two files and then writes the results to standard output in a side-by-side format. It displays each line of the two files with a series of spaces between them if the lines are identical. It displays greater than sign if the line only exists in the file specified by the File2 parameter, and a | (vertical bar
2 min read
factor command in Linux with examples
The factor command in Linux is used to print the prime factors of the given numbers, either given from command line or read from standard input. The numbers given through standard input may be delimited by tabs, spaces or newlines. Syntax: factor [NUMBER] Factor 100 (non-prime number): We get the prime factors 2 and 5 that make up 100. Factor 13 (p
1 min read
chpasswd command in Linux with examples
chpasswd command is used to change password although passwd command can also do same. But it changes the password of one user at a time so for multiple users chpasswd is used. Below figure shows the use of passwd command. Using passwd we are changing the password of the guest user. Here first you have to enter the password of the currently signed u
2 min read
dir command in Linux with examples
dir command in Linux is used to list the contents of a directory. How is dir command different from ls? dir command differs from ls command in the format of listing contents that is in default listing options. By default, dir command lists the files and folders in columns, sorted vertically and special characters are represented by backslash escape
3 min read