kanpeki
kanpeki

Reputation: 445

How to edit files from remote server as root while ssh-ing from VS code?

I'm connecting to a virtual machine in Azure using Remote-SSH extension in Visual Studio Code. Problem is I can't ssh as root, but need root privileges to edit some files in /etc folder. 'Sudo-ing' in the terminal window has no effect. I'm thinking changing the file permissions would allow me to edit the files as a normal user, but I'd rather avoid that. Any ideas how I could edit the files in VS Code then?

Upvotes: 9

Views: 10283

Answers (3)

Naoyuki Yamada
Naoyuki Yamada

Reputation: 21

You can achieve this by becoming root with RemoteCommand in ssh config.

example ssh config:

Host target-host-with-root
  HostName xx.xx.xx.xx
  User ubuntu
  IdentityFile ~/.ssh/target-host-key.pem
  RemoteCommand sudo -s

Then in vscode, remote-ssh: Connect-host and select target-host-with-root and you will be able to edit and save file as root.

Upvotes: 0

Cameron Tacklind
Cameron Tacklind

Reputation: 7224

One solution is to use bindfs (sudo apt install bindfs).

To enable editing /etc as root for the current user, working in ~/.bind-mounts/etc:

mkdir -p ~/.bind-mounts/etc
sudo bindfs --map=root/${USER}:@root/@$(id -gn) /etc ~/.bind-mounts/etc

# When you're done
sudo umount ~/.bind-mounts/etc
# or run bindfs with '-f' to keep it in the foreground

bindfs has many options. --map may not be the best. It's been working in my testing so far.

Everything seems to work as you'd expect. Lets you "explore" as root and any modifications will be made as if you had root's permissions. Just don't forget the security implications!


Original answer is here, behind the fold.

Upvotes: 0

kanpeki
kanpeki

Reputation: 445

I ended up using SSH FS extension instead. Created a new configuration with my connection details (host, username and private key). Then in File > Preferences > Settings > Extensions > SSH FS Configuration > Sshfs: Configs I added "sftpSudo": true, which allowed me to edit the files as root.

p.s. I'll leave the question open for a while longer, just in case somebody can explain how I can achieve this using Remote-SSH extension.

Upvotes: 7

Related Questions