Objective

The objective is to change system's hostname on Ubuntu 18.04 Bionic Beaver Linux ( server or desktop )

Operating System and Software Versions

  • Operating System: - Ubuntu 18.04 Bionic Beaver
  • Software: - systemd 235 or higher

Requirements

Privileged access to your Ubuntu System as root or via sudo command is required.

Difficulty

EASY

Conventions

  • # - requires given linux commands to be executed with root privileges either directly as a root user or by use of sudo command
  • $ - requires given linux commands to be executed as a regular non-privileged user

Instructions

Obtain current hostname

Use hostnamectl command to retrieve a current system hostname:
$ hostnamectl
   Static hostname: ubuntu
         Icon name: computer-vm
           Chassis: vm
        Machine ID: ceb8b579410b472899a95049f8a61e05
           Boot ID: 3f73eea15fb34a38937488149143d9d3
    Virtualization: oracle
  Operating System: Ubuntu Bionic Beaver
            Kernel: Linux 4.13.0-32-generic
      Architecture: x86-64

Change Hostname

hostnamectl command can also be used to set new or update the current hostname on Ubuntu 18.04 Bionic Beaver. The following linux command will change system's static hostname to linuxconfig:
$ sudo hostnamectl set-hostname linuxconfig
Futhermore, check for the existence of /etc/cloud/cloud.cfg configuration. If the file exists edit the file and change the settings within:
FROM:
preserve_hostname: false
TO:
preserve_hostname: true
The above will preserve your new hostname after reboot.

Next, confirm the new hostname:
$ hostnamectl 
   Static hostname: linuxconfig
         Icon name: computer-vm
           Chassis: vm
        Machine ID: ceb8b579410b472899a95049f8a61e05
           Boot ID: 3f73eea15fb34a38937488149143d9d3
    Virtualization: oracle
  Operating System: Ubuntu Bionic Beaver
            Kernel: Linux 4.13.0-32-generic
      Architecture: x86-64
Your command line prompt will update to reflect the new hostname settings the next time you login.

Appendix

You may also want to update your /etc/hosts file so the system can resolve itself via new host name. Open /etc/hosts file:
$ sudo nano /etc/hosts
and append your new hostnane right after the old hostname:
$ cat /etc/hosts
127.0.0.1       localhost
127.0.1.1       ubuntu linuxconfig

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