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

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

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

debug1: setgroups() failed: Operation not permitted条线显然伸出来了,但是直到它尝试接受连接时它才会消失。

答案1

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

(此链接确认这是正确的方法:http://cygwin.com/ml/cygwin/2008-04/msg00363.html

答案2

这是一个基于 Bo Jeanes 的回答的用户空间 bash 脚本:

  • 在 home 中创建工作目录
  • 在工作目录中生成服务器密钥
  • 使用位于工作目录中的 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

cat << EOF > ${HOME}/custom_ssh/sshd_config
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
EOF

/usr/bin/sshd -f ${HOME}/custom_ssh/sshd_config
echo "----- Process ID : ${HOME}/custom_ssh/sshd.pid -------"
  • OpenSSH_7.9p1,OpenSSL 1.1.1a 2018 年 11 月 20 日
  • pam auth(使用相同的本地和远程用户测试)

答案3

作为此线程的更新,OpenSSH 7.5 版本弃用了 UsePrivilegeSeparation 选项,因此无法禁用权限分离。看来现在无法以用户身份运行 SSHD。

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

答案4

我详细检查了以普通用户身份运行 sshd 服务的可能性。程序版本的详细信息:

sshd 版本 OpenSSH_7.4,OpenSSL 1.0.2k

最后,在解决了许多错误之后,我到达了 SSHD 中止并出现以下错误:

尝试以非 root 用户写入登录记录(中止)

我检查了源代码,看看是否可以在不更改源代码的情况下解决问题。请参阅代码这里导致程序中止的某些部分代码:

#ifndef HAVE_CYGWIN
    if (geteuid() != 0) {
        logit("Attempt to write login records by non-root user (aborting)");
        return (1);
    }
#endif

它通过检查用户权限(geteuid() != 0)并由此导致问题。

相关内容