Reputation: 371
I am using visual studio code with this command in ubuntu linux Running VS Code with the root privileges (not recommended)
sudo code --user-data-dir="~/.vscode-root"
I always use this in command line because normal way visual studio code does not allow me to save my code updates. but today
sudo code --user-data-dir="~/.vscode-root"
this code does not work anymore when i try to run it in command line, it does not open visual studio code anymore. Giving click in vscode icon run normally but it does not allow me to save anything, it asked me retry as sudo, i do and it does not do nothing, i can not enter as sudo in this way. any suggestions on how to solve this issue? Thanks in advance
Upvotes: 12
Views: 26403
Reputation: 31
If you configure the sshd in wsl with root access, you can use vscode with remote ssh.
create a /usr/local/bin/sucode
as below
#!/bin/sh
# wsl host name configured in /etc/wsl.conf
ssh_profile=root@wsl-ubuntu
# function to encode string to url string
urlencode() {
python -c 'import urllib.parse, sys; print(urllib.parse.quote(" ".join(sys.argv[1:])))' "$@"
}
# get absolute path
path_string=$(realpath $@)
# get url of path
path_url=$(urlencode $path_string)
# if is file
[ -f $1 ] && code --file-uri "vscode-remote://ssh-remote+$ssh_profile$path_url"
# if is folder
[ -d $1 ] && code --folder-uri "vscode-remote://ssh-remote+$ssh_profile$path_url"
and make sure it has the correct ownership and permission.
Now you can use
sucode dir[/file]
Upvotes: 0
Reputation: 1
Personally, if I have to edit anything that requires administrative access, I'll normally use Vim to do so instead of VSCode.
Security aspects aside, I completely agree with the answer by glob about using /root/.config/Code
instead of '.'
. Or, create a directory for this somewhere else...
As implied, some people want to edit nginx/Apache files or perhaps Linux configuration files such as /etc/hosts as a Visual Studio Code Superuser.
If you were to use '.'
it will create the entire "Code" directory structure inside of the working directory (/etc or /etc/nginx, etc.) in a dispersed fashion. That's at least 15 directories including Backups, CachedExtensions, Code Cache, plus files. This is not desired in a place such as /etc or any other directory that requires administrative access. Not only is it messy, but is a potential security concern.
Upvotes: 0
Reputation: 41
I had the same problem
sudo code myfile
stopped working sometime in July/August 2021
I use VS Code as my standard code/text editor and that includes editing different system config files where I need root privileges to edit.
A simple way to fix this problem is to add an alias in the config-file for your terminal. (~/.bashrc
if you have bash as your shell)
I call the alias "sucode"
alias sucode='sudo code $1 --user-data-dir='"'"'/root/.config/Code - OSS/'"'"' --no-sandbox'
Save the file and restart your shell (source ~/.bashrc
) and from now on you enter sucode myfile
instead of sudo code myfile
whenever you need to edit a text file with root privileges.
Please note the path to the root account's user data folder for VS Code.
On my Arch-system the folder where VS Code stores it's user data is called "Code - OSS" and for the root account the complete path to this folder is:
/root/.config/Code - OSS/
It's good to check to see if your installation of VS Code has the same path. If it hasn't, you have to modify the alias-command accordingly.
In my opinion it's not such a great idea to specify the user-data-dir as the dot-folder (".") because then VS Code will clutter every folder, where you happen to be when you call VS Code, with dozens of root owned folders.
Upvotes: 4
Reputation: 2411
best way (recommended):
just use this :
sudo code "your-directory" --user-data-dir='.' --no-sandbox
for example to open /etc/nginx/nginx.conf
, use:
sudo code /etc/nginx/nginx.conf --user-data-dir='.' --no-sandbox
vscode will create files where you open terminal because of :
--user-data-dir='.'
another way (not recommended):
use sudo chmod -R 777
But it will give the full access of your project directory to all users on the machine
You can use chown on the project directory to own it only for yourself
cd /location
sudo chown your_username your_project_directory
Example:
let's say I have a directory called project1
in my /var/opt
location and my user name is tom.
cd /var/opt
sudo chown tom project1
Upvotes: 10