是否可以以普通用户身份运行 sshd?

Bo *_*nes 47 ssh

我的目标是sshd使用我自己的配置文件在非特权端口(例如 2222)上启动第二个实例。

显然,该sshd进程不能setuid以运行sshd守护程序的用户以外的用户身份登录,这显然是不可能的。

但是,是否有可能有一个工作sshd守护程序适用于当前正在运行的用户?对于我的用例,这很好。

我尝试sshd使用我自己的配置文件和主机密钥启动一个实例,然后sshd进程启动(没有关于不是 root 的抱怨,就像某些命令一样),但是当我尝试连接到该端口时,sshd进程终止了。

$ /usr/sbin/sshd -dD -h .ssh/id_rsa -p 2222 
debug1: sshd version OpenSSH_5.6p1
debug1: read PEM private key done: type RSA
debug1: private host key: #0 type 1 RSA
debug1: setgroups() failed: Operation not permitted
debug1: rexec_argv[0]='/usr/sbin/sshd'
debug1: rexec_argv[1]='-dD'
debug1: rexec_argv[2]='-h'
debug1: rexec_argv[3]='.ssh/id_rsa'
debug1: rexec_argv[4]='-p'
debug1: rexec_argv[5]='2222'
debug1: Bind to port 2222 on 0.0.0.0.
Server listening on 0.0.0.0 port 2222.
debug1: Bind to port 2222 on ::.
Server listening on :: port 2222.
debug1: fd 6 clearing O_NONBLOCK
debug1: Server will not fork when running in debugging mode.
debug1: rexec start in 6 out 6 newsock 6 pipe -1 sock 9
debug1: inetd sockets after dupping: 5, 5
Connection from ::1 port 57670
debug1: Client protocol version 2.0; client software version OpenSSH_5.6
debug1: match: OpenSSH_5.6 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.6
debug1: list_hostkey_types: 
No supported key exchange algorithms
debug1: do_cleanup
debug1: do_cleanup
debug1: audit_event: unhandled event 12
Run Code Online (Sandbox Code Playgroud)

debug1: setgroups() failed: Operation not permitted条线显然很突出,但直到它尝试接受连接才会终止。

Bo *_*nes 46

经过一番挖掘,我想通了。

从您创建的新文件的sshd -f ~/.ssh/sshd_config位置开始该过程/.ssh/sshd_config。在其他选项(例如不同的主机密钥、不同的端口等)中,您需要添加行UsePrivilegeSeparation no. 这将阻止sshd进程尝试执行任何setuidsetgid调用,并允许它以您的用户身份继续运行并以您的用户身份接受连接。

编辑:在弄清楚之后,其他人向我发送了此链接,这证实这是正确的方法:http : //cygwin.com/ml/cygwin/2008-04/msg00363.html


小智 11

作为对该线程的更新,版本 7.5 中的 OpenSSH 弃用了 UsePrivilegeSeparation 选项,因此无法禁用权限分离。现在似乎不可能以用户身份运行 SSHD。

https://www.openssh.com/releasenotes.html

  • 好吧,我还没有尝试过,但我认为现在已经“修复”了:“sshd(8):在没有 root 权限的情况下启动时,不需要存在权限分离用户或路径。使运行回归测试更容易不接触文件系统。” (2认同)

ina*_*ndu 7

这是一个基于 Bo Jeanes 回答的 userland bash sript:

  • 在家里创建工作目录
  • 在工作目录中生成服务器密钥
  • 使用位于工作目录中的pid文件生成基本配置文件
  • 启动 SSH 守护进程

mkdir ${HOME}/custom_ssh
ssh-keygen -f ${HOME}/custom_ssh/ssh_host_rsa_key -N '' -t rsa
ssh-keygen -f ${HOME}/custom_ssh/ssh_host_dsa_key -N '' -t dsa
echo "Port 2222
HostKey ${HOME}/custom_ssh/ssh_host_rsa_key
HostKey ${HOME}/custom_ssh/ssh_host_dsa_key
AuthorizedKeysFile  .ssh/authorized_keys
ChallengeResponseAuthentication no
UsePAM yes
Subsystem   sftp    /usr/lib/ssh/sftp-server
PidFile ${HOME}/custom_ssh/sshd.pid" > ${HOME}/custom_ssh/sshd_config
/usr/bin/sshd -f ${HOME}/custom_ssh/sshd_config
echo "
--------
Process ID : ${HOME}/custom_ssh/sshd.pid
-------"
Run Code Online (Sandbox Code Playgroud)
  • OpenSSH_7.9p1、OpenSSL 1.1.1a 2018 年 11 月 20 日
  • pam auth(使用相同的本地和远程用户测试)

  • 我仍然看到:“debug1:setgroups()失败:不允许操作” (2认同)