How To: Setting and Configuring the Hostname in Linux

It a common step when setting up a new Linux machine or virtual machine that you will need to change the hostname if you’re installing the machine from existing image. Below are the steps necessary to change the name.

First check what is the existing hostname

You can use any of the following commands to check the current hostname:

    • uname -n
    • hostname -a
    • hostname -s
    • hostname -d
    • hostname -f
    • hostname

Editing /etc/hosts

DHCP

If you’re assigning your IP Address using a DHCP configuration you will need to open the file /etc/hosts with any text editor (vi). Configure the existing file so that it is similar to:

    • 127.0.0.1 name.intelliware.ca localhost.localdomain localhost name

Static

If you’re assigning your IP Address using a Static configuration you will to open the file /etc/hosts with any text editor (vi). Leave current configuration (127.0.0.1 localhost.localdomain localhost) and add a line similar to:

    • 192.168.3.150 name.intelliware.ca name

Therefore the file should look similar to:

    • 127.0.0.1 localhost.localdomain localhost
    • 192.168.3.150 name.intelliware.ca name

Setting the hostname using the command “hostname”

After you update the /etc/hosts file you can run the command “hostname” as following to set the new hostname:

    • hostname name.intelliware.ca

Check /etc/hostname

After you set the new hostname you want to verify the hostname. To do cat/less/more the file /etc/hostname. Some Linux environments do not have this file.

    • less /etc/hostname
    • name.intelliware.ca

Check /etc/sysconf/network

Verify in the file /etc/sysconfig/network that the hostname variable is set correctly. If not modify the file so that it is correct.

NETWORKING=yes
HOSTNAME=”name.intelliware.com”

Check /proc/sys/kernel/hostname

Verfiy in the file /proc/sys/kernel/hostname that the hostname is set correctly. If not change using the following command:

    • echo name.initelliware.ca > /proc/sys/kernel/hostname

Dynamic DNS – Updating the local DNS server with your host name and DHCP IP

For Red Hat Linux if you receive your IP address from a DHCP server, you may update the local DNS server by adding the following line to the correct ifcfg file in /etc/sysconfig/network-scripts, such as ifcfg-eth0 or ifcfg-eth1:

    • DHCP_HOSTNAME=”mybox.mydomain.com”

or if running Debian, edit /etc/network/interfaces as follows (adding the hostname line):

    • iface eth0 inet dhcp
    • hostname mybox.mydomain.com

It's only fair to share...
Share on FacebookGoogle+Tweet about this on TwitterShare on LinkedIn

Leave a Reply