How to Use the hostname Command in Linux {With Examples} (2024)

Introduction

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.

How to Use the hostname Command in Linux {With Examples} (1)

Prerequisites

  • A system running Linux
  • Access to the command line / terminal window

hostname Command Syntax

The hostname command uses the following format:

hostname [options] [new_hostname]

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:

hostname
How to Use the hostname Command in Linux {With Examples} (2)

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:

hostname -shostname --short

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.

hostname -ahostname --alias
How to Use the hostname Command in Linux {With Examples} (4)

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:

sudo hostname -F [file path]sudo hostname --file [file path]
How to Use the hostname Command in Linux {With Examples} (5)

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:

hostname -bhostname --boot
How to Use the hostname Command in Linux {With Examples} (6)

Display Domain Name

Use the -d or --domain option to display the name of your DNS domain:

hostname -dhostname –domain

Display FQDN

If you want to display a system’s FQDN, use the -f, --fqdn, or --long option:

hostname -fhostname --fqdnhostname --long

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:

hostname -Ahostname --all-fqdns

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:

hostname -yhostname --yphostname --nis
How to Use the hostname Command in Linux {With Examples} (7)

Change NIS Domain Name

Use this command with the following format to change your NIS domain name:

sudo hostname -y [NIS hostname]sudo hostname --yp [NIS hostname]sudo hostname --nis [NIS hostname]
How to Use the hostname Command in Linux {With Examples} (8)

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:

hostname -ihostname --ip-address

This command only works if the hostname can be resolved.

How to Use the hostname Command in Linux {With Examples} (9)

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:

hostname -Ihostname --all-ip-addresses
How to Use the hostname Command in Linux {With Examples} (10)

Change Hostname Until Reboot

You can change your computer’s hostname with the hostname command. To do this, use the following format:

sudo hostname [new hostname]

Any changes to the hostname you make using this command will only last until the next reboot.

How to Use the hostname Command in Linux {With Examples} (11)

Change Hostname Permanently

To change the hostname permanently, use a text editor like Nano to make changes to the hostname and hosts files:

sudo nano /etc/hostnamesudo nano /etc/hosts

You can also use the hostnamectl command to permanently change the hostname:

sudo hostnamectl set-hostname [new hostname]
How to Use the hostname Command in Linux {With Examples} (12)

Note: For more details on how to change the hostname on a Linux system, see our guides How To Change Hostname On CentOS 7, How To Change Hostname On CentOS 8, How To Change Hostname On Ubuntu 18.04, How To Change Hostname On Ubuntu 20.04 and How to Change Hostname in Debian 10.

Additional hostname Options

Use the -V or --version option to print out the version of the hostname software package on your Linux system:

hostname -Vhostname --version
How to Use the hostname Command in Linux {With Examples} (13)

If you need help with any of the commands, use the -h or --help option to print out a help message:

hostname -hhostname --help
How to Use the hostname Command in Linux {With Examples} (14)

Conclusion

After following this guide, you now know how to use the hostname command in Linux and all the options that can extend its use.

For more Linux commands, check out our Linux Commands Cheat Sheet.

How to Use the hostname Command in Linux {With Examples} (2024)

FAQs

How do I use hostname in Linux? ›

-F, --file filename: It is used to read the hostname from a file. -i, --ip-address: It is used to print the network addresses of the hostname. This option will work only if the hostname is in resolving circ*mstances. -I, --all-ip-addresses: It is used to print the network addresses of the host.

How do I run a hostname command? ›

Click Start, Run. In the Run box, type "CMD" and press enter. In the Command prompt that opens, type "hostname" and press enter. The computer name displays on the next line.

What is an example of a hostname? ›

Hostnames are composed of a sequence of labels concatenated with dots. For example, "en.wikipedia.org" is a hostname. Each label must be from 1 to 63 octets long. The entire hostname, including the delimiting dots, has a maximum of 253 ASCII characters.

How do I use hostname? ›

In the window the window that appears on the bottom-left hand corner of your screen, type in cmd and click OK. The command prompt window will appear. In this window, type hostname and press Enter. The name of your computer will be displayed.

How to get IP address using hostname in Linux? ›

You can determine the IP address or addresses of your Linux system by using the hostname , ifconfig , or ip commands. To display the IP addresses using the hostname command, use the -I option. In this example the IP address is 192.168. 122.236.

