How to modify the hostname on Ubuntu 20.04

This article was first published in: https://www.itcoder.tech/posts/how-to-change-hostname-on-ubuntu-20-04/

Usually the host name is set during the installation of the operating system or dynamically specified during the creation of the virtual machine.

This guide explains how to set or change the hostname on Ubuntu 20.04 without rebooting the system.

One, understand the host name##

A host name is the label name that marks a device on the network. In the same network, you should not have two or more machines with the same hostname.

In Ubuntu, you can use the hostnamectl command to edit the system hostname and related settings. This tool recognizes three different host names:

We strongly recommend using the full domain name (FQDN) to set static and transient names, such as host.example.com.

Only root or users with sudo authority can modify the system hostname.

Two, display the current host name##

To view the current host name, run the hostnamectl command without any parameters:

hostnamectl

In this example, the current hostname is set to: ubuntu2004.localdomain.

Three, modify the system host name##

Modifying the system hostname is a simple process, the syntax is as follows:

sudo hostnamectl set-hostname host.example.com
sudo hostnamectl set-hostname "Your Pretty HostName"--pretty
sudo hostnamectl set-hostname host.example.com --static
sudo hostnamectl set-hostname host.example.com --transient

For example, if you want to modify the hostname of the system today to neptune.linuxize.com, you can use the following command:

sudo hostnamectl set-hostname neptune.linuxize.com

You can also set a pretty hostname:

sudo hostnamectl set-hostname "Linuxize's laptop"--pretty

hostnamectl does not produce any output. If successful, return 0, otherwise, return a non-zero error code.

The static hostname is stored in /etc/hostname, and the pretty hostname is stored in the /etc/machine-info file.

You should not use the same hostname on different machines on the same network.

In most systems, the host name is mapped to 127.0.0.1 in /etc/hosts. Open the file and modify the old hostname to the new hostname.

127.0.0.1 localhost
127.0.0.1 neptune.linuxize.com

# The following lines are desirable for IPv6 capable hosts
::1  localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

If you are running Ubuntu on a cloud instance and have the cloud-init package installed, you can also edit the /etc/cloud/cloud.cfg file. This software package is provided by the Cloud Server vendor and is usually installed by default, and it can be used to handle the initialization of the cloud server instance.

If the file exists on your system, open it:

sudo nano /etc/cloud/cloud.cfg

Search for "preserve_hostname" and change the value from false to true.

# This will cause the set+update hostname module to not operate(iftrue)
preserve_hostname:true

Save the file and close the editor.

Fourth, verify and modify##

To verify that the hostname has been completely modified, enter the hostnamectl command:

hostnamectl

Your new hostname will be printed out on the terminal.

 Static hostname: neptune.linuxize.com
 Pretty hostname: Linuxize's desktop
   Icon name: computer-vm
   Chassis: vm
  Machine ID: a04e3543f3da460294926b7c41e87a0d
   Boot ID: aa31b274703440dfb622ef2bd84c52cb
 Virtualization: oracle
 Operating System: Ubuntu 20.04 LTS
   Kernel: Linux 5.4.0-26-generic
  Architecture: x86-64

Five, summary##

We have shown you how to easily modify the hostname on Ubuntu 20.04 without rebooting the machine.

There are many reasons why you need to change the hostname. The most common reason is that the host name needs to be set automatically when the instance is created.

Original: https://linuxize.com/post/how-to-change-hostname-on-ubuntu-20-04/

Copyright notice: This work is licensed under the Creative Commons Attribution-Share in the Same Way 4.0 International License Agreement.

If you have any questions, please contact us in the following ways:

WeChat:

WeChat group: add the above WeChat, remark the WeChat group

QQ: 3217680847

QQ Group: 82695646

Recommended Posts

How to modify the hostname on Ubuntu 20.04
How to modify the hostname on CentOS 8
How to set or modify the time zone on Ubuntu 20.04
How to run the parsing server on Ubuntu 14.04
How to check the installed JDK on Ubuntu
How to install the latest MySQL on Ubuntu 18.04
How to add the gzip module to Nginx on Ubuntu 14.04
How to install Ruby on Ubuntu 20.04
How to install Memcached on Ubuntu 20.04
How to install Java on Ubuntu 20.04
How to install MySQL on Ubuntu 20.04
How to install VirtualBox on Ubuntu 20.04
How to install Elasticsearch on Ubuntu 20.04
How to install Protobuf 3 on Ubuntu
How to install Apache on Ubuntu 20.04
How to install Git on Ubuntu 20.04
How to install Node.js on Ubuntu 16.04
How to install MySQL on Ubuntu 20.04
How to install Vagrant on Ubuntu 20.04
How to install Bacula-Web on Ubuntu 14.04
How to install Anaconda3 on Ubuntu 18.04
How to install Memcached on Ubuntu 18.04
How to install Jenkins on Ubuntu 16.04
How to install MemSQL on Ubuntu 14.04
How to install Go on Ubuntu 20.04
How to install MongoDB on Ubuntu 16.04
How to install Mailpile on Ubuntu 14.04
How to install PrestaShop on Ubuntu 16.04
How to upgrade to PHP 7 on Ubuntu 14.04
How to install Skype on Ubuntu 20.04
How to install Jenkins on Ubuntu 20.04
How to install Python 3.8 on Ubuntu 18.04
How to install KVM on Ubuntu 18.04
How to install KVM on Ubuntu 20.04
How to install opencv3.0.0 on ubuntu14.04
How to install Anaconda on Ubuntu 20.04
How to install Prometheus on Ubuntu 16.04
How to install Jenkins on Ubuntu 18.04
How to deploy Django on Ubuntu 14.04
How to install Apache on Ubuntu 20.04
How to install R on Ubuntu 20.04
How to install Moodle on Ubuntu 16.04
How to install Solr 5.2.1 on Ubuntu 14.04
How to install Teamviewer on Ubuntu 16.04
How to secure Nginx on Ubuntu 14.04
How to install MariaDB on Ubuntu 20.04
How to install Mono on Ubuntu 20.04
How to install Go on Ubuntu 20.04
How to install Zoom on Ubuntu 20.04
How to uninstall software on Ubuntu
How to install Nginx on Ubuntu 16.04
How to install OpenCV on Ubuntu 20.04
How to install Spotify on Ubuntu 20.04
How to install Postman on Ubuntu 18.04
How to install Go 1.6 on Ubuntu 16.04
How to install MySQL on Ubuntu 14.04
How to install PostgreSQL on Ubuntu 20.04
How to install VLC on Ubuntu 18.04
How to install TeamViewer on Ubuntu 20.04
How to install Webmin on Ubuntu 20.04
How to install Docker Compose on Ubuntu 18.04