≡ Menu

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’. How can I remove tom and set it to jerry on Ubuntu Linux? How do I change the Ubuntu computer name from ‘ubuntu’ to ‘AvlinStar’?

You can use the hostname command 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 command.

Display the current Ubuntu hostname

Simply type the following command:
$ hostname
Sample outputs:

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

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

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

Gif 01: Ubuntu change the computer name demo

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.

Share this tutorial on:

Like this? Follow us on Twitter OR support us by using Patreon

{ 6 comments… add one }
  • Marco January 27, 2016, 9:24 pm

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

  • hash January 27, 2016, 9:24 pm

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

  • Louis January 29, 2016, 5:53 pm

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

  • Mengz You January 30, 2016, 3:40 am

    How about hostnamectl command?

  • ajay June 16, 2016, 7:32 am

    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 December 8, 2016, 4:41 am

    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”

Security: Are you a robot or human?

Leave a Comment

You can use these HTML tags and attributes: <strong> <em> <pre> <code> <a href="" title="">

   Tagged with: , ,