This is one of the common problems you may face if you are using Visual Studio Code in Linux and that is, VS Code asking about the
root permission every time for saving a file.
Yesterday I was working on one of my web projects and the problem was, whenever I tried to save my file I was getting an
Insufficient permissions error.
Honestly, this is not an error, it's just VS Code needs
sudo privilege to save the file and you can easily give it by clicking on the
Retry as Sudo... button. But, here is a catch, it's not going to ask about the
sudo privileges for the first or one time, every time when you have to save the file, it will ask the
sudo password. And believe me, it's frustrating as hell especially, if you are working on some kind of web project where you have to save the file frequently in order to track your changes or if you habituated in saving the file after the few lines of typing.
After spending a few hours and doing a couple of Google search, I have found some solutions which I am going to discuss. If you Google this topic, you will find the same solutions as well but, I would not recommend every solution which you find in the first place and you have to know why it's not.
Running VS Code with the
root privileges ( not recommended )
sudo code --user-data-dir="~/.vscode-root"
If you run this command from the terminal, then it will forcefully start the VS Code with the
root privileges, but running a user-level application, with
root privileges is potentially dangerous because if some vulnerability found in VS Code, that might affect your system. VS Code also warns you about this.
And even more dangerous thing is, the user gets the full ability to modify any system file from VS Code without verifying the
root password and if the user doesn't know what he/she is actually doing, then there is a high chance that they might bring some serious problem, even they can break the system.
chmod -R 777 command ( It depends )
sudo chmod -R 777 your_project_directory_location
sudo chmod -R 777 /var/opt/MY_PROJECT
If you use this command, then not only you, other users on the machine ( If you have more than one user account ) also able to see, modify your project. Because the
chmod 777 command gives full control over the file/directory to everyone. So, only use this command if you want to share your project with others. Otherwise, it makes no sense to give full control of your personal project to everyone.
chown command ( Recommended)
sudo chown your_username your_project_directory
let's say I have a directory called
project1 in my
/var/opt location and my user name is
sudo chown mir project1
However, if you already created some file before applying
chown, don't forget to change their permission also
sudo chown your_username your_project_directory/your_file_name
sudo chown mir project1/file1.txt
It's recommended because it neither creates any security issue nor gives unnecessary access to your projects to the others. It solves your problem by keeping your's project up to you. Because using the
chown command you have limited the full access of your project directory within your user account.
And for this reason, this is the best solution so far
Thanks for reading & I would like to know what's your opinion on this...