Linux Change Hostname

last updated in Categories , , , , , , ,

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:

server.nixcraft.net.in

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 HOSTNAME=newhost.example.com, enter:

HOSTNAME=server2.nixcraft.com

Save and close the file. Type the following command:

hostname server2.nixcraft.com
hostname

Now, proceed to step # 2.

Debian / Ubuntu: Change Hostname

Edit, /etc/hostname file, enter:

vi /etc/hostname

Delete old name and add new name:

server2.nixcraft.com

Save and close the file. Type the following command:

hostname server2.nixcraft.com
hostname

Now, proceed to step # 2.

Slackware: Change Hostname

Edit /etc/HOSTNAME, enter:

vi /etc/HOSTNAME

Delete old name and add new one:

server2.nixcraft.com

Save and close the file. Type the following command:

hostname server2.nixcraft.com
hostname

Now, proceed to step # 2.

Suse / OpenSuse: Change Hostname

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

server2.nixcraft.com

Save and close the file. Type the following command:

hostname server2.nixcraft.com
hostname

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.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.

Share this on (or read 16 comments/add one below):

16 comment

  1. 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 ?!

  2. For my debian-based xubuntu

    after editing /etc/hostname with sudo

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

  3. *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

    *edit
    vi /etc/hostname

    1. well you do not vi hostname file you pipe stuff there

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

  4. 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

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

      The above Commands should help.

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

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

    All other ways had failed but this one worked great

    Have a question? Post it on our forum!