Skip to Content

3 ways to check hostname in Linux

Do you know how to check your hostname in Linux? There are a few different ways that you can do this, and in this blog post we will discuss three of them. Knowing your hostname is important because it allows you to identify which computer you are working on. If you are ever having problems with your system, being able to identify the hostname can help troubleshoot the issue. Let’s take a look at three ways to check your hostname in Linux!

Th following commands can be used to check hostname in Linux

  • hostname
  • uname -n
  • cat /etc/hostname

 

Understanding hostname in Linux

Hostname is the computer name that one may have chosen to identify their device. Hostname, or a computer’s name, can be anything you want it to be. It is just like choosing your own URL and has very few limitations — unless you’re managing a network of computers and need to follow certain rules for naming.

Check hostname with hostname command in Linux

The best way to check your hostname is to use the ‘hostname’ command. This command will print out the hostname of the system. You can also use the ‘-i’ option to get the IP address of the system as well.

$ hostname
mycomputer
$ hostname -i
192.168.0.100

The ‘hostname’ command can be used to change the hostname of the system as well. For example, if you want to change your hostname to ‘mynewcomputer’, you can use the following command. The new name will be lost after you reboot your server.

$ hostname mynewcomputer

check hostname with uname command in Linux

Another way to check your hostname is to use the ‘uname’ command with the ‘-n’ option. This will print out the network node hostname of the system. The ‘uname’ command can be used to get a variety of information about the system, including the hostname. You can use the ‘-a’ option to get all of the information. Another option is the ‘-s’ option which prints out the kernel name.

To use the ‘uname’ command in Linux, you can use the following syntax. uname [-a] [-n] [-s]

$uname -n
mycomputer
$uname -a
Linux mycomputer.example.com

check hostname from /etc/hostname file in Linux

Another way to check your hostname is to look at the ‘/etc/hostname’ file. This file contains the hostname of the system, and is used by many programs to identify the system.

$ cat /etc/hostname
mycomputer

The ‘/etc/hostname’ file can be edited to change the hostname of a system as well. For example, if you want to change your hostname to ‘mynewcomputer’, you can use the following command.

$ vi /etc/hostname
mynewcomputer

After saving the file, you will need to reboot the system for the changes to take effect.

As you can see, there are a few different ways that you can check your hostname in Linux. Try out these three methods the next time you need to check your hostname in Linux!