Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Edit files with sudo #150

Open
barrelful opened this issue May 30, 2024 · 1 comment
Open

Edit files with sudo #150

barrelful opened this issue May 30, 2024 · 1 comment

Comments

@barrelful
Copy link

barrelful commented May 30, 2024

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants