6

I am using VS Code (1.30.02) and Ubuntu 18.04. When I try to save any changes in VS Code, I get this error:

Failed to save 'SomeFileName.js': Insufficient permissions. Select 'Retry as Sudo' to retry as superuser.

I have the same issue if I try to create a new file:

Permission denied writing to file (file:///path/to/new/file/newfile.js)

I am making these changes or trying to create new files in my own directory. I am new to Ubuntu, so I apologize if this is a stupid question, but I am not sure what I am doing wrong. What is the issue?

Jason
  • 173
  • 1
  • 2
  • 7
  • I don't know or use VS Code but generally, you create and save files in your home directory (/home/username). If saving files in your home directory is not possible, something is wrong on another level. Saving files elsewhere should not be possible without some modification first and is not encouraged. – Jos Jan 31 '19 at 16:31
  • 1
    I am logged in and saving inside my home directory. – Jason Jan 31 '19 at 18:27

3 Answers3

7

It looks like you somehow changed file ownerships in your home directory.

One way to correct this without endangering your system is

sudo chown -c -R $USER:$USER $HOME

Explanation:

  • chown: change the ownership of files/directories
  • -c: report all changes
  • -R: do this recursively (for all files/directories beneath the given one)
  • $USER:$USER: change the owner and the group that owns the the entry to the user that issues the command (sudo preserves the values)
  • $HOME: do this with your home directory

You can test those environment variables with the following commands

echo $USER
sudo echo $USER
echo $HOME
sudo echo $HOME
guntbert
  • 13,134
  • What if I dont want to change the folder permission because the owner is www-data and I need to serve the files on my apache? My user is in the group www-data and I can change files in the project folder via terminal but cannot change them via VS code. Any idea? – Erik Čerpnjak Oct 12 '21 at 16:05
  • [SOLVED] I had to log out of linux and log back in in order to start working (prerequisite: the user has to be in the www-data group) – Erik Čerpnjak Oct 12 '21 at 16:14
1

If you want to make changes and create new files with VsCode without changing the whole home directory ownerships.

You can just change the ownerships of your project folder.

sudo chown -c -R $USER:$USER (project folder)
-1

Navigate to the parent folder of the file in your terminal and enter the following code:

sudo chmod -R 777 filename
zx485
  • 2,426
  • 2
    Please don't suggest 777 permissions blindly. – guntbert Jun 14 '19 at 21:05
  • I had a similar issue recently, it was annoying to always click the "Retry as Sudo", whenever I want to save my work so I tried editing the permission of the folder containing the file to 777 and it worked for me. I thought maybe it could help him solve his also. – Mbiplang Ardel Jun 15 '19 at 07:08