Linux change my hostname / computer system name

I am using Ubuntu Linux. How do I change my hostname or the machine name using the command line option?

You need to use hostname command or hostnamectrl command on modern Linux distro. It is used to either set or display the current host or domain name of the system. This name is used by many of the networking programs to identify the Linux based machine. The following instructions should work on both Debian and Ubuntu Linux systems or any Linux distro if you are using hostnamectrl.

How to display current hostname

Just type hostname command to print the name of the system on screen:
$ hostname
Sample outputs:


Change the Linux hostname

Set hostname to
# hostname
# hostname

Sample outputs:

Change hostname permanently on a Debian/Ubuntu Linux

You need to edit a file called /etc/hostname:
# vi /etc/hostname
Set new hostname:

Save and close the file. You need to reboot the system or run any one of the following command:
# /etc/init.d/ start
# invoke-rc.d start

Linux change hostname using hostnamectl command (systemd only)

Most modern Linux distribution comes with systemd. If you are using systemd based distro try hostnamectl command.

Query hostname with hostnamectl

Let us print out current hostname:
$ hostnamectl

Change hostname with hostnamectl

The syntax is as follows to set hostname to ‘viveks-laptop’:
$ hostnamectl set-hostname 'viveks-laptop'
Sample outputs:

Fig.01: hostnamectl in action on a Linux based system

Set the deployment environment description

ENVIRONMENT must be a single word without any control characters. One of the following is suggested: “development”, “integration”, “staging”, “production”. The syntax is:
$ hostnamectl set-deployment ENVIRONMENT
$ hostnamectl set-deployment production

How to set the location string for the system, if it is known

The syntax is:
$ hostnamectl set-location LOCATION
$ hostnamectl set-location "NYC Home"
$ hostnamectl set-location "DC 2, right rack, 2nd shelf"

A note about an RHEL (Red hat) / CentOS / Fedora Linux users

If you are using CentOS or Fedora or Redhat (RHEL) Linux, see this FAQ.

🐧 Please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on Linux, Open Source & DevOps via:
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
12 comments… add one
  • Michael Stelly Aug 2, 2009 @ 4:33

    Thanks. That’s exactly what I needed!

  • papa Feb 10, 2011 @ 19:34

    hostname —temp changing the hostname
    For Permanent
    vim /etc/hosts
    vim /etc/sysconfig/network

    • tong Nov 8, 2011 @ 4:23

      i did as you tell but can’t ping to hostname. My hostname is linuxserver, when i use command ping it appear connect: Network is unreachable.

  • root May 31, 2011 @ 14:21

    after changing, type “bash” and the prompt will change to the newest hostname your just changed.

  • name Jun 4, 2011 @ 0:00

    this just doesn’t work, it seems like it does but you wont be able to use “sudo” anymore…..

  • Deviram yaduwanshi Jun 7, 2011 @ 4:53

    hostname change command

  • Bob Dec 31, 2011 @ 10:14

    Thanks for pointing me to the right direction.
    As for me and perhaps ppl using certain distro, may have to edit the /etc/hosts file as well.
    sudo gedit /etc/hosts localhost newhostnamehere
    Look for the line that says: originalname
    and change it to the newhostname

    PS: until I fix this, sudo took a long time trying to but fail to “resolve” the name, before asking for password and the GUI ‘update’ taskbar icon and GUI package manager stop working.

    • arik1 Nov 30, 2014 @ 15:30

      The same thing happened to me, thanks for the comment

  • haji Mar 2, 2012 @ 15:08

    my need change host name in centos 5.7 ??

  • martin Apr 27, 2012 @ 11:16

    Really from my own point of view it is so amazing to use it please i need some e-book how to use it.

  • Kitana Aug 21, 2012 @ 23:11

    Setting up a CentOS 6 box today and was able to use the hostname command successfully without errors. Not sure why using the other FAQ is suggested.

  • ashkan Mar 10, 2015 @ 7:46

    so useful, thanks a lot!!!

Leave a Reply

Your email address will not be published. Required fields are marked *

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