Set your hostname in Linux

If you are an administrator then you know what a hostname is. If you are not an administrator then you should know the hostname of a computer is the name which a network attached device is known. Unless you have manually changed your hostname, or set it up during installation, your hostname will most likely be "localhost.localdomain".

Hostnames are most often used for administrative habits. In other words hostnames will show up in some scanning applications, can be used for name to IP address resolution, and much more. But to make this useful you will have to manually set your hostname on your Linux machine. This article will show you just how to do that.

Find your hostname

The most common means to find your host name is to open up a terminal window and enter the command:

hostname

If you have not set your hostname, more than likely you will see returned:

localhost.localdomain

A quick and easy way to spot if you have not changed your hostname is to check when you open up your terminal window. At your bash prompt you will see something like:



[[email protected] ~]$

As you can see a portion of the hostname (everything preceding the first ".") will be used in your prompt. In the example you see above you see "localhost" which should tell you the hostname has not been changed.

Temporarily changing your hostname

You can temporarily change your hostname by issuing a single command. You have to issue this command either using sudo or as the root user. To make this change you will use the same command you used to find out your hostname, only you will include the new hostname to the command. Say you want to change your hostname to "willow". To make this change temporarily issue the command:

Read also:  Wireshark 2.4 released

hostname willow

Now issue the command hostname which should report back:

willow

This change will last until you reboot your machine.

Permanently changing your hostname

How you permanently change your hostname will depending upon which distribution you use. We'll examine making this change on both a Ubuntu system and a Fedora system. First Ubuntu.

On a Ubuntu system there is a file called /etc/hostname. The contents of this file will have a single line containing the hostname of your machine. Open up this file in your favorite editor, delete the default hostname, add your desired hostname, and save the file. To make this chang take effect issue the command:

/etc/init.d/hostname.sh start

This hostname will remain intact upon reboot.

Now to change your hostname in Fedora. This is taken care of in the /etc/sysconfig/network file. The default contents of this file will look like:

NETWORKING=yes
HOSTNAME=localhost.localdomain

Erase the "localhost.localdomain" portion and change that to reflect the hostname you want. Once you have made this change save the file. Once you have made this change issue the following command (as the root user):

/etc/rc.d/rc.sysinit

For the change to take effect.

Final thoughts

With your hostname changed applications like Lanmap will now be easier to use because machines will have unique names. This change will also make administering your systems easier because you will have set machine hostnames to reflect either the user of the machine or the machines' job.

Please share this article

Facebooktwittergoogle_plusredditlinkedinmail



Responses to Set your hostname in Linux

  1. thedude July 6, 2009 at 10:04 pm #

    lmgtfy?

  2. Dotan Cohen July 7, 2009 at 6:20 pm #

    If I change the hostname of a machine, will other machines on the LAN (DHCP handed out by a router) be able to access that machine by hostname instead of IP address automatically, or will I have to make other configuration changes so that other machines will be able to associate the new name with that machine?

    Thanks.

  3. Jack Wallen July 8, 2009 at 9:48 pm #

    Dotan: If the machine is a part of a domain it should appear without having to map it on the other machines. if it is not a part of a domain you will have to map it. for this to really work effectively, you might have to employ the help of Samba. if you are using DHCP you can always set your hostname in the dhclient.conf file. you can add a line like:

    send host-name "HOSTNAME"

    which should make sure your router (or DHCP server) knows your hostname.

  4. Dotan Cohen July 20, 2009 at 5:22 pm #

    Thanks, Jack. I assume that I would have to add the host-name line to each machine on the network? That doesn't help because the IP address changes each time I connect / disconnect from the network (laptop computer). I do not want to add a Samba server, I am surprised that there is no native way to do this in Linux alone.

  5. Hubba August 14, 2009 at 5:07 am #

    Quick Ubuntu way:

    sudo echo 'newname' > /etc/hostname

  6. Just Some Guy October 30, 2011 at 7:39 am #

    This:

    /etc/rc.d/rc.sysinit

    Should not be done on a FC/RHEL system running at any run level above single user.

    --JSG

    • Jim August 22, 2012 at 8:00 pm #

      Don't do a sysinit !!!!

Leave a Reply