Linux Change Hostname

How do I change hostname under a Linux operating system using the cli or over ssh based session?

You need to use the hostname or hostnamectl command. Both commands are used to either set or display the current host or domain name of the system. The hostname is used by many of the networking programs (such as sendmail, Apache servers) to identify the machine. Hence setting correct hostname is recommend.


Linux Change Hostname Using hostnamectl command

Almost all modern Linux distro comes with systemd an init system used in Linux distributions to bootstrap the user space and to manage system processes after booting.

Show your computer name

Simply type hostnamectl:
$ hostnamectl
Sample outputs:

   Static hostname: gfs03
         Icon name: computer-vm
           Chassis: vm
        Machine ID: beb217fbb4324b7d9959f78xxxxxxxxx
           Boot ID: 123a3aa710314175aec7c54yyyyyyyyy
    Virtualization: qemu
  Operating System: Ubuntu 16.04.3 LTS
            Kernel: Linux 4.10.0-40-generic
      Architecture: x86-64

Set or change your computer name

I am going to change gfs03 hostname to gfs-server-03:
$ hostnamectl set-hostname 'gfs-server-03'
Verify it:
$ hostnamectl
That is all. You just changed your server name permanently.
Linux Change Hostname

Linux Change Hostname Using hostname command

Older version of Linux distro used hostname command. Some modern distro without systemd or with systemd still use the following method. If possible use hostnamectl command. Let us see some examples.

Display Hostname

Type the following command:
$ hostname
Sample ouputs:

Change Hostname

You need to update two files:

  1. Linux Distribution specific file. Edit appropriate file as per your distribution as follows.
  2. /etc/hosts

Redhat / CentOS / Fedora: Change Hostname

Edit /etc/sysconfig/network, enter:

vi /etc/sysconfig/network

Set, enter:

Save and close the file. Type the following command:


Now, proceed to step # 2.

Debian / Ubuntu: Change Hostname

Edit, /etc/hostname file, enter:

vi /etc/hostname

Delete old name and add new name:

Save and close the file. Type the following command:


Now, proceed to step # 2.

Slackware: Change Hostname

Edit /etc/HOSTNAME, enter:

vi /etc/HOSTNAME

Delete old name and add new one:

Save and close the file. Type the following command:


Now, proceed to step # 2.

Suse / OpenSuse: Change Hostname

Edit /etc/HOSTNAME, enter:
Delete old name and add new one:

Save and close the file. Type the following command:


Now, proceed to step # 2.

Step #2: Update /etc/hosts

Now, you need to edit /etc/hosts file, enteR:

vi /etc/hosts

Change all old hostname with newer one.

This entry is 4 of 19 in the Linux/Unix Set hostname Tutorial series. Keep reading the rest of the series:
  1. Ubuntu Linux Change Hostname (computer name)
  2. Debian Linux: Change Hostname / Computer Name Permanently
  3. Linux change my hostname / computer system name
  4. Linux Change Hostname
  5. RHEL / Centos Linux 7: Change and Set Hostname Command
  6. OpenBSD Change Hostname
  7. CentOS Linux 5/6 Change Hostname Command
  8. Linux setting hostname and domain name of my server
  9. FreeBSD Change Hostname without reboot
  10. SuSe Linux Change Hostname without reboot
  11. How to set the hostname on Fedora Linux
  12. Ubuntu 18.04 LTS change hostname permanently
  13. Ubuntu set hostname permanently (computer name) command
  14. OpenSUSE Linux set hostname permanently (computer name) command
  15. RHEL 8 Linux set hostname permanently (computer name) command
  16. CentOS 8 Linux set hostname permanently (computer name) command
  17. Ubuntu 20.04 LTS set hostname permanently (computer name) command
  18. Set / Change FQDN on Ubuntu 20.04 LTS
  19. Alpine Linux - Setting system hostname
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallCentOS 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 VPNCentOS 8 Debian 10 Firewall Ubuntu 20.04

16 comments… add one
  • Iris Nov 13, 2012 @ 3:37

    echo “servername” > /proc/sys/kernel/hostname

  • Yeah Mar 16, 2013 @ 7:01

    None of this does anything to change the prompt which still reads: [root@localhost /] in CentOS6. After years of Linux mods, why is it still so damn complicated to change a single prompt ?!

    • Kimmo Mar 18, 2013 @ 7:45

      Sure it does, but you have to re-connect ssh to get it visible

  • smudger Mar 31, 2013 @ 16:34

    For my debian-based xubuntu

    after editing /etc/hostname with sudo

    >> cd /etc/init.d
    >> sudo start hostname

  • vishnu May 23, 2013 @ 15:31

    *In Fedora Simply follow this:

    # whereis hostname

    output: hostname: /bin/hostname /usr/bin/hostname /etc/hostname /usr/share/man/man1/hostname.1.gz /usr/share/man/man5/hostname.5.gz

    vi /etc/hostname

    • noname001 Aug 22, 2013 @ 12:18

      well you do not vi hostname file you pipe stuff there

      echo “hostname_1” >/etc/hostsname
      just to be clear

  • Pablosky Jan 29, 2014 @ 20:49

    Thank you for help on how to find the file. It was crazy for me because it wasn’t hostname “xP

  • sabarimurugan S Mar 24, 2014 @ 17:07

    I configure my rpm& yum server but the following error occurs please anyone tell me how to rectify the error

    file:///var/ftp/pup/repo/Server/repodata/repomd.xml: [Errno 5] OSError: [Errno 2] No such file or directory: ‘/var/ftp/pup/repo/Server/repodata/repomd.xml’
    Trying other mirror.
    Error: Cannot retrieve repository metadata (repomd.xml) for repository: Server. Please verify its path and try again

  • Logash Jul 16, 2014 @ 5:24

    How do i check which version of linux am running in and bit version?

    • mathews Oct 8, 2014 @ 15:35

      cat /proc/version – provides the bit information
      cat /etc/issue – Release

      The above Commands should help.

  • Sly Nov 19, 2014 @ 11:17

    you can also use :
    “sudo /bin/hostname” to view the hostname and “sudo /bin/hostname NEWNAME” to change the hostname to NEWNAME.

    • Sly Nov 19, 2014 @ 11:18

      the command i wrote is for Ubuntu

  • Mark Nov 25, 2014 @ 18:03

    On RHEL/Centos 7 I have had to use hostnamectl set-hostname

    All other ways had failed but this one worked great

  • K Thangamariappan Nov 26, 2014 @ 10:55

    For open SuSE/SLES 10

    /etc/hostname instead of /etc/HOSTNAME

  • pravinbhai s.Vaghmarya May 25, 2016 @ 9:16

    Address of hostname

  • pravinbhai s.Vaghmarya May 25, 2016 @ 9:17

    Change hostname addres

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.