How to Edit the Hosts File on Ubuntu and Block a Website

The hosts file is an OS based plain text file that maps hostnames to IP addresses.It contains lines of IP address in the first field and host names in the second field. Each field is separated by tab space. you can also add a comment line by adding a hash (#) in the front.

Edit the hosts file on Ubuntu:

Open your Terminal app by pressing Ctrl+Alt+T key combination. If you want to take a backup of your hosts file , typing the following command in your terminal app.

sudo cp /etc/hosts /etc/hosts.old

Type your Ubuntu password and press enter. Then run the below command in terminal.

sudo gedit /etc/hosts

or

sudo -E gedit /etc/hosts

It will open the hosts file in the gedit text editor.

Also Read:  Folio Markdown Note Taking Application

It you you want to block www.google.com, then add below line in your hosts file.

127.0.0.1    www.google.com

Save the file and close it. You can add as many sites as you want.

If the blocking is not working correctly in Google chrome browser, then just disable  the “Use a web service to help resolve navigation errors” option in the chrome settings.

If you liked this article, please subscribe to our YouTube Channel. You can also stay connected with us on X (Twitter) and Facebook.



This Post Has 2 Comments

  1. Spas Spasov

    Please change the command line to: `sudo -H gedit`

Leave a Reply