Ease Linux Networking With /etc/hosts
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
Cool trick one can learn to easily connect between multiple PC’s, in Linux and Windows too. Both files are called “hosts” files.
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.
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.
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”