How to change hostname on ubuntu 20.xx

February 07, 2021

ubuntu
configuration

Check your current hostname using hostname or hostnamectl:

$ hostname

ubuntu

using hostnamectl:

$ hostnamectl
   Static hostname: ubuntu
         Icon name: computer
        Machine ID: 4e7602fbc48335fd9170deb9e360206c
           Boot ID: 97f5788224fasde59791a12a8fa7ec69
  Operating System: Ubuntu 20.10
            Kernel: Linux 5.8.0-1013-generic
      Architecture: x86-64

To change the hostname to mylinux or any other desired hostname use the following hostnamectl command:

$ sudo hostnamectl set-hostname mylinux

next, edit the hosts file in /etc to reflect the changes:

sudo vi /ect/hosts

append here the new hostname after localhost:

127.0.0.1 localhost
127.0.1.1 mylinux

to check the new hostname use again hostname or hostnamectl:

$ hostnamectl
   Static hostname: mylinux
         Icon name: computer
        Machine ID: 4e7602fbc48335fd9170deb9e360206c
           Boot ID: 97f5788224fasde59791a12a8fa7ec69Chan
  Operating System: Ubuntu 20.10
            Kernel: Linux 5.8.0-1013-generic
      Architecture: x86-64

Written by huckbit web developer who lives and works in UK building cool things. You can find me on twitter.