Everything You Need to Know About the 'Domainname' Command

A computer's host name serves several purposes

www in metal block letters

Bart Sadowski / E+ / Getty Images

Several related commands expose or modify the hostname of your computer. They are standard commands that should work regardless of your Linux distribution.

The 'hostname' Command

Every computer has a hostname and your computer's hostname is likely to have been set up when you first installed Linux. Discover your computer's hostname by running the following command in a terminal window:


On some machines, your hostname may show up as something like computername.computerdomain.

The hostname is basically used to identify your computer on a network and the domain that it belongs to.

You can get just the computer name returned by running the following command:

hostname -s

Alternatively, you can get just the domain name by running this command:

hostname -d

Some Linux shells, including BASH, include the hostname as part of the shell prompt.

The 'domainname' Command

Instead of using the hostname with the -d switch to return the domain name you can simply run the following command:


If you have a domain set up it will be returned otherwise you will see the text (none). In many cases, especially when you're not otherwise networked, you may see localdomain instead.

The domainname command returns the NIS domain name of the system.

NIS stands for Network Information System. NIS is a Remote Procedure Call-based client/server system that allows a group of machines within an NIS domain to share a common set of configuration files. This arrangement permits a system administrator to set up NIS client systems with only minimal configuration data and to add, remove, or modify configuration data from a single location.

The dnsdomainname Command

The dnsdomainname command returns the DNS domain name. You can run it by typing the following into the terminal:


DNS stands for Domain Name Server and it is used by the internet to convert IP addresses to real domain names. Without domain names, we would all be using large spreadsheets to work out that will take us to linux.lifewire.com.

Unless you are running a web server, your computer will not have a DNS domain name and running the dnsdomainname command will return either nothing or the localhost.

Setting The NIS Domain Name

Set a NIS domain name for your computer using the following command:

sudo domainname mydomainname

You will probably need sudo to elevate your permissions.

The /etc/hosts File

The /etc/hosts file in NANO for Ubuntu on WSL

In a terminal window run the following command to open the hosts file in the Nano editor:

sudo nano /etc/hosts

You'll see several lines of text in the /etc/hosts file as follows: localhost 

The first part is the IP address of the computer, the second part is the computer name. To permanently add a NIS domain for the computer change the line as follows: localhost.yourdomainname

You can also add aliases as follows: localhost.yourdomainname mycomputer mylinuxcomputer

More About the 'domainname' Command

The domainname command offers several switches, including:

  • -a: Returns the aliases for the domain listed in the hosts file
  • -b: Sets a domain name
  • -d: DNS domain name
  • -f: Long host name
  • -F: Read the host name information from a file
  • -i: IP addresses for the host name
  • -I: All addresses for the host
  • -s: Short host name