1

I'm on aix 7.1, my shell is bash

  • If i did
    ssh ibmunix 
    gqlplus  user/pass
    
    All works and gqlplus starts. gqlplus is on /opt/freeware/bin
  • If i did from another machine
    ssh ibmunix "gqlplus  user/pass"
    
    I get the error message
    gqlplus: command not found
    
  • If i did
    ssh -t  ibmunix "source .bash_profile; gqlplus  user/pass"
    
    it works fine.

The question is: is possible with ssh to read automatically .bash_profile?

AdminBee
  • 22,803
elbarna
  • 12,695

1 Answers1

2

I have found the solution first enable on sshd_config user environement

PermitUserEnvironment yes

Then edit $HOME/.ssh/environement and put PATH and whatever you want

The $HOME/.ssh/environment file can't contain other than variables and comments for example good environment file content

HOME=/home/user
PATH=$PATH:/opt/freeware/sbin:/bin:/opt/freeware/bin:/usr/bin:/sbin:/usr/sbin

bad environment file content

source $HOME/.profile
export PATH=$PATH:/usr/bin

The permission must also correct otherwise don't work correct perms are 0700 for .ssh and 0400 for .ssh/environment

ls -lhd .ssh/
drwx------ 2 username username 4,0K nov  4 16:29 .ssh//
ls -lhd .ssh/environment
-r-------- 1 username username 73 nov  4 16:29 .ssh/environment

The proof that works. On Linux the command sed_64 doesn't exist, it exist on Ibmaix remote machine on /opt/freeware/bin dir.

ssh linuxmachine sed_64
bash: sed_64: command not found

ssh remoteaix sed_64 Usage: sed_64 [OPTION]... {script-only-if-no-other-script} [input-file]...

-n, --quiet, --silent suppress automatic printing of pattern space -e script, --expression=script

.....

WARNING: the .ssh/environment is read before .bashrc and .bash_profile using ssh so if you don't put the correct PATH you will not find some commands, even if correct path is set on .bash_profile, .bashrc

WARNING: This configuration is safe only on home-private environment, don't use on production/serious places. Because the option PermitUserEnvironment allow any user to bypass their login shell and any ForcedCommand.

elbarna
  • 12,695
  • FYI PATH=$PATH:... doesn't work for me at all. If I set this and try $ ssh host 'echo $PATH', it prints a literal $PATH (and anything else results in command not found). Explanation here. – Ryan Lue Nov 03 '20 at 09:31
  • Works fine for me on Linux Slackware current, with sshd default configuration, tried now ssh remote 'echo $PATH' /usr/local/bin:/usr/bin:/bin:/usr/games echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/games:/usr/lib64/openjdk/bin:/usr/lib64/openjdk/jre/bin:/usr/lib64/qt/bin:/usr/lib64/qt5/bin:/sbin:/usr/sbin:/bin:/usr/bin I tried now on AIX – elbarna Nov 03 '20 at 18:45
  • Weird. FWIW my server is latest Debian stable with OpenSSH version 1:7.9p1-10+deb10u2. – Ryan Lue Nov 04 '20 at 07:56
  • Probably something wrong in your configuration, I have tried now on debian buster with ssh 1:7.9p1-10+deb10u2 and works fine ssh remote 'echo $PATH' /usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin $ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/games – elbarna Nov 04 '20 at 14:22
  • tried also on AIX, works perfect – elbarna Nov 04 '20 at 15:27
  • I have also improve my answer and test all now, works fine – elbarna Nov 04 '20 at 15:42
  • Thanks for following up. It's still not working for me, so I posted a question to try to get to the bottom of it. – Ryan Lue Nov 05 '20 at 02:38
  • @RyanLue The "proof" does not prove that $PATH is expanded in PATH=$PATH:/opt/freeware/bin:..., only that the PATH is set to that value. If you have a directory named literally $PATH in your current directory, commands will be looked up in it, too. Try with mkdir '$PATH'; ln -s /bin/uname '$PATH/cat' ;-) –  Nov 05 '20 at 08:18
  • 1
    This answer is dangerous; sysadmins should not enable PermitUserEnvironment because that will allow any user to bypass their login shell and any ForcedCommand, and break a lot of implicit assumptions made by ssh itself, other software and other sysadmins. –  Nov 05 '20 at 08:25
  • 1
    If you're able to user PermitUserEnvironment in the server's configuration, then you're also able to use SetEnv and set whatever environment variable you like. –  Nov 05 '20 at 08:31
  • I have improved the answer with the security consideration of user414777, thanks for the warning. – elbarna Nov 05 '20 at 10:02