Linux Change Hostname

Cpanel/Whm License $3/mo Plesk License $10/mo Cloudlinux License $5/mo

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.

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.

Related posts

Zuckerberg to Study Crypto in Quest to Fix Facebook

SXI ADMIN

Zooko Wilcox Envisions ‘Ambitious’ Changes for Zcash Cryptocurrency

SXI ADMIN

ZombieChain Comes Alive: Can Ethereum Sidechains Save the Dapps?

SXI ADMIN

ZoKrates Seeks to Bring Best of Zcash to Ethereum with Devcon Debut

SXI ADMIN

Zk-Starks? New Take on Zcash Tech Could Power Truly Private Blockchains

SXI ADMIN

Zk-Snarks Everywhere: Ethereum Privacy Tech Hits Tipping Point

SXI ADMIN

ZipZap to Offer Cash-for-Bitcoin Service at 28,000 UK Locations

SXI ADMIN

ZipZap Resumes Cash-to-Bitcoin Services for UK Shoppers

SXI ADMIN

ZipZap Raises $1.1 Million to Grow Global Bitcoin Payments Network

SXI ADMIN

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More