SchoofsKelvin / vscode-sshfs

Extension for Visual Studio Code: File system provider using SSH
GNU General Public License v3.0
546 stars 36 forks source link

Opening terminal fails if folder is only accessible by root #323

Closed hatl closed 2 years ago

hatl commented 2 years ago

root as target user configured:

"username": "user"
"sftpSudo": "root"

When opening a terminal: bash: line 1: cd: /root/my_folder: Permission denied


sudo would need to be ran done before the cd command


My current workaround (unfortunately always opening the terminal in a specific folder): "terminalCommand": "sudo /bin/bash -c 'cd /root/my_folder && /bin/zsh'"

hatl commented 2 years ago

Temporary hack for my use case - definitely not a generic solution :wink:

diff --git a/src/pseudoTerminal.ts b/src/pseudoTerminal.ts
index 6ea9d43..1da1642 100644
--- a/src/pseudoTerminal.ts
+++ b/src/pseudoTerminal.ts
@@ -205,7 +205,8 @@ export async function createTerminal(options: TerminalOptions): Promise<SSHPseud
                     commands.unshift(`cd ${workingDirectory}`);
                 }
                 const pseudoTtyOptions: PseudoTtyOptions = { ...PSEUDO_TTY_OPTIONS, cols: dims?.columns, rows: dims?.rows };
-                const cmd = joinCommands(commands, separator)!;
+                // const cmd = joinCommands(commands, separator)!;
+                const cmd = `sudo /bin/bash -c 'cd ${workingDirectory} && /bin/zsh'`
                 Logging.debug(`Starting shell for ${connection.actualConfig.name}: ${cmd}`);
                 const channel = await toPromise<ClientChannel | undefined>(cb => client.exec(cmd, { pty: pseudoTtyOptions }, cb));
                 if (!channel) throw new Error('Could not create remote terminal');
SchoofsKelvin commented 2 years ago

The sftpSudo is, as the name indicates, only for the SFTP part of the connection.

For your user case, using terminalCommand, if /bin/zsh copies the working directory it's called in, you don't even need to cd. As you might've seen (although my code is a bit complex), it actually runs cd ...; <terminalCommand> so if your terminalCommand can inherit the working directory (which I assume it does, based on your code), no issue there.

Alternatively, in case the sudo changes the working directory first, you might be able to do something similar to sudo -c "cd $(pwd) && /bin/zsh" (or PWD=$(pwd); sudo "... $pwd ...") to stay in the current working directory. If zsh is already the shell for root, sudo -s might actually be all you need, which should keep the working directory.


Apart from improving the terminalCommand on your end, there are two features I could add to help with this though:

While that first feature is definitely doable, the latter one not so much. In the meantime, using the pwd command within terminalCommand should help your issue regarding the working directory, without having to run a custom version of the extension.

hatl commented 2 years ago

thanks for you reply unfortunately, the pwd won't work since the initial cd fails because of the missing permissions the ${remoteWorkspaceRoot} approach sounds interesting - this should fix the issue

SchoofsKelvin commented 2 years ago

Should be fixed in ddfafd5 (and follow-up 749a611), which'll be released in the next version (1.24.2 or later) of the extension.

I added the ${workingDirectory} variable which'll automatically get replaced with the intended working directory. For example, I set the Terminal Command for my non-root config to sudo bash -c "cd ${workingDirectory} && bash" (and made sure my non-root user can use sudo) and spawning a terminal will now properly run bash in the correct working directory. This also works with right-clicking a directory in the Explorer and using the Open remote SSH terminal context menu option.

The above also works when replacing the last bash with zsh, and it also works if you're trying to do it on a directory your user doesn't have permissions for (since the actual cd happens inside sudo).

hatl commented 2 years ago

works nicely thanks!

Yvtq8K3n commented 1 year ago

In my case, I'm able to open as root using the terminal with the hotfix "sudo bash -c "cd ${workingDirectory} && bash". However, when I try to open it via FileExplorer I get permission denied and cant access any files.