Blog

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.

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

French Government Outlines New Regulations for Bitcoin Market Transparency

Viking Coin

Regulators in Maryland, Nevada and Canada Weigh in on Bitcoin Debate

Viking Coin

Colorado Senators File Bill to Exempt Cryptos from Securities Laws

Viking Coin

Ether Up from 100-Day Low, But Bounce Back Lacks Substance

Viking Coin

LINE’s Crypto Exchange Lists Its Own Token Against Bitcoin, Ether

Viking Coin

Canadian Realtor Accepts Bitcoin Deposits for Residential Properties

Viking Coin

Korea’s Exchange Hacks: What the Country’s Crypto Scene Is Saying

Viking Coin

See Filesystems In Linux

Viking Coin

Why Bitcoin Fungibility is Essential

Viking Coin