What is the format of hostname in Linux? ›

Each element of the hostname must be from 1 to 63 characters long and the entire hostname, including the dots, can be at most 253 characters long. Valid characters for hostnames are ASCII(7) letters from a to z, the digits from 0 to 9, and the hyphen (-). A hostname may not start with a hyphen.

How to use hostname command in shell script? ›

In this case, firstly, we take a hostname as a command-line argument and assign it to the hostname variable. Secondly, we use dig to resolve it to an IP address. Thirdly, we print either the successful result or a failure message based on the success of the resolution.

How to get IP from hostname command? ›

In an open command line, type ping followed by the hostname (for example, ping dotcom-monitor.com). and press Enter. The command line will show the IP address of the requested web resource in the response.

How do I find the fully qualified hostname in Linux? ›

Meanwhile, Linux users can find FQDN by entering the hostname –fqdn command line in the Terminal. To open the Terminal, press CTRL + ALT + T or search for it by clicking the Dash icon, typing “terminal” in the search box, and selecting the Terminal application.

What is hostname and how it works? ›

A hostname is a unique label assigned to a device connected to a computer network. It serves as a human-readable identifier for that device, allowing you to easily distinguish it from other devices on the network. You can think of it as the name of your computer or other network-enabled devices.

What are the three types of hostname? ›

There are three classes of hostname : static, pretty, and transient. The “static” host name is the traditional hostname , which can be chosen by the user, and is stored in the /etc/hostname file. The “transient” hostname is a dynamic host name maintained by the kernel.

How do I check my hostname? ›

How to Find the Computer/Host Name, MAC/Physical Address & IP Address on a Windows Computer
  1. Open the Command Prompt. ...
  2. Type ipconfig/all after the prompt and press Enter (see red underline). ...
  3. Locate Host Name (see the top red box in image). ...
  4. Locate Physical Address, (see middle red box in image above).

What do I put for hostname? ›

The hostname for a computer on a home network may be something like new laptop, Guest-Desktop, or FamilyPC. Hostnames are also used by DNS servers, so you can access a website by a common, easy-to-remember name. This way, you don't have to recall a string of numbers (an IP address) to open a website.

How to check IP in Linux? ›

The following commands will get you the private IP address of your interfaces:
  1. ifconfig -a.
  2. ip addr (ip a)
  3. hostname -I | awk '{print $1}'
  4. ip route get 1.2.3.4 | awk '{print $7}'
  5. (Fedora) Wifi-Settings→ click the setting icon next to the Wifi name that you are connected to → Ipv4 and Ipv6 both can be seen.

How do I add a hostname to a file in Linux? ›

Use vim to open the /etc/hosts file.
  1. sudo vim `/etc/hosts` Update the /etc/hosts file to include your persistent hostname for localhost, similar to the following:
  2. 127.0.0.1 localhost persistent-hostname. ...
  3. sudo hostnamectl set-hostname persistent-hostname. ...
  4. hostname.

How to set hostname domain in Linux? ›

Change a server's hostname
  1. Check existing hostname. ...
  2. Modify the value to match your FQDN hostname, ...
  3. Open the file at /etc/hosts. ...
  4. Change the domain name (where required) ...
  5. Change networking configuration /etc/sysconfig/network. ...
  6. Change networking configuration /etc/sysconfig/network.

Where is Linux hostname set? ›

The hostname value in /etc/hosts is often set to localhost by default. With this context in mind, here are all the different ways to manipulate a hostname on Linux. [ Free download: Advanced Linux commands cheat sheet. ]

What does hostname do? ›

The hostname is used to distinguish devices within a local network. In addition, computers can be found by others through the hostname, which enables data exchange within a network, for example. Hostnames are used on the internet as part of the fully qualified domain name.

Top Articles
Latest Posts
Article information

Author: Otha Schamberger

Last Updated:

Views: 5739

Rating: 4.4 / 5 (55 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Otha Schamberger

Birthday: 1999-08-15

Address: Suite 490 606 Hammes Ferry, Carterhaven, IL 62290

Phone: +8557035444877

Job: Forward IT Agent

Hobby: Fishing, Flying, Jewelry making, Digital arts, Sand art, Parkour, tabletop games

Introduction: My name is Otha Schamberger, I am a vast, good, healthy, cheerful, energetic, gorgeous, magnificent person who loves writing and wants to share my knowledge and understanding with you.