Ease Linux Networking With /etc/hosts

Jack Wallen
Jan 15, 2009
Updated • Jan 14, 2013
Linux
|
5

How many times have you gone to your Linux box to administer your network and found yourself developing carpal tunnel from typing ssh -v -l jlwallen 192.168.1.10 -X? Or how taxing to your memory is it when you have to remember an IP address assigned to that Linux file server that never needs babysitting save for when you do an upgrade - how do you remember that one IP address when you have a network that consists of hundreds of machines?

Thankfully the Linux operating system contains its own system for mapping IP addresses to names. So instead of having to type (or remember) an IP address for that file server at 192.168.1.10 you can map it to a name like "files".

The hosts file is actually a smaller part to a larger system that includes a deny and an allow file. But this article will focus on the /etc/hosts file. This file most be edited by the root user and can be edited in a standard text editor like Nano.

Once you have the file open you will instantly understand the format. The default entry for /etc/hosts looks like this:

127.0.0.1               localhost

This is the loopback entry and must be left alone. This is a special entry responsible for software local addressing. Do not delete or change this entry. But this entry illustrates how this file works. The "official" structure of an /etc/hosts entry looks like this:

IP Address          FQDN     NAME

From our loopback entry you can see that the FDQN is not required. If the machine does have a fully qualified domain name use it, if not - don't. Let's use the example we started early on. We'll create an entry for the file server.

192.168.1.10          files

Now instead of typing ssh -l -v jlwallen 192.168.1.10 -X you can enter ssh -v -l jlwallen files -X and you're on your way. See how simple mapping addresses can be in Linux? And don't hesitate to fill this file up. If you have a network with hundreds of servers, add an entry for each machine to make your network much easier to navigate.

Advertisement

Previous Post: «
Next Post: «

Comments

  1. Syahid A. said on January 25, 2009 at 3:04 pm
    Reply

    Cool trick one can learn to easily connect between multiple PC’s, in Linux and Windows too. Both files are called “hosts” files.

  2. Charles said on January 16, 2009 at 1:19 pm
    Reply

    This is not a linux feature as you seem to claim, you can do this from every operating system I know of, yes, even windows. The file lives in C:\Windows\system32\drivers\etc.

  3. Anonymous said on January 16, 2009 at 7:53 am
    Reply

    Hey Jack, Great tip that editor http://www.nano-editor.org/. I do not have a Linux network with hundreds of machines, i have only one machine, but good to rememeber for hopeful, the not to far future. Thanks again for yet one outher christal clear artikel.

  4. hybrid-kernel said on January 16, 2009 at 4:50 am
    Reply

    For my one and only server I set up a passwordless (I know) login with the command “sshs” (ssh server). I just use the server for playing around, so security isnt a biggie.

    btw sshs=”ssh -i /path/to/rsync.key user@ip”

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.