How to edit your host file on Linux
To test how a web server responds to a request with a certain hostname, editing your host file is often the easiest way that doesn’t require you to actually change the domain’s pointer in the dns. This is great for testing a new server without making the change for everyone, or if you are developing something and don’t want to buy a domain yet.
Start by opening your computers host-file in your text editor. The file is always located in /etc/hosts on Linux. This has to be done as root, and if you have sudo, can simply be done with this command:
$ sudoedit /etc/hosts
If this command ends up opening vi or vim, and you aren’t sure how to edit in it, check out our simple guide on the basics of vim.
The layout of the file is pretty simple. For every new line, just start with the IP address you want the host name to point to, add a space and follow it by the host name:
192.168.1.37 www.example.com
192.168.1.37 example.com
Save the file when you’ve added the domains you want. Now all future requests your computer makes to those domains will go to the IP you’ve chosen.
To ignore a line without deleting it, just add a pound-sign before it:
#192.168.1.37 www.example.com
192.168.1.37 example.com
That’s all there is to editing the hosts-file!