Set Hostname on Linux

Types of hostnames

The hostname can be configured as follows

  1. Static host name assigned by sysadmin. For example, “server1”, “wwwbox2”, or “server42.cyberciti.biz”.
  2. Transient/dynamic host name assigned by DHCP or mDNS server at run time.
  3. Pretty host name assigned by sysadmin/end-users and it is a free-form UTF8 host name for presentation to the user. For example, “Vivek’s netbook”.

hostnamectl command

Let us see how to use the hostnamectl command.

How do I see the host names?

$ hostnamectl
## OR ##
$ hostnamectl status

Sample outputs:

   Static hostname: centos-7-rc
         Icon name: computer
           Chassis: n/a
        Machine ID: b5470b10ccfd49ed8e4a3b0e953a53c3
           Boot ID: f79de79e2dac4670bddfe528e826b61f
    Virtualization: oracle
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-229.1.2.el7.x86_64
      Architecture: x86_64

How do I set the host name?

The syntax is:

# hostnamectl set-hostname Your-New-Host-Name-Here
# hostnamectl set-hostname "Your New Host Name Here" --pretty
# hostnamectl set-hostname Your-New-Host-Name-Here --static
# hostnamectl set-hostname Your-New-Host-Name-Here --transient

To set host name to “R2-D2”, enter:

# hostnamectl set-hostname R2-D2

To set static host name to “server1.cyberciti.biz”, enter:

# hostnamectl set-hostname server1.cyberciti.biz --static

To set pretty host name to “Senator Padmé Amidala’s Laptop”, enter:

# hostnamectl set-hostname "Senator Padmé Amidala's Laptop" --pretty

To verify new settings, enter:

# hostnamectl status

Sample outputs:

   Static hostname: server1.cyberciti.biz
   Pretty hostname: Senator Padmé Amidala's Laptop
Transient hostname: r2-d2
         Icon name: computer
           Chassis: n/a
        Machine ID: b5470b10ccfd49ed8e4a3b0e953a53c3
           Boot ID: f79de79e2dac4670bddfe528e826b61f
    Virtualization: oracle
  Operating System: CentOS Linux 7 (Core)
       CPE OS Name: cpe:/o:centos:centos:7
            Kernel: Linux 3.10.0-229.1.2.el7.x86_64
      Architecture: x86_64

How do I delete a particular host name?

The syntax is:

# hostnamectl set-hostname ""
# hostnamectl set-hostname "" --static
# hostnamectl set-hostname "" --pretty

How do I change host name remotely?

Use any one of the following syntax:

# ssh root@server-ip-here hostnamectl set-hostname server1

OR set server1 as host name on a remote server called 192.168.1.42 using ssh:

# hostnamectl set-hostname server1 -H root@192.168.1.42

 

 

 

Reference

 

Note:

  • hostnamectl exists on RHEL/Centos7, Debian8 (validated)

 

Some topics in SSH

Login SSH without typing password every time

  1. create public and private keys on local-host (debian)
    jason@debian$ ssh-keygen
    Generating public/private rsa key pair.
    Enter file in which to save the key (/home/jason/.ssh/id_rsa):[Enter key]
    Enter passphrase (empty for no passphrase): [Press enter key]
    Enter same passphrase again: [Pess enter key]
    Your identification has been saved in /home/jason/.ssh/id_rsa.
    Your public key has been saved in /home/jason/.ssh/id_rsa.pub.
    The key fingerprint is:
    33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 jason@debian
    

    This command creates two RSA key files, id_rsa for private key, and id_rsa.pub for public key, in the directory $HOME/.ssh.

  2. copy the public key file to remote-host (fedora) using ssh-copy-id
    jason@debian$ ssh-copy-id -i ~/.ssh/id_rsa.pub fedora
    jason@fedora's password:
    jason@fedora$
    

    Now try logging into the remote machine (fedora)

    jason@debina$ ssh fedora
    Last login: Thu Feb 16 11:45:20 2017 from debian
    jason@fedora$
    

    Notice that ssh does not ask for password. Check the authorized_key to make sure we haven’t added extra keys that you weren’t expecting.

    jason@fedora$ cat .ssh/authorized_keys
    

ref – 3 Steps to Perform SSH Login Without Password Using ssh-keygen & ssh-copy-id
In case ssh-copy-id is not available (for example, on mingw), as an alternated solution, you can manually copy the public key file (id_rsa.pub) to the remote host, and add the content as a record into $HOME/.ssh/authorized_keys. The file is a text file, so if it doesn’t exist, you can create it with a text editor. Don’t forget to change the mode to 644, otherwise sshd won’t use it.

Define host aliases in SSH config

Following lines in ~/.ssh/config define two aliases for the host staging.example.com

host foo bar # aliases separated by whites
  hostname staging.example.com

ref – Defining host aliases in your SSH config