George T
George T

Reputation: 259

Edit files with VS Code ssh tool that require sudo permissions

I am trying to edit some apache configuration files in a server. I would like to use VS code ssh plug in, but I have not found a way to save the configured file, since I am logging into the VM as a user (with sudo permissions) but not root! I get a permission denied error.

I guess there must be a way to just edit the file from VS code (I also tried winSCP, but no result) and when I save the changes, save them as a sudoer.

I heard from a friend that when he tried to save the file from VScode, there is a promt to retry as sudo. I do not get that, and unfortunately I do not now any more details, other than he s using Macintosh.

Suggestions I came across and I do not like:

  1. Change file ownership
  2. Enable root log in

Suggestions I tried

  1. Tweak with the plug in (Tried to force sudo su as first command, configuring extention.js but there is an error that it writes in an empty pipe)
  2. Using winSCP

If someone has a suggestion of using another tool, I am happy to hear it.

Specs

Server runs Ubuntu 18, apache and my PC windows 10. If any other spec is required I will edit this section.

TLDR.

I need a way to edit a file owned by root, having logged in in as a user with root permissions. I would like to do it through a UI interface since I find difficult editing and creating multiple files through nano/vi.

Upvotes: 19

Views: 36063

Answers (8)

Saurabh Chandra Patel
Saurabh Chandra Patel

Reputation: 13586

To adjust your SSH connection, you may use the following command:

ssh -i 'key.pem' [email protected] -t 'sudo -i'

This command initiates an SSH connection with the specified server using the 'key.pem' file for authentication and opens connection with root privileges.

Upvotes: -1

Tolulope Adekunte
Tolulope Adekunte

Reputation: 21

I found this extension to be a better alternative for me

https://marketplace.visualstudio.com/items?itemName=FriedrichVoelker.save-as-root-on-remote

Instead of typing the commands in the save as root remote ssh by yy0931

I can just do ctrl + s and it will request for the password.

Upvotes: 2

Linh
Linh

Reputation: 397

There is an extension called "Save as Root in Remote - SSH". You can give that a try.

enter image description here

Upvotes: 7

ardh
ardh

Reputation: 21

This is the configuration that works for me. I'm using Windows 11 as the client, VSCode 1.82.0, SSH FS v1.26.1, and Rocky 8.6 as the destination server. You must find the appropriate SFTP server path on your server.

Press F1 and Open "Open User Settings(JSON)"

enter image description here

{
"name": "server-01",
"privateKeyPath": "d:\#PERSONAL\SSH\id_rsa",
"port": 3223,
"group": "LAB",
"root": "/etc/config",
"host": "10.25.26.27",
"username": "admin",
"label": "lab-server-01",
"sftpCommand": "/usr/libexec/openssh/sftp-server", // Change this to reflect your server
"sftpSudo": true, // or use a username, e.g., "admin"
}

I hope it will help anyone else.

Upvotes: 0

Wu Wei
Wu Wei

Reputation: 2277

Save as Root in Remote - SSH can solve that.

https://marketplace.visualstudio.com/items?itemName=yy0931.save-as-root

It builts on top the official Remote - SSH extension for vscode and let's you save a file as root from the command palette (F1, Ctrl+Shift+P, or Cmd+Shift+P).

It also allows you to save it with root privileges but as a certain user. In my case I needed to save it as www-data. Works fine.

You have to install the mentioned extension again on the remote after connecting with the Remote - SSH extension to the target machine. After that the Save as Root and Save as Specified User become available in the command palette:

enter image description here

Upvotes: 6

George T
George T

Reputation: 259

I actually fixed the VS code extension. If you go to the extension.js and search the running command (it is an array of functions with string output, joined by one empty space), you will find it named l. Just add to the list of the command

"-t sudo su"

And it works like charm.

Image of the file

Upvotes: 5

PerryBuddy
PerryBuddy

Reputation: 56

The given solution by Georg T is still valid, but the code can look different in newer versions (e.g. version 0.92.0).

For me it was easier to search for "running script" and replacing

"bash"

in the command, which you can see here: Remote SSH code snippet

And the path and file is also still the same:

C:\Users\<Username>\.vscode\extensions\ms-vscode-remote.remote-ssh-0.92.0\out\extension.js

Upvotes: 2

Lionel Houdelier
Lionel Houdelier

Reputation: 91

I had exactly the same problem and the only exact answer I found is with:

SSH FS vscode extension

and

adding "sftpSudo": true, to the "sshfs.configs" section in setting.json (look in the extension settings to find it easily).

It will connect the user with sudo automatically, and then we can modify all files on the fly.

Upvotes: 9

Related Questions