When starting ssh session with ssh command I seem to have two options – default interactive session with default env and starting from home directory – or to execute arbitrary command but non-interactively (even tricks like ssh "command; command; bash -i -l"
don't seem to do much good). Quite often I want interactive session, but with something happening before that – normally a directory change, or sometimes system environment adjustment. These things would vary from session to session, so I cannot just stick them into .bashrc
or so.
Is there any way to make that happen?
Best Answer
I'm self-answering, as I've finally discovered the secret. Neither
-t
option forssh
, nor-l
option forbash
will lead to login shell on their own - but in combination they work.ssh user@host.com -t 'cd /some/where; FOO=BAR NUMBER=42 bash -l'
changes directory, sets environment variables, and then starts proper login shell (the only difference I've found so far is that/etc/motd
isn't displayed this way - it's normallyssh
's orlogin
's responsibility, notbash
's - other than that everything seems to work perfectly, and all environmental variables are identical).These environment / directory changes happen after ssh, so they're not restricted by
PermitUserEnvironment
and related settings (exactly as planned), but before.bashrc
/.profile
get executed. This has upsides and downsides - it is harder to just override something that gets set from bash init scripts likePS1
, but easier to pack exactly the right values intossh
command lines, and have.profile
do all the heavy lifting.And if really necessary, it's actually pretty easy to get bash to execute something after
.profile
with command line likessh user@foo.com -t 'cd /mnt; echo ". ~/.bash_profile; PS1=\"\\h-\w \"" >~/xxx; bash --init-file ~/xxx'
- very ugly when put that way, but these alternative.profile
files can be prepared before. (as far as I can tellbash
has a few candidate locations for.profile
script and will execute the first one found -. file
doesn't have such automatic fallbacks, so you'll need to check where's your normalprofile
if you want to do that)