Modifying the hostname in Linux is a crucial task for system administrators and users seeking to customize their systems. The hostname, which identifies a device within a network, might need to be changed for several reasons, such as reconfiguring the system or managing the network efficiently. This guide outlines detailed steps for altering the hostname across various Linux distributions.
Important note:
Before you move forward with modifying the hostname in Linux, confirm that you have access to a Linux system with the required administrative rights, a basic understanding of Linux commands and terminal operations, and an SSH client if you are working on the system remotely.
Modifying Hostname in Linux
You will explore different ways of changing the hostname in the Linux system:
- Using hostnamectl
This command is utilized for setting and altering the hostname in Linux. This approach is suitable for contemporary Linux distributions. Follow the below steps:- Connect to your Linux system through SSH.
- Sign in as the root user or perform admin commands using sudo.
- To check the current hostname, execute the following command:
hostnamectl
- Modify the hostname using the subsequent command:
sudo hostnamectl set-hostname new-hostname
- Now, to verify the change, run the below command:
hostnamectl
- Using sysctl
Another way is to use the “sysctl” command, which allows you to modify the kernel parameter for the hostname momentarily. Follow the steps:- Connect to terminal window vis SSH.
- To change the hostname, run the following command:
sudo sysctl kernel.hostname=new-hostname
- Keep in mind that this approach is not permanent and will not remain effective after a reboot.
Let’s see how to change the Hostname on various distributions:
- Ubuntu: Use the hostnamectl command for versions 16.04 and later. For older versions, edit the “/etc/hostname” and “/etc/hosts” files manually.
- Debian: Supports the hostnamectl command. Older versions require updates to the “/etc/hostname” file and modifications to the “/etc/hosts” file.
- CentOS: The hostnamectl command is available from CentOS 7 onwards. In CentOS 6 and earlier, update the “/etc/sysconfig/network” file manually.
- RedHat: RedHat 7 and newer support the hostnamectl command. For older versions, modify the /etc/sysconfig/network file.
Troubleshooting Hostname Changes
If you run into problems while altering the hostname, try these tips:
- Check Permissions: Make sure you have the necessary permissions to modify system settings.
- Avoid Typos: Double-check for any typos in the hostname you’re setting.
- Use Right Commands: Ensure you’re using the right command for your specific Linux distribution.
- Restart Services: If the hostname change doesn’t take effect right away, restart your system or network services.
By following the above-outlined steps, you can change the hostname in Linux. If you need further assistance, don’t hesitate to reach out to our support team.
Working with Linux hostnames? Learn How to change the hostname of server in WHM