Categories
How To

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

vi /etc/sysconfig/network

Set HOSTNAME=newhost.example.com, enter:

HOSTNAME=server2.nixcraft.com

HOSTNAME=server2.nixcraft.com

Save and close the file. Type the following command:

hostname server2.nixcraft.com
hostname

hostname server2.nixcraft.com
hostname

Now, proceed to step # 2.

Debian / Ubuntu: Change Hostname

Edit, /etc/hostname file, enter:

vi /etc/hostname

vi /etc/hostname

Delete old name and add new name:

server2.nixcraft.com

server2.nixcraft.com

Save and close the file. Type the following command:

hostname server2.nixcraft.com
hostname

hostname server2.nixcraft.com
hostname

Now, proceed to step # 2.

Slackware: Change Hostname

Edit /etc/HOSTNAME, enter:

vi /etc/HOSTNAME

vi /etc/HOSTNAME

Delete old name and add new one:

server2.nixcraft.com

server2.nixcraft.com

Save and close the file. Type the following command:

hostname server2.nixcraft.com
hostname

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

server2.nixcraft.com

Save and close the file. Type the following command:

hostname server2.nixcraft.com
hostname

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

vi /etc/hosts

Change all old hostname with newer one.

This entry is 4 of 15 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

Posted by: SXI ADMIN

The author is the creator of SXI LLC 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.

By SXI ADMIN

Skype: live:lauragold838239
WhatApp : +447520628210

Leave a Reply

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