jeanp413 / open-remote-ssh

VSCode Remote Development: Open any folder on a remote machine using SSH.
MIT License
274 stars 36 forks source link

Edit files with sudo #150

Open barrelful opened 3 months ago

barrelful commented 3 months ago

I do not have access to root in machines but I have sudo to edit some files eg. /etc. How should I use this extension to edit those files? If I just use my ssh user and try to save it will say:

Failed to save 'filename': Unable to write file 'vscode-remote://ssh-remote+app/filepath/filename' (NoPermissions (FileSystemError): Error: EACCES: permission denied, open '/filepath/filename')

I found a couple of workarounds for VSCode on StackOverflow but not really for this extension.

@jeanp413 do you know a way to do that? Maybe would be nice to have this case in the docs since it should be a common case.

I created a small shell script I upload to remote host to workaround this limitation but I have to run for each file I edit:

execute_with_sudo.sh

# This file needs to be uploaded to the remote host
FILE_PATH=$1
TMP_DIR=~/tmp_edit

if [ -z "$FILE_PATH" ]; then
    echo "Usage: $0 /path/to/system/file"
    exit 1
fi

FILE_NAME=$(basename "$FILE_PATH")
TMP_FILE="$TMP_DIR/$FILE_NAME"

# Create the temporary directory if it doesn't exist
mkdir -p "$TMP_DIR"

# Copy the file to the temporary directory
sudo cp "$FILE_PATH" "$TMP_FILE"
sudo chown $USER:$USER "$TMP_FILE"

# Open the file in VSCodium for editing (this assumes you're connected via VSCodium's SSH extension)
echo "You can now edit $TMP_FILE in VSCodium..."

# Wait for the user to finish editing
read -p "Press [Enter] key after editing $TMP_FILE..."

# Copy the file back to its original location
sudo cp "$TMP_FILE" "$FILE_PATH"
sudo rm "$TMP_FILE"
echo "$FILE_PATH has been updated with your changes."
jeanp413 commented 3 months ago

This is a vscode issue related to filesystem https://github.com/microsoft/vscode/issues/48659, not related to this extension. From that issue I see a couple of workarounds: