25

I have Ubuntu 16.04 and I have to download the C# extension for Visual Studio Code, once I installed it, It doesn't make effect. Then, vscode give me and advice that I should open vscode with admin privileges to make effect of the extensions installed, so I wrote on my terminal:

sudo code .

but it doesn't work, the terminal throws me:

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.

but I don't know how to specify an alternate user data directory. I was searching how to do that in visual studio code docs but there is not a reference for this issue. If you know how to open with admin privileges in linux please help me.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Anargu
  • 517
  • 2
  • 8
  • 17
  • When running as root this worked for me. ```code --user-data-dir="~/.vscode-root" --no-sandbox``` You can also add this to your .bashrc, .zshrc etc. to simplify it. Then you can just run vscode command from the terminal: ```alias vscode='code --user-data-dir="~/.vscode-root" --no-sandbox'``` – Michael Jul 01 '21 at 11:55

8 Answers8

31

To run with superuser:

$ sudo code --user-data-dir=~/root

By the way you will be able to run without setting params in the upcoming patch 1.5.0.

Dauren Akilbekov
  • 4,627
  • 2
  • 27
  • 32
13

You can achieve this in either of following ways :

  • Run vscode as superuser:
    $ sudo code --user-data-dir=~/root
    This will open vscode without your previous settings (fresh) with superuser privileges and you can install your extensions.

OR

  • Follow these steps :
  1. sudo chown -R <user> <path_to_your_vscode_installation_directory>
  2. Hit follwing in terminal
    to check the current user on your machine :
    whoami
    for e.g. john
  3. You can find path of vscode directory using following command :
    whereis code
    e.g. in my case path is : /usr/share/code
  4. Now run :
    sudo chown -R john /usr/share/code
    This will run vscode with admin privilege
  5. Now install your extensions
  6. After reset owner back to root
    sudo chown -R root /usr/share/code
Shubham Yadav
  • 311
  • 2
  • 6
6

My simple solution is:

sudo code --verbose --user-data-dir --no-sandbox

(not as root but as sodoer user)

Payedimaunt
  • 1,014
  • 2
  • 10
  • 23
  • 1
    If working with docker containers it takes away priviledges from a user that docker-compose is set up to. In that case adding yourself to a group with rights is a solution. – Katarzyna Feb 04 '23 at 01:08
2

You can press Shift+Cmd+P in visual studio code and type:

shell command

and press "install" in the PATH.

Then in your terminal go to the folder that you want to open and type "code ." It'll open the project in visual studio code as root.

TylerH
  • 20,799
  • 66
  • 75
  • 101
alesm
  • 29
  • 1
1

To able to run vs code as superuser: open code file with a text editor

sudo nano /usr/bin/code

then comment on the following line of codes :

if [ "$(id -u)" = "0" ]; then
    for i in "$@"
    do
        case "$i" in
            --user-data-dir | --user-data-dir=* | --file-write )
                CAN_LAUNCH_AS_ROOT=1
            ;;
        esac
    done
    if [ -z $CAN_LAUNCH_AS_ROOT ]; then
        echo "You are trying to start Visual Studio Code as a super user which isn't recommended. If this was intended, please specify an alternate user data directory using the \`--user-data-dir\` argument." 1>&2
        exit 1
    fi
fi

to

#if [ "$(id -u)" = "0" ]; then
#   for i in "$@"
#   do
#       case "$i" in
#           --user-data-dir | --user-data-dir=* | --file-write )
#               CAN_LAUNCH_AS_ROOT=1
#           ;;
#       esac
#   done
#   if [ -z $CAN_LAUNCH_AS_ROOT ]; then
#       echo "You are trying to start Visual Studio Code as a super user which isn't recommended. If this was intended, please specify an alternate user data directory using the \`--user-data-dir\` argument." 1>&2
#       exit 1
#   fi
#fi
Mercurial
  • 27
  • 4
0

In my case, in OpenSuse tumbleweed, I performed a couple of extra steps:

  1. I needed to invoke sudo telling it to preserve the environment as vscode (X11 really) needs to know the value of the DISPLAY environment variable.
  2. I wanted to reuse my existing data dir and installed extensions.
sudo -E code --user-data-dir=$HOME/.config/Code --extensions-dir=$HOME/.vscode/extensions

After I am done with my editing / debugging session, I needed to reset the permissions of the data dir and extensions, like so:

sudo chown -R your-user:your-group $HOME/.config/Code $HOME/.vscode/extensions

Where your-user and your-group correspond to your uid/gid which you can obtain from the id command.

luv2learn
  • 606
  • 8
  • 12
0

here's a step-by-step solution on how to resolve the error "You are trying to start Visual Studio Code as a super user which isn't recommended" when upgrading VS Code in the terminal:

  1. Open a terminal window and navigate to the directory where the VS Code executable is located.

  2. Run the command:code --no-sandbox --user-data-dir=/my/custom/data/dir . This will start VS Code without the sandbox and use the specified directory for user data.

Rohit
  • 9
  • 2
0

Running vscode as root stop working (they removed --no-sandbox option) [https://code.visualstudio.com/updates/v1_59#_progress-for-electron-sandbox-support] [https://github.com/microsoft/vscode/issues/146445]

If you just want to browse, view and edit 'root' owned directory tree (/etc/, /var/lib/, ...) as root using vscode, you can do via Remote - SSH vscode extension. Just connect to your localhost as root via ssh (you need sshd service running): In short:

  1. Edit /etc/ssh/sshd_config:
Match Address 127.0.0.1
    PermitRootLogin yes
  1. Execute:
# generate ssh private key if you don't have. In my case it's RSA key.

sudo sh -c "cat /home/YOUR_USER/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys"
sudo systemctl restart ssh

# Test that you can connect:
ssh root@localhost
  1. Connect to your localhost via "Remote Explorer" in vs code.

[https://ponteshare.ch/2022/01/vscode-remote-ssh-as-root/]

Juraj Michalak
  • 1,138
  • 10
  • 9