5 ‘hostname’ Command Examples for Linux Newbies


Linux is a popular open-source operating system used by many developers, system administrators, and users worldwide. One of essential commands in Linux is 'hostname' command, which allows you to view or set hostname of your system. In this article, we will explore five 'hostname' command examples that can help Linux newbies understand how to use this command.

What is a Hostname?

A hostname is a unique name that identifies a computer on a network. It can be a combination of letters, numbers, and hyphens, and it usually ends with a domain name. For example, google.com, ubuntu.com, etc., are domain names.

The hostname command in Linux is used to display or set hostname of a system. Here are some examples of how to use hostname command.

View Current Hostname

The first and most simple example is to view current hostname of your system. To do this, open terminal and type following command −

hostname

This command will display current hostname of your system. For example, if your system hostname is 'ubuntu,' output will be −

ubuntu

Set a New Hostname Temporarily

You can set a new hostname for your system temporarily using 'hostname' command. This temporary hostname will be lost after system is restarted. To set a new hostname, type following command in terminal −

sudo hostname new-hostname

Replace 'new-hostname' with name you want to set as your temporary hostname. You will need to enter your password when prompted to run command with root privileges.

After running command, you can verify new hostname by typing following command −

hostname

The output will show new temporary hostname you just set.

Set a New Hostname Permanently

If you want to set a new hostname permanently, you need to modify 'hostname' file in /etc directory. To do this, follow steps below −

a) Open terminal and type following command to open 'hostname' file in nano editor −

sudo nano /etc/hostname

b) In nano editor, replace current hostname with new hostname you want to set.

c) Save changes and exit editor by pressing 'Ctrl+X', then 'Y', and finally 'Enter'.

d) Next, you need to modify hosts file to include new hostname. Type following command in terminal −

sudo nano /etc/hosts

e) In nano editor, locate line with '127.0.0.1' and replace current hostname with new hostname you want to set.

f) Save changes and exit editor by pressing 'Ctrl+X', then 'Y', and finally 'Enter'.

g) Finally, restart your system to apply new hostname.

After restarting your system, you can verify new hostname by typing following command −

hostname

The output will show new permanent hostname you just set.

Display FQDN (Fully Qualified Domain Name)

The FQDN (Fully Qualified Domain Name) is complete domain name that identifies a computer on a network. It includes hostname and domain name. To display FQDN of your system, type following command in terminal −

hostname -f

The output will show FQDN of your system. For example, if your system hostname is 'ubuntu' and your domain name is 'example.com,' output will be −

ubuntu.example.com

Display IP address associated with hostname

You can display IP address associated with hostname of your system using 'hostname' command. To do this, type following command in terminal −

hostname -I

The output will show IP address associated with hostname of your system. For example, if your system hostname is 'ubuntu' and its IP address is '192.168.1.10', output will be −

192.168.1.10

Change Hostname Without Restarting System

If you want to change hostname of your system without restarting it, you can use following command −

sudo hostnamectl set-hostname new-hostname

Replace 'new-hostname' with new hostname you want to set. This command will update hostname and write it to relevant configuration files without restarting system.

Check Current Hostname of a Remote Machine

If you need to check current hostname of a remote machine, you can use following command −

ssh user@remote-machine hostname

Replace 'user' with username you use to log in to remote machine, and 'remote-machine' with IP address or hostname of remote machine. This command will log in to remote machine and display its current hostname.

Use 'domainname' Command

In addition to 'hostname' command, Linux also has a 'domainname' command that can be used to display or set system's NIS/YP domain name. To display current NIS/YP domain name, type following command in terminal −

domainname

To set a new NIS/YP domain name, type following command −

sudo domainname new-domain-name

Replace 'new-domain-name' with new domain name you want to set.

Use 'uname' Command to Display System Information

The 'uname' command in Linux can be used to display various system information, including hostname. To display hostname using 'uname' command, type following command in terminal −

uname -n

The output will show current hostname of your system.

Debugging Networking Issues

If you're experiencing networking issues on your Linux system, 'hostname' command can be useful in troubleshooting problem. For example, if you're unable to connect to a remote machine, you can use 'hostname' command to verify hostname of remote machine and ensure that you're using correct hostname when trying to connect.

Similarly, if you're experiencing DNS resolution issues, you can use 'hostname' command to check current hostname and FQDN of your system and ensure that they're correctly configured in your DNS server.

Virtual Hosting

If you're hosting multiple websites or applications on a single server, you can use 'hostname' command to configure virtual hosting. Virtual hosting allows you to host multiple websites or applications on a single server, each with its own hostname and domain name.

To configure virtual hosting, you can use 'hostname' command to set hostname for each website or application and configure your web server to respond to requests for each hostname.

Security

The 'hostname' command can also be useful in securing your Linux system. By configuring a unique hostname for your system, you can make it more difficult for attackers to identify and target your system on a network.

In addition, some security tools and applications rely on hostname to function properly. For example, if you're using a firewall or intrusion detection system, you may need to configure rules or alerts based on hostname of your system.

Multi-homed Systems

If your Linux system has multiple network interfaces or IP addresses, 'hostname' command can be useful in configuring system's hostname for each interface or IP address.

To do this, you can use 'hostname' command with '-I' option to display IP address associated with each hostname. You can then configure your system's network interfaces to use appropriate hostname for each IP address.

Conclusion

The 'hostname' command is a simple yet powerful command that can help Linux newbies manage hostname of their systems. It can be used to view current hostname, set a new hostname temporarily or permanently, display FQDN, and display IP address associated with hostname.

By using 'hostname' command, Linux newbies can easily manage hostname of their systems and ensure that their systems are properly identified on a network. This command is essential for system administrators and developers who need to manage multiple systems on a network.

In conclusion, 'hostname' command is a must-know command for anyone using Linux, and Linux newbies can use examples provided in this article to become more familiar with this command.

Updated on: 10-Apr-2023

117 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements