Ubuntu Linux Change Hostname (computer name)

I am a new Ubuntu Linux laptop user. I setup my computer name to ‘tom’ during installation but now I would like to change the computer name to ‘jerry’. Can you tell me how do I remove tom and set it to jerry on Ubuntu Linux? How do I change the Ubuntu computer name from ‘ubuntu’ to ‘AvlinStar’? Can you tell me more about Ubuntu Linux change hostname command?

You can use the hostname command or [nixmd name=”hostnamectl”] to see or set the system’s host name. The host name or computer name is usually at system startup in /etc/hostname file. Open the terminal application and type the following commands to set or change hostname or computer name on Ubuntu Linux.

Ubuntu change hostname command

The procedure to change the computer name on Ubuntu Linux:

  1. Type the following command to edit /etc/hostname using nano or vi text editor:
    sudo nano /etc/hostname
    Delete the old name and setup new name.
  2. Next Edit the /etc/hosts file:
    sudo nano /etc/hosts
    Replace any occurrence of the existing computer name with your new one.
  3. Reboot the system to changes take effect:
    sudo reboot

Sample outputs:

Gif 01: Ubuntu change the computer name demo

Display the current Ubuntu hostname

Simply type the following command:
$ hostname
Sample outputs:

Fig.01: Ubuntu Linux Show the hostname/computer name command

How to change the Ubuntu server hostname without a system restart?

Type the following commands:
$ sudo hostname new-server-name-here
Next edit the /etc/hostname file and update hostname:
$ sudo nano /etc/hostname
Finally, edit the /etc/hosts file and update the lines that reads your old-host-name:
$ sudo nano /etc/hosts
From: old-host-name
To: new-server-name-here
Save and close the file.

Ubuntu Linux Change Hostname Using hostnamectl

Systemd based Linux distro such as Ubuntu Linux 16.04 LTS and above can simply use the hostnamectl command to change hostname. To see current setting just type the following command:
$ hostnamectl
Sample outputs:

   Static hostname: nixcraft
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: 291893e6499e4d99891c3cf4b70a138b
           Boot ID: 9fda2365b77841649e40a141fde46537
  Operating System: Ubuntu 17.10
            Kernel: Linux 4.13.0-21-generic
      Architecture: x86-64

To change hostname from nixcraft to viveks-laptop, enter:
$ hostnamectl set-hostname viveks-laptop
$ hostnamectl


In this tutorial, you learned how to change hostname on Ubuntu Linux. For more information see this page here.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 10 comments so far... add one

CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
10 comments… add one
  • Marco Jan 27, 2016 @ 21:24

    Last file should be /etc/hosts instead of /etc/hostname

  • hash Jan 27, 2016 @ 21:24

    On any system using SystemD to check current:
    To change:
    hostnamectl set-hostname viveks-desktop

  • Louis Jan 29, 2016 @ 17:53

    Very useful. I didn’t konw about changing /etc/hosts.

  • Mengz You Jan 30, 2016 @ 3:40

    How about hostnamectl command?

  • ajay Jun 16, 2016 @ 7:32

    Dear Vivek,
    The above is good and solves to Change/Modify hostname/hosts..
    But the same is not holds for System Name
    Ex: vivek@ubuntu is changed as vivek@vivek_desktop or anything else..
    But It could not change “vivek” to newname

  • harish Dec 8, 2016 @ 4:41

    Hi it worked and Thank you very much for your help
    I have a Doubt about AWS EC2
    i created a new instance using existing instance and i want to change the hostname of new instance automatically but it showing its private ip address only for the new one
    please help me

    my code is
    aws ec2 create-tags –resources `aws ec2 run-instances –image-id –count 1 –instance-type t2.micro –key-name –user-data file://my-userdata.sh –subnet-id –security-group-ids |jq -r “.Instances[0].InstanceId”` –tags “Key=Name,Value=servername”

  • SHIV Jun 9, 2017 @ 2:38

    It’ll hold the value to the system, i mean persist the value even after restart if you add it to /etc/sysconfig/network

  • Hernan Nov 23, 2017 @ 13:05

    Thank you

    Worked as a charm

  • bagus Jul 24, 2020 @ 6:35

    your site always to the point
    like it!

  • thiyagi Feb 17, 2021 @ 9:43


Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Problem posting comment? Email me @ webmaster@cyberciti.biz