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.
Table of Contents
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 hostname in Linux.
1. Change Hostname in Linux from Commandline
Changing computer name in Linux is easy! 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>
E.g:
$ 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:
- Using
hostnamectl
command, - Using
nmcli
command, - Editing
/etc/hostname
file, - Using
sysctl
command, - Editing
/proc/sys/kernel/hostname
file.
1.1. Assign hostname with hostnamectl command in Linux
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 ubuntuserver
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
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 ostechnixserver
Alternatively, you can use the hostname
command:
$ hostname ostechnixserver
1.2. Set 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
Log out and log back in to apply the changes.
1.3. Configure hostname by editing /etc/hostname file in Linux
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 ubuntuserver
To configure your 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.
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 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 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 Hostname in Linux from Graphical mode
This method is for any Linux distribution installed with GNOME desktop environment.
Open the Gnome Settings section from Dash.
Navigate to About -> Device Name. Click the Device Name option and enter the new hostname.
Hope this helps.
Related read: