Question

How to edit files with VS Code remote-ssh with non-root user?

I’ve been using VS Code remote SSH to ssh into my droplets and edit files when needed, per this tutorial: https://www.digitalocean.com/community/tutorials/how-to-use-visual-studio-code-for-remote-development-via-the-remote-ssh-plugin

However, up till now I was using a root user. For security purposes, I’m trying to disable root access: https://www.digitalocean.com/community/tutorials/how-to-disable-root-login-on-ubuntu-20-04

But when I ssh with a non-root user, I’m only able to view files with VS Code. If I want to edit/save my changes, I have to use sudo, like so: sudo code filename, and this throws me an error: sudo: code: command not found

I found a couple of threads discussing this issue, but no resolutions (see below). If anyone has any pointers, I’d appreciate it. I’d really hate having to go back to nano and Vim :)

https://github.com/microsoft/vscode-remote-release/issues/1688 https://github.com/microsoft/vscode/issues/48659


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

alexdo
Site Moderator
Site Moderator badge
April 25, 2023

Hello @geochanto

It is a good idea to edit the files with non-root user, as this can also help you to collaborate with other users without the need of giving your root user credentials or sharing ssh-keys.

You can make sure the username is added to the corresponding group (If the files/folders of your site/application are managed by the www-data user/group, then add the user you will use in VSCode to this group).

https://www.digitalocean.com/community/tutorials/how-to-add-and-delete-users-on-ubuntu-20-04

I’ll also recommend checking this article on tips and tricks on remote development with Visual Studio Code

https://code.visualstudio.com/docs/remote/troubleshooting

Regards

KFSys
Site Moderator
Site Moderator badge
April 25, 2023

Hey @geochanto,

The issue should be because you are trying edit files which have root:root ownership. When using a non-root user, you may not have permission to edit certain files or directories. To overcome this issue, you can change the ownership or permissions of the files and directories you need to edit, so your non-root user can modify them without using sudo.

Here is an example of how you can achieve this using a new group web-admin:

  1. SSH into your droplet and create a new group, for example, “web-admin”:
sudo groupadd web-admin
  1. Add your non-root user to the group: Replace <your_user> with your non-root username:
sudo usermod -a -G web-admin <your_user>
  1. Change the ownership of the files and directories: Change the ownership of the files and directories you want to edit to the “web-admin” group. For example, if you want to edit files in the /var/www/html directory:
sudo chown -R :web-admin /var/www/html
  1. Change the permissions of the files and directories: Give the group write permissions to the files and directories you want to edit:
sudo chmod -R g+w /var/www/html
  1. Re-login to apply the changes: Exit the SSH session and log back in for the changes to take effect.

Now, you should be able to edit the files in /var/www/html using the Remote SSH extension in VS Code without running into permission issues.

Remember to replace /var/www/html with the path to the files or directories you need to edit. Also, be cautious when changing file permissions, as it can potentially create security vulnerabilities if not handled correctly. Only grant permissions to users who require access to specific files or directories.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up