SSH – Force Command execution on login even without Shell

后端 未结 4 1647
粉色の甜心
粉色の甜心 2021-01-31 20:39

I am creating a restricted user without shell for port forwarding only and I need to execute a script on login via pubkey, even if the user is connected via ssh -N user@ho

4条回答
  •  别跟我提以往
    2021-01-31 21:33

    The ForceCommand option runs without a PTY unless the client requests one. As a result, you don't actually have a shell to execute scripts the way you might expect. In addition, the OpenSSH SSHD_CONFIG(5) man page clearly says:

    The command is invoked by using the user's login shell with the -c option.

    That means that if you've disabled the user's login shell, or set it to something like /bin/false, then ForceCommand can't work. Assuming that:

    1. the user has a sensible shell defined,
    2. that your target script is executable, and
    3. that your script has an appropriate shebang line

    then the following should work in your global sshd_config file once properly modified with the proper username and fully-qualified pathname to your custom script:

    Match User foo
        ForceCommand /path/to/script.sh
    

提交回复
热议问题