Configuring a Static Hostname

Configuring a Static Hostname

What is a Hostname?

In a Linux environment, the hostname serves as an identifier for the system on a network. While many systems automatically generate hostnames during installation, it’s often beneficial to set a static hostname to ensure consistency and ease of identification. In this article, we’ll explore how to establish a static hostname using the hostnamectl command in Linux.

A hostname is a label assigned to a device on a network. It typically consists of a name followed by a domain name, forming a fully qualified domain name (FQDN). Hostnames are used for communication between devices on a network and can help identify the system’s purpose or location.

Why Set a Static Hostname?

Setting a static hostname ensures consistency and predictability across networked systems. It simplifies system administration tasks, such as configuring network services, accessing remote systems, and identifying devices in log files and monitoring tools.

Conclusion

Configuring a static hostname using hostnamectl in Linux is a simple yet essential task for ensuring consistency and ease of identification in a networked environment. By following the steps outlined in this article, users can establish a static hostname for their systems with confidence, enhancing system administration and network management capabilities.

Configuring a Static Hostname

Checking the Current Hostname

Before setting a static hostname, it’s essential to check the current hostname to ensure clarity.

[root@localhost ~]# hostnamectl
   Static hostname: (unset)                          
Transient hostname: localhost
         Icon name: computer-vm
           Chassis: vm 🖴
        Machine ID: e260d02bfdeb46f298de406b45839d31
           Boot ID: a1f9b581895b4985a3a8c1fbd121f93c
    Virtualization: kvm
  Operating System: Rocky Linux 9.3 (Blue Onyx)      
       CPE OS Name: cpe:/o:rocky:rocky:9::baseos
            Kernel: Linux 5.14.0-362.8.1.el9_3.x86_64
      Architecture: x86-64
   Hardware Vendor: Red Hat
    Hardware Model: KVM
  Firmware Version: 1.16.1-1.el9

Setting a Static Hostname

To set a static hostname, use the hostnamectl command followed by the set-hostname option and the desired hostname.

[root@localhost ~]# hostnamectl set-hostname vm1.lab.hackelarre.cc

Updating /etc/hosts file

It’s a good practice to update the /etc/hosts file with the new hostname to ensure proper name resolution. Add the following line, replacing your_ip_address and myhostname with your actual IP address and hostname.

[root@localhost ~]# vim /etc/hosts
127.0.0.1          localhost
::1                localhost
192.168.122.188    vm1.lab.hackelarre.cc

Rebooting the System and Verifying the Changes

While not necessary, reboot the system can ensure that the changes take effect. Confirm that the hostname has been successfully updated by checking the hostname.

[root@localhost ~]# systemctl reboot
[root@vm1 ~]# hostname
vm1.lab.hackelarre.cc

Leave a Reply

WordPress Appliance - Powered by TurnKey Linux