etc/hosts in Ubuntu Linux

Introduction

The file etc/hosts is used to define the correspondence between IP addresses and domain names at the level of the local host.

It is like a DNS server that works only for one host

Example

On a relatively clean Ubuntu etc/hosts file looks roughly like this

cat /etc/hosts

127.0.0.1 localhost 127.0.1.1 LD-ANDREI # The following lines are desirable for IPv6 capable hosts ::1 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters

If there is a host with IP 10.1.55.55 on your network, the usual way to connect over SSH looks like this

ssh andrei@10.1.55.55

If there are many addresses, they are hard to remember. Maybe you are simply too lazy to type this IP address by hand.

Both of these problems are solved by an entry in etc/hosts

sudo vi /etc/hosts

10.1.55.55 srv.andrei.com srv

Now, to connect, it is enough to type

ssh andrei@srv.andrei.com

Or simply

ssh andrei@srv

If you set up passwordless access with ssh-copy-id for andrei@10.1.55.55, then it will also work for andrei@srv

Banner image

Article author: Andrei Olegovich

Banner image