Everything You Need To Know About The Domainname Command

Find The Domain Name For Your Computer
Find The Domain Name For Your Computer.


This guide will introduce you to 5 commands as follows:

  • hostname
  • domainname
  • ypdomainname
  • nisdomainname
  • dnsdomainname

You can find out full details about the hostname command by reading this guide which was updated recently.

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.

You can find out your computer's hostname by running the following command in a terminal window.


In my case the result was simply "garymint".

On some machines your hostname may show up as something like this "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

The domainname Command

Instead of using the hostname with the minus 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).

The domainname command returns the NIS domain name of the system. So what is the NIS domain name?

NIS stands for Network Information System. This guide defines NIS as follows:

NIS is a Remote Procedure Call (RPC)-based client/server system that allows a group of machines within an NIS domain to share a common set of configuration files. This 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 ypdomainname Command

The YPDomainName actually displays the same information as the domainname command. Try it for yourself by typing the following into a terminal window:


So why are there multiple commands for the same thing?

YP stands for Yellow Pages but had to be changed due to legal reasons.

This was changed to NIS which was mentioned in the previous section.

You can use ypdomainname if you so wish but you may as well save your fingertips some effort and stave off that RSI by leaving it to just domainname.

The nisdomainname Command

The nisdomainname also displays the same information as the domainname command. As you will have gathered by the previous sections there used to be a yellow pages domain name which could be returned by using the ypdomainname command.

The yellow pages domain name was changed to network information system (NIS) and so the nisdomainname command came about.

The domainname command was subsequently created for ease of use.

You can use the nisdomainname command as follows:


The results will be the same as the domainname command.

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.about.com.

The chances are that unless you are running a web server your computer will not have a DNS domain name and running the dnsdomainname command will return nothing.

Setting The NIS Domain Name

You can set an NIS domain name for your computer using the following command:

sudo domainname mydomainname

You will probably need sudo to elevate your permissions.

You can also use the ypdomainname and nisdomainname commands as follows:

sudo ypdomainname mydomainname

sudo nisdomainname mydomainname

The /etc/hosts File

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

sudo nano /etc/hosts

There will be a number of 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 an 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 has a number of switches as follows:

domainname -a

This will return the aliases for the domain listed in the hostfile.

domainname -b

The domain name that will be used if no other is set.

You can set the domain name that will be used using the above switch by specifying the name as part of the command line as follows:

domainname -b mydomainname

Here are some more commands:

  • domainname -d - DNS domain name
  • domainname -f - Long host name
  • domainname -F - read the host name information from a file
  • domainname -i - IP addresses for the host name
  • domainname -I - All addresses for the host
  • domainname -s - Short host name
  • domainname -y - NIS/YP domain name


For more information about Linux and network administration it is worth reading the Linux Network Administrator's Guide.