27 C
Monday, March 8, 2021

How To Set Or Change Hostname On Linux

Must read

We already discussed how to view or find a Linux system’s hostname. In this brief guide, we will see how to set or change hostname in Linux, either temporarily or permanently.

What is Hostname?

A Hostname is an unique, alphanumeric label assigned to a Linux system to identify it on the network. A typical hostname can contain alphabets, numbers and a few special characters such as hyphen (-), period (.), and underscore (_). Generally, a hostname consist of up to 253 characters. Please note that a hostname should not start with a hyphen. In most Linux distributions, the hostname is stored in the /etc/hostname file.

The hostname is usually set while installing the OS. You can also set a new hostname or change the existing hostname after installing the OS, either from command line or from Settings section in graphical mode. First, we will see the CLI way to set Linux hostname.

1. Set Or Change Hostname On Linux From Commandline

Changing computer name in Linux is no big deal. We can set a Linux system’s name with a few one-liner commands.

The simplest and quickest way to change a Linux system’s hostname is:

$ hostname <new_hostname>


$ hostname ostechnix

This command sets the hostname as ostechnix. Please note that this command will only set the computer name temporarily. Once you reboot the system, the hostname will be reverted to old name. To permanently set hostname in Linux, use any one of the following methods.

There are many ways to permanently set or change Linux hostname from commandline. Here are a few methods:

  1. Using hostnamectl command,
  2. Using nmcli command,
  3. Editing /etc/hostname file,
  4. Using sysctl command,
  5. Editing /proc/sys/kernel/hostname file.

1.1. Set Linux hostname with hostnamectl command

The hostnamectl command is used to display and change the system hostname and related settings in Linux systems.

Let us check the current hostname with command:

$ hostname

As you see in the above output, my Ubuntu system’s name is “ubuntuserver”. Let us change it to ostechnixserver.

To set hostname in Linux with hostnamectl command, run:

$ sudo hostnamectl set-hostname ostechnixserver
Set Linux hostname with hostnamectl command

Replace the “ostechnixserver” in the above example with your own name.

Log out and log back in to take effect the changes. If you are on SSH session, exit the session and reconnect it.

Now verify if the hostname has been changed or not with hostnamectl command:

$ hostnamectl 
   Static hostname: ostechnixserver
Transient hostname: ubuntuserver
         Icon name: computer-vm
           Chassis: vm
        Machine ID: cb4ade4901394ac5a0d25fa6b3889e6c
           Boot ID: 78b6e9d83e0e481eb960423e7947e45c
    Virtualization: oracle
  Operating System: Ubuntu 20.04 LTS
            Kernel: Linux 5.4.0-60-generic
      Architecture: x86-64

Or display only the hostname:

$ hostnamectl --static
View Linux hostname with hostnamectl command
View Linux hostname with hostnamectl command

Alternatively, you can use the hostname command:

$ hostname
View Linux hostname
View Linux hostname

1.2. Set Linux hostname with nmcli command

The nmcli is a commandline tool used to create, display, edit, delete, activate, and deactivate network
connections, as well as control and display network device status in Linux operating systems.

To set hostname in Linux with nmcli command, do:

$ sudo nmcli general hostname ostechnixserver
Set Linux hostname with nmcli command
Set Linux hostname with nmcli command

Log out and log back in to apply the changes.

1.3. Change Linux hostname by editing /etc/hostname file

As mentioned already, the computer name is stored in /etc/hostname file in most Linux distributions.

Let us look at the contents of this file:

$ cat /etc/hostname

To change the Linux system’s hostname, edit /etc/hostname file with your favorite editor:

$ sudo nano /etc/hostname

Replace old hostname with new hostname of your choice.

Change Linux hostname
Change Linux hostname

Save the file and close it. Log out and lag back in to take effect the changes.

Alternatively, use the following one-liner command to update the hostname in /etc/hostname file:

$ echo "ostechnixserver" > sudo tee -a /etc/hostname

1.4. Change Linux hostname with sysctl command

Sysctl is used to modify kernel parameters at runtime. The parameters available are those listed under /proc/sys/. Procfs is required for sysctl support in Linux. You can use sysctl to both read and write sysctl data.

To change hostname in Linux with sysctl command, run:

$ sudo sysctl kernel.hostname=ostechnixserver

Replace “ostechnixserver” with your own hostname. Don’t forget to log off and log back in to apply the changes.

1.5. Change Linux hostname with ProcFs

Proc file system, or procfs in short, is a virtual file system maintained by the Linux kernel. It is also sometimes referred to as a process information pseudo-file system. It doesn’t contain ‘real’ files but runtime system information such as system memory, devices mounted, hardware configuration etc. All these information are available under a special directory named /proc in Linux.

If you want to update the hostname, just append the new hostname in /proc/sys/kernel/hostname file using command:

$ sudo sh -c "echo 'ostechnixserver' > /proc/sys/kernel/hostname"

You need to log out and log back in to apply the changes.

2. Set Or Change Hostname On Linux From Graphical mode

This method is for any Linux distribution installed with GNOME desktop environment.

Open the Gnome Settings section from Dash.

Launch Ubuntu Gnome Settings
Launch Ubuntu Gnome Settings

Navigate to About -> Device Name. Click the Device Name option and enter the new hostname.

Change hostname from Gnome settings in Linux
Change hostname from Gnome settings in Linux

Hope this helps.

Related read:

Source link

- Advertisement -

More articles


Please enter your comment!
Please enter your name here

Latest article