How to Fix Ubuntu Error Writing /Etc/hosts Permission Denied

How to Fix: Error Writing /Etc/hosts - Permission Denied in Ubuntu? | Linuxjava.com

If you like the following content, please share it, so we can grow and improve the site for you

Are you trying to make changes in your Ubuntu Operating System’s “hosts” file but cannot save those changes?

The “hosts” file contains the different IPs and their respective host names and addresses, and its main job is to convert or translate hostnames to their respective IP addresses. There are various scenarios where the user would want to change this file. However, this file has severe access restrictions, as any unwanted change to this file can lead to various vulnerabilities.

This guide will explain why a user can get the “Error Writing – Permission Denied” prompt when saving the changes inside the “hosts” file and its solution.

How to Fix: Error Writing /Etc/hosts – Permission Denied in Ubuntu?

The system administrators can only access the “hosts” files, known as the Root users or sudo users in Ubuntu. However, reading the file can be done by a normal user as well.

To access and make changes to the “Hosts” file located at “/etc/hosts” in Ubuntu, you require root user or sudo privileges. Therefore, remember to open the “hosts” file using the “sudo” keyword at the start of the command or by using the sudo shell. However, you can choose any editor to make changes.

1. Use the “sudo” Keyword in Command.

2. Use the Sudo Shell (Root Shell)

3. Change the File Permissions of the “hosts” File.

Use the “sudo” Keyword in Command.

As mentioned, you need root user access rights to save thehostsfile changes. Otherwise, any changes you make will be temporary, and you will get an error promptError Writing – Permission Denied.”

To demonstrate this, you can use the nano editor to open the “hosts” file placed inside the “etc” directory by running the following command inside the terminal.

 nano /etc/hosts

Once you execute the command, you will observe that the nano editor shows the following prompt, even before trying to write changes to the file.

How to Fix: Error Writing /Etc/hosts - Permission Denied in Ubuntu? | Linuxjava.com

Make any changes to the file and then press the key combination, “CTRL + O,” to save the save changes, and you will get the following error.

How to Fix: Error Writing /Etc/hosts - Permission Denied in Ubuntu? | Linuxjava.com

To avoid this, exit the nano editor by pressing the “CTRL + X.” Then, use the “sudo” keyword at the start of the command to open the nano editor with sudo rights.

sudo nano /etc/hosts

However, you will be prompted by the terminal to provide the password for the sudo user. Type in the password and press the “Enter” key to continue.

How to Fix: Error Writing /Etc/hosts - Permission Denied in Ubuntu? | Linuxjava.com

Once you do that, you can open the “hosts” file in the nano editor without the warning prompt.

How to Fix: Error Writing /Etc/hosts - Permission Denied in Ubuntu? | Linuxjava.com

Add the lines that you want to define the hostnames and their IP addresses, and press “CTRL + O” to save the file. You will also see a progress prompt.

How to Fix: Error Writing /Etc/hosts - Permission Denied in Ubuntu? | Linuxjava.com

Lastly, you can close the file by pressing “CTRL + X.”

Use the Sudo Shell (Root Shell)

Another method of changing the host file without encountering the permission denied error is using the sudo or root shell. The root shell executes every command with sudo privileges even when the keyword “sudo” has not been explicitly used in the command.

To use the root shell, open up a new terminal session on your Ubuntu system and execute the following command.

sudo -i

After that, you will be prompted to provide the password for the root user. Therefore, type in the password and continue. Once you do that, you will see that the user’s name changes to “root” in the terminal.

How to Fix: Error Writing /Etc/hosts - Permission Denied in Ubuntu? | Linuxjava.com

Next, open the “/etc/hosts” files with the nano editor by running the following command inside your root shell.

nano /etc/hosts

Your “hosts” files will be readable in the nano editor and will not show warning prompts because the command was executed as the root user. Make the changes, and press “CTRL + O” to write the changes in the file.

How to Fix: Error Writing /Etc/hosts - Permission Denied in Ubuntu? | Linuxjava.com

Note: Whenever you use the root shell (sudo shell), each command is executed with the highest privileges, so you will not get any warning or prompts while executing the commands. Therefore, you can run commands that can crash your system files.

Change the File Permissions of the “hosts” File.

You get the permission denied error when you try to access the “hosts” file because the permissions for normal users are set to “Readonly. If you want to access the “hosts” file as a normal user regularly and make changes to it, then you can change this file permission.

To do this, open up a new terminal and then type in the following command to change the “pwd” to the “etc” directory.

cd /etc/

Once you are inside the “etc” directory, you can view the file permissions of all the files by running the following command.

ls -al

How to Fix: Error Writing /Etc/hosts - Permission Denied in Ubuntu? | Linuxjava.com

As you can see, the list is quite long. You can easily filter out to display the file permissions of only the “hosts” file by running the following command.

ls -al hosts

How to Fix: Error Writing /Etc/hosts - Permission Denied in Ubuntu? | Linuxjava.com

The output shows that the file permissions are set to “rw” for root and only “r” for normal users. Next, run the following command to grant the users write access over the “hosts” file.

sudo chmod 666 hosts

After that, verify that the permissions are changed by running thels -al hostscommand again.

How to Fix: Error Writing /Etc/hosts - Permission Denied in Ubuntu? | Linuxjava.com

Once you are done, open up a new terminal session to ensure the terminal has no access to the sudo password. Open the host file in the nano editor and observe that you will no longer get the warning prompt.

How to Fix: Error Writing /Etc/hosts - Permission Denied in Ubuntu? | Linuxjava.com

Now, you can add the hostnames and their respective IP addresses and write the changes into the file system without encountering the permission denied error.

Note: Changing the File permissions of the system files is not very good practice as it makes the system files vulnerable. Therefore, it is advised that you use the “sudo” keyword whenever you want to make permanent changes to the “hosts” file.

Conclusion

Changing the “hosts” can be necessary in many scenarios, but it would require you to have the root privileges. The “hosts” file is in the “etc” directory, which contains multiple system files that configure various aspects of the Ubuntu File system.

If you want to view the “hosts” file, you can use the nano editor or the cat command, and you will not encounter any errors. But, if you make some changes in the “hosts” file and then try to save those changes permanently, you will get the permission denied error due to insufficient access rights.

To fix this, use the keyword “sudo” at the start of the command to run the command as a root user. Otherwise, you can shift over to the root or sudo shell, which will execute every command as the root user.

Lastly, you can change the file permission of the host file, but be careful with this method as it will leave the host file vulnerable.

Don`t copy text!
Scroll to Top