Closed hatl closed 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');
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:
terminalCommand
currently supports VSCode-like variable (e.g. ${remoteWorkspaceRoot}
, as shown higher up in that file), and something similar could be added for $pwd
/$workingDirectory
etc.shellSudo
option. The complexity here is that there are several ways to sudo, i.e. sudo
and su
which might act differently between distributions, along with perhaps some other distributions having even different ones (and this is ignoring that I have to use those within all kinds of shells). I could make the extension try a bunch of them and stick with what works, but that's of course something I'd have to add.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.
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
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
).
works nicely thanks!
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.
root as target user configured:
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'"