ElectronicsGadgetITScienceTechnology

How to set up a Linux hosts file

Oscar Wong/Moment/Getty Images

Linux is a very powerful operating system and also one of the most flexible operating systems on the market. That flexibility has always been a big selling point of the OS. Users who don’t like how Linux works can bend it to their needs.

One very simple example of this is the Linux hosts file. Please understand that the hosts file is not specific to Linux only. Windows and MacOS also use hosts files. In fact, MacOS hosts files are almost identical to Linux hosts files.

What is a hosts file?

A hosts file makes it easy to map IP addresses to domain names. Sound familiar? Because that’s exactly what DNS does today. But in the days of ARPANET, there was no DNS, so we needed a way to map these addresses. This is where the hosts file comes into play. Given that DNS takes care of that, why do we need a hosts file?

The best example I can give is a LAN-based network setup with servers or desktops that you want to be able to access without having to enter the full IP address. You may have a server on your LAN that you access regularly.

ssh jack@192.168.1.100

You can map that IP address to your hosts file by simply typing:

ssh jack@invoiceplane

It’s not less typing, but it certainly means you don’t have to remember that server’s IP address. Use nicknames instead. This is very useful when working with many servers on a LAN. You can also use these nicknames in your web browser. For example, if you installed his Invoiceplan (an invoicing solution) on a server in your LAN, and the document root for that service is /var/www/html/invoiceplane, you can use the server’s IP address as the nickname for Invoice and your browser’s address Type “invoice/invoiceplane” in the bar to open the service.

Also: 5 Best Linux Distros For Beginners: You Can Do This

How to configure the Linux hosts file

How do you set this up? It’s very simple. Let me show you how.

requirement

All you need is a running instance of Linux and a few IP addresses to map. That’s all. Make some hosts magical.

Log in to your Linux machine and open a terminal window. To open the hosts file for editing, enter the following command: sudo nano /ets/hosts.

Each line in the hosts file is a unique IP address mapping, arranged in the following format:

IP Address Nickname Domain Name

For example (to stick with the Invoiceplane example), you can map the IP address 192.168.1.11 to the nickname Invoice with a domain of invoiceplane.lan. That entry looks like this (appended to the end of the file):

192.168.1.11 invoice invoiceplane.lan

With this entry in your hosts file, you can access that server in three ways:

  • IP address (such as ssh 192.168.1.11)
  • Nickname (e.g. ssh invoice)
  • Domain name (such as ssh invoiceplane.lan)

One caveat with domain name entries is that you can’t access the server using that entry in a web browser (it’s limited to IP addresses and nicknames).

Also: These are the best Linux distributions for programming.

After adding the required entries to the host, save and close with the Ctrl+X key combination. After saving the file, the network subsystem picks up the changes immediately, so you don’t need to reboot the OS.

Also: Cool things you can do on your Linux desktop that you can’t do on MacOS or Windows

If you’re new to Linux, you probably don’t need to worry about the hosts file at first. Ultimately, though, the deeper you get into using Linux, the more likely you are to encounter many situations where a hosts file is very necessary.

https://www.zdnet.com/article/how-to-set-up-linux-hosts-file/#ftag=RSSbaffb68 How to set up a Linux hosts file

Back to top button