The hostname is used to identify a computer or system in a network. The hostname is a user-generated value generally set as easy to remember the name. Operating systems generally set this name automatically during setup but also provide the ability to change it during Ubuntu installation. The hostname can be also changed after the Ubuntu installation. There are different ways to change Ubuntu hostname like hostname command, hostnamectl command, /etc/hostname file, etc.
Print Hostname with hostname Command
Before changing the Ubuntu hostname we can print or display the current hostname of the Ubuntu system. The hostname
command is used to display the current hostname of the Ubuntu via the command-line interface.
$ hostname
Print Hostname with hostnamectl Command
Another way to list the current hostname of the Ubuntu is using the hostnamectl
command. This command also provides extra information than the hostname like Kernel version, Architecture, Boot ID, Machine ID, etc. The hostname is provided via the Static hostname
line.
$ hostnamectl
Change Hostname with hostname Command
The hostname command is used to print and set the hostname of the Ubuntu. We can use the hostname in order to change the Ubuntu hostname easily just proıviding the new hostname as a parameter. But the hostname change is an administrator operation and requires root privileges. The sudo
command should be used with the hostname command in order to change the Ubuntu hostname.
$ sudo hostname linuxtect
Change Hostname with hostnamectl Command
The hostnamectl command is the next-generation command which is the successor of the hostname command. The hostnamectl command can be used to change hostname easily. The set-hostname
parameter is used with the hostnamectl command in order to set the newly provided hostname for Ubuntu.
$ hostnamectl set-hostname linuxtect
Change Pretty Hostname with hostnamectl Command
In some cases, the hostname can be set as a numeric or mechanical value in order to manage a bunch of Ubuntu systems in a row. This type of hostname is not human-friendly and hard to remember or use. Instead of the pretty hostname
can be used to set a hostname that is more human-friendly and easy to read. The --pretty
option is provided to set the pretty hostname for the Ubuntu.
$ hostnamectl set-hostname linuxtect --pretty
Change Hostname via /etc/hostname File
The hostname of the Ubuntu is stored in the file /etc/hostname
. The hostname and hostnamectl commands work with this file in order to display or change the hostname of the Ubuntu. We can change this file content directly in order to change the hostname of the Ubuntu. First, open this file with a text editor which is nano
in this example.
$ nano /etc/hostname
Delete the current hostname and put a new hostname like below and save the file.
linuxtect
Change Hostname via GUI
The hostname can be changed via the GUI by using the Settings
tool. First, open the settings tool and then navigate to the About
section via the left panel like below.
In the About section click to the Device Name
which opens the change screen.
In the following screen set the hostname which is also called as device name and then click to the Rename
button like below.