37

I recently installed Visual Studio Code editor. I downloaded the 32bit .deb package. Installed and run normally, but when i tried to save a file in my project folder, it returns permission denied. So, i re-run code with

sudo code .

and it returns

It is recommended to start vscode as a normal user.
To run as root, you must specify an alternate user data directory with the --user-data-dir argument.

I looked for how to specify an alternate user data directory with the --user-data-dir argument but I didn't find much.

So, how do I run Visual Studio Code as root in Ubuntu 16.04?

edit: I would prefer a solution that avoids changing directory permissions.

edit2: project directory: /var/www/project (I'm using Apache with vhost)

  • 6
    why not just set the permissions on your project folder to allow it to save? – TheWanderer Jul 27 '16 at 1:04
  • 2
    I don't understand why you want to run code as root; this is potentially dangerous. You should figure out the real reasons why you can't save the file. – edwinksl Jul 27 '16 at 1:07
  • 1
    Where is your project folder – Nuwan Thisara Jul 27 '16 at 2:17
  • 1
    @Nuwan /var/www/project (im using Apache with vhost) – wdarking Jul 27 '16 at 2:22
  • 2
    Possible duplicate of How to avoid using sudo when working in /var/www? – David Foerster Sep 24 '16 at 23:45
52

It is very dangerous to run VScode as root (as it is any other application) however, should you absolutely need to:

You must specify the user data directory to use when running vs code:

sudo code --user-data-dir="~/.vscode-root"

Then from the window you can open your project folder

Again: This is not recommended. Have fun.

Update May 2018

For everyone asking why this isn't recommended, you clearly don't understand why sudo even exists. From this AskUbuntu Question:

It defeats the security model that's been in place for years. Applications are meant to be run with non-administrative security (or as mere mortals) so you have to elevate their privileges to modify the underlying system. For example, you wouldn't want that recent crash of Rhythmbox to wipe out your entire /usr directory due to a bug. Or that vulnerability that was just posted in ProFTPD to allow an attacker to gain a ROOT shell.

It's just good practice on any operating system to run your applications on a user level and leave administrative tasks to the root user, and only on a per-need basis.

I'm not saying that you should never use VSCode as root(though its pretty easy to avoid doing) I'm saying you should be careful when you want to do so and know the risks.

The absolute best thing to do to is make a copy of a restricted file, edit it, and copy it back when you're absolutely sure it's finished.

Also for editing files to which your user does not have permissions, I would now recommend Talha Junaid's answer which asks for permissions every single time you want to save a file. The prompt for root access did not exist when I wrote this answer.

  • 4
    why it is not recommended to run VScode as root? How to change something in hosts for example? I will need root permission is not? – zwitterion Dec 14 '16 at 16:59
  • 1
    Thanks it is working: now I have one VSCode window running as root and I want to start another vscode window as root, Is that possible? – RLaaa Mar 8 '17 at 22:08
  • 1
    Yes it's possible, open another terminal. – Lucas D.A.W. Dec 1 '17 at 10:26
  • 3
    @MarcoDufal Absolutely not. This is a horrible idea. Especially when working with OS files you should absolutely not set the permission to 777. that means that everything on the system regardless of who/what started it or what it's allowed to do has permission to modify that set of files and folders. At the very most you should "Own" the files so you are allowed to edit them with something closer to 755 permissions. – zawata May 23 '18 at 19:01
  • 3
    @zawata you are perfectly right! Please excuse a novice. Thanks for the heads up! – Marco Dufal May 23 '18 at 19:28
12

Just in case you want make changes to a single file I would prefer the following approach.

  • Run vs-code as regular user.
  • make changes to file
  • File -> Save or press Ctrl + S
  • vs-code will pop up error in right bottom corner saying

    Failed to save 'grub': Insufficient permissions. Select 'Retry as Admin' to retry as administrator

    enter image description here

  • Click on Retry as Admin. It will ask for administrator credentials

    enter image description here

  • Enter credentials and enjoy saving files with vs-code.

  • 1
    Is there any way to do this without having to do this every time for every single file? I have hundreds of files I need to make changes to and VS Code keeps asking for my password. I'm about ready to throw my laptop against the wall. – Joshua Kemmerer Aug 22 '18 at 15:33
  • Then you can follow the accepted answer given above. askubuntu.com/a/820205/367228 – Talha Junaid Aug 22 '18 at 16:51
4

You have to give permission to your html folder.

try it in your terminal.

sudo chmod -R 777 /var/www/html/

please make sure check the permission of existing project files and folder.

  • 9
    This is the most stupid advice ever ... restricted rights are in place to stop malicious processes to modify or destroy your system. Work on a copy of the restricted files and then copy them in place when done, or even better, make an install script to do it. You could also add your user to the group owning the directories and files. – Soren A Mar 15 at 9:04
  • cd to your project folder and run sudo chmod -R 777 <project_dir_name> and done – lilhamad Apr 5 at 12:01
  • 1
    cd to your project folder and run sudo chmod -R 777 <project_dir_name> and done – lilhamad Apr 5 at 12:04

protected by Community Sep 25 '16 at 6:49

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.