Setting a persistent hostname in Linux

 

A Linux hostname is the name of your computer on a network.

It is used to identify your computer to other computers on the network.

For example, if your computer's hostname is "myserver", other computers on the network can refer to it as "myserver".

A Linux hostname is not the same as your computer's IP address.

IP addresses are used to route traffic to your computer on the network. Hostnames are used to identify your computer to other computers on the network.

You can think of a Linux hostname like the name of your house. It is used to identify your house from other houses on the street.

Your IP address though is like your house's address. It is used to route mail to your house.

 

What does it mean to set a "hostname":

Setting a persistent hostname in Linux means that the hostname will remain the same even after the system reboots.

Before we begin, here are some additional things to keep in mind when setting a persistent Linux hostname:

  • The hostname must be unique on the network. Ensure that the new hostname doesn't conflict with existing names in your network.
  • The hostname cannot be longer than 64 characters.
  • The hostname cannot contain spaces or special characters.
  • You can also set the hostname in the /etc/hostname file. However, this is not a persistent change, as the hostname will be reset to the default value after the system is rebooted.
  • Always back up important configuration files before making changes.

 

How to set a hostname based on different Linux distributions:

 

cPanel & WHM

For cPanel & WHM servers, you need to use one of the following methods to ensure that all of the necessary system and service changes occur:

  • Use WHM's Change Hostname interface (Home » Networking Setup » Change Hostname).
  • Call WHM API 1's sethostname function:
            whmapi1 --output=jsonpretty \
            sethostname \
            hostname='$myservername.example.com'
            
  • Run the set_hostname utility as the root user:
    /usr/local/cpanel/bin/set_hostname $myservername.example.com [option]

For Systems Using systemd (e.g., Ubuntu 16.04+, Debian 8+, CentOS 7+)

  1. Open a terminal window. Run the following command to see the current hostname:
    hostname
  2. Use the following command to set the hostname, replacing the current hostname with the desired hostname. For example, if you want to change the hostname to "myserver", you would run the following command:
    sudo hostnamectl set-hostname myserver
  3. Run the following command to verify that the hostname has been changed:
    hostnamectl
    or:
    hostname
  4. You may want to restart the networking service:

    Using the systemctl command (safer option):

    sudo systemctl restart NetworkManager

     

    IMPORTANT: Previously you can use:

    systemctl restart network

    But Network scripts were deprecated in Red Hat Enterprise Linux 8 and no longer provided by default. In Red Hat Enterprise Linux 8, NetworkManager must be running to run the "ifup" and the "ifdown" scripts. By default if you try to use the legacy network.service unit file is not available with RHEL 8, you will get this error:

    Failed to restart network.service: Unit network.service not found.

     

    For AlmaLinux, you can also use the following command to restart the server networking service:

    nmcli networking off
    nmcli networking on

     

    For Rocky Linux, you can also use the following command to restart the server networking service. Be super-careful with this when you are on a production machine as you may lose connection to the system.

    nmcli networking off
    nmcli networking on

    This will restart the networking service, which will bring down all network interfaces and then bring them back up.

     

    Using the ip link command:

    sudo ip link set down all
    sudo ip link set up all

    This will bring down all network interfaces and then bring them back up.

    The systemctl command is the preferred method for restarting the network in Linux systemd, as it will also restart any network-related services that are dependent on the networking service. The ip link command is a more manual approach, but it can be used if the systemctl command is not available.

     

    Method Description
    systemctl restart NetworkManager As a dynamic network control and configuration daemon, NetworkManager keeps network devices and connections up and active when available.
    ip link set down all && ip link set up all Brings down all network interfaces and then brings them back up.

     

    Now check the server network status:

    sudo /etc/init.d/networking status
    or
    sudo systemctl status NetworkManager
    or:
    nmcli -o

 

For Debian/Ubuntu (Pre-16.04)

  1. Open the /etc/hostname file with a text editor:
    sudo nano /etc/hostname
  2. Replace the existing hostname with the desired one.
  3. Save and exit the editor.
  4. Open the /etc/hosts file:
    sudo nano /etc/hosts
  5. Find the line starting with 127.0.1.1 and replace the existing hostname with the new one.
  6. Save and exit the editor.
  7. Reboot the system or run:
    sudo /etc/init.d/hostname.sh start

 

For Red Hat/CentOS (Pre-7)

  1. Open the /etc/sysconfig/network file:
    sudo nano /etc/sysconfig/network
  2. Change the HOSTNAME value to your desired hostname:
    HOSTNAME=your_hostname
  3. Save and exit the editor.
  4. Update the /etc/hosts file similarly to the Debian/Ubuntu instructions.
  5. Reboot the system for the hostname change to take effect. Or run:
    sudo service network restart
Var dette svaret til hjelp? 0 brukere syntes dette svaret var til hjelp (0 Stemmer)