How to Fix: Error Writing /Etc/hosts – Permission Denied in Ubuntu?
![How to Fix: Error Writing /Etc/hosts - Permission Denied in Ubuntu?](https://draculaservers.com/tutorials/wp-content/uploads/2024/06/Dracula-Servers-Thumbnail-16.png)
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.
Table of Contents
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.
Use the “sudo” Keyword in Command
As mentioned, you need root user access rights to save the “hosts” file changes. Otherwise, any changes you make will be temporary, and you will get an error prompt “Error 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.
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.
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.
Once you do that, you can open the “hosts” file in the nano editor without the warning prompt.
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.
Lastly, you can close the file by pressing “CTRL + X.”
Affordable VPS Hosting With Dracula Servers
Looking for reliable and budget-friendly Virtual Private Server (VPS) hosting? Look no further than Dracula Servers. Dracula Servers offers a range of VPS hosting plans tailored to meet diverse needs. With competitive pricing, robust performance, and a user-friendly interface, it’s an excellent choice for individuals and businesses alike.
Explore the Dracula Servers website to discover hosting solutions that align with your requirements and take your online presence to new heights with their affordable and efficient VPS hosting services.
Visit Dracula Servers and experience reliable VPS hosting without breaking the bank.
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.
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.
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
The reason that you get the permission denied error when you try to access the “hosts” file is that the file permissions for the normal users are set to “Read” only. 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
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
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 the “ls -al hosts” command again.
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.
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, then 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 not having adequate 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 shell or the sudo shell which will execute every command as the root user.
Lastly, you can change the file permission of the hosts files, but be careful with this method as it will leave the hosts file vulnerable.
Check out More Linux Tutorials Here!
Subscribe
Login
0 Comments
Oldest