Home » How to vscode sudo connect to remote host

How to vscode sudo connect to remote host

by mn.sobieh

When I use vscode remote explorer to connect to remote servers to read long configuration files. or investigate/search for configuration in a system, software, or solution that has no information about. I face files navigation limitations due to the limited user which I use it to connect to the server is limited. This article explains how to elevate vscode and allow it to connect to remote host as root with sudo

This will allow vscode to work on the remote host as root.

Remote host sudo user.

The user you connect to should have sudo without password in my example username is ansible

Connect to the remote host then create a file in /etc/sudoers.d/ or update the main file using visudo

  • Add to /etc/sudoers.d/admin
ansible ALL=(ALL:ALL) NOPASSWD:ALL 
  • Then change permission to 0440
chmod 0440 /etc/sudores/admin
  • Verify sudo setting by typeing
visudo -c

vscode-Server directory structure

now when you connect to the remote host with vscode for the first time, It creates a hidden directory inside the user’s home directory named vscode-server where it installs vscode-server binaries and configuration inside it.

Next, it creates a sub-directory with unique ID related to your vscode editor inside the vscode-server direcotry. Then, It starts an instance of vscode-server and terminate it when you disconnect

This unique identifier is related to the client you connect from. For example, I connect from two workstations, therefore I have two directories.

vscode remote host directory structure
vscode remote host directory structure

Configure vscode with sudo

After you connect to the server

find which client ID you are using by typing ps command

ps command to find process
find vscode process and path

navigate into ~/.vscode-server/bin

List the content to detect latest updated directory

Edit the server.sh which is located inside clientID directory

vscode remote host server script location
vscode remote host server script location

Add sudo in the begining of the last line

Server.sh code

Conclusion, Now when you connect using vscode to the remote host, you will find you self connected with sudo privliges and vscode Terminal will show as root user with full priviliges.

You may also like

Leave a Comment