Plink does not source bashrc or bash_profile on connect

后端 未结 2 597
时光说笑
时光说笑 2021-01-04 12:32

I am trying to use plink as an ssh alternative on windows, but I am finding that when plink connects to a remote linux machine, it does not source .bash_profile or .bashrc.<

相关标签:
2条回答
  • 2021-01-04 13:10

    If you simply connect to a remote host via ssh or plink, it will start the login account's default shell. If that shell is bash, bash will automatically source .bash_profile.

    If you connect to a remote host via ssh or plink asking for a command to be executed, ssh will try to execute just that command.

    What you want to achieve can be done by using a ForcedCommand. See also here:

    • https://serverfault.com/questions/162018/force-ssh-to-use-a-specific-shell/166129#166129 and
    • http://oreilly.com/catalog/sshtdg/chapter/ch08.html

    Set the forced command to be a script that does 2 things:

    1. source the .bash_profile
    2. run original command (env vars $SSH_ORIGINAL_COMMAND or $SSH2_ORIGINAL_COMMAND)
    0 讨论(0)
  • 2021-01-04 13:21

    The accepted answer helped me solve the same problem using plink. Here is a bit more detail that may help people in future:

    When a ssh connection is made to run a single command using plink, bash is not invoked as an "interactive login shell", so it doesn't run /etc/profile, ~/.bash_profile, ~/.bash_login, or ~/.profile (see the bash manual pages).

    For my purposes, I needed ~/.profile to run prior to the command passed in the plink command line.

    A forced command can be added to the authorized_keys file for that key (see the sshd manual pages). A forced command (e.g. to run ~/.profile) stops it running the command specified by plink, so to get it to do both, the forced command should be to execute a script that runs .profile then the original plink command. The latter is stored in an environment variable $SSH_ORIGINAL_COMMAND so your script can do

    source .profile
    $SSH_ORIGINAL_COMMAND
    

    and you specify the script in the ~/.ssh/authorized_keys file as follows, before the key, on the same line:

    command="source forced_command.script" ssh-rsa A3AABzaC1yc...
    
    0 讨论(0)
提交回复
热议问题