Introduction
In this practical guide, you will discover how to effectively manage the hostname of a Linux system. This involves using the command line tools hostname
and hostnamectl
. This lab explores the essential usage of the hostname
command and the more feature-rich hostnamectl
command for managing hostname settings. You'll learn how to display the current hostname, and explore various options to reveal different aspects of hostname information. Furthermore, you will gain proficiency in changing the system's hostname using the hostnamectl
command. This is crucial for any aspiring systemadmin.
Explore the hostname Command
In this section, we will delve into the hostname
command, a fundamental tool for displaying or setting the system's hostname.
Let's begin by checking the current hostname of the system:
hostname
Example output:
ubuntu
The hostname
command, executed without arguments, simply displays the system's current hostname.
Next, we'll use the hostname
command to display more detailed information about the system:
hostname -a
hostname -d
hostname -f
hostname -i
hostname -s
Example output:
ubuntu
example.com
ubuntu.example.com
172.17.0.2
ubuntu
hostname -a
: Shows the alias hostname.hostname -d
: Shows the DNS domain name.hostname -f
: Shows the fully qualified domain name (FQDN).hostname -i
: Shows the network addresses associated with the host.hostname -s
: Shows the short hostname.
As illustrated, the hostname
command offers several options to display various aspects of the system's hostname. Understanding these options is key for a Linux systemadmin.
Manage Hostnames with the hostnamectl Command
In this section, we will investigate the hostnamectl
command, offering a more comprehensive approach to managing system hostnames.
First, let's examine the current hostname settings using hostnamectl
:
hostnamectl
Example output:
Static hostname: ubuntu
Icon name: computer-vm
Machine ID: 7b6d7b3f1d9d4c5a8d1a2b3c4d5e6f7
Boot ID: 9a8b7c6d5e4f3a2b1c0d9e8f7a6b5
Virtualization: docker
Operating System: Ubuntu 22.04 LTS
Kernel: Linux 5.15.0-1023-aws
Architecture: x86-64
The hostnamectl
command displays a wealth of information about the system, including the static hostname, icon name, machine ID, boot ID, virtualization type, operating system, kernel version, and architecture. This information is invaluable for system administrators.
Next, let's change the hostname using hostnamectl
. Remember you'll often need root privileges:
sudo hostnamectl set-hostname new-hostname
After executing this command, the system's hostname will be updated to "new-hostname". This change might require a reboot for full effect.
To verify the new hostname, run:
hostnamectl
Example output:
Static hostname: new-hostname
Icon name: computer-vm
Machine ID: 7b6d7b3f1d9d4c5a8d1a2b3c4d5e6f7
Boot ID: 9a8b7c6d5e4f3a2b1c0d9e8f7a6b5
Virtualization: docker
Operating System: Ubuntu 22.04 LTS
Kernel: Linux 5.15.0-1023-aws
Architecture: x86-64
As you can observe, the static hostname has been successfully updated to "new-hostname".
Customize the Hostname on Ubuntu 22.04
In this final section, we will learn how to manually customize the hostname on an Ubuntu 22.04 system, providing an alternative approach to hostname management.
First, let's confirm the current hostname again:
hostnamectl
Example output:
Static hostname: new-hostname
Icon name: computer-vm
Machine ID: 7b6d7b3f1d9d4c5a8d1a2b3c4d5e6f7
Boot ID: 9a8b7c6d5e4f3a2b1c0d9e8f7a6b5
Virtualization: docker
Operating System: Ubuntu 22.04 LTS
Kernel: Linux 5.15.0-1023-aws
Architecture: x86-64
To manually change the hostname, we need to edit the /etc/hostname
file. This requires root privileges:
sudo nano /etc/hostname
In the file, replace the existing hostname with the desired new hostname, for example, "my-custom-hostname":
my-custom-hostname
Save the file and exit the text editor. This usually involves pressing Ctrl+X, then Y, then Enter.
Next, we need to update the /etc/hosts
file to reflect the new hostname. Again, root privileges are needed:
sudo nano /etc/hosts
Locate the line that begins with "127.0.0.1" and replace the hostname with the new one:
127.0.0.1 my-custom-hostname localhost
Save the file and exit the text editor.
Finally, let's verify the new hostname:
hostnamectl
Example output:
Static hostname: my-custom-hostname
Icon name: computer-vm
Machine ID: 7b6d7b3f1d9d4c5a8d1a2b3c4d5e6f7
Boot ID: 9a8b7c6d5e4f3a2b1c0d9e8f7a6b5
Virtualization: docker
Operating System: Ubuntu 22.04 LTS
Kernel: Linux 5.15.0-1023-aws
Architecture: x86-64
The hostname has been successfully changed to "my-custom-hostname". A reboot might be necessary for all applications to recognize the change.
Summary
In this lab, we examined the hostname
command, a tool for displaying or setting the system's hostname. We learned to use its various options to display different aspects of the system's hostname, such as the alias, DNS domain name, fully qualified domain name (FQDN), network addresses, and short hostname. We then explored the hostnamectl
command, providing a more complete solution for managing the system's hostname. We saw how to use hostnamectl
to display detailed system information, including the static hostname, icon name, machine ID, boot ID, virtualization type, operating system, kernel version, and architecture. Finally, we demonstrated how to change the hostname using the hostnamectl set-hostname
command. Mastering these techniques is fundamental for any Linux systemadmin responsible for server management.