通过无密码的 ssh 连接(rpm 安装)

Connect by ssh without password (rpm installation)

我已经在本地机器上安装了我自己的 rpm 包。在安装过程中,我应该测试没有密码的 ssh 连接。

rpm 包有一个文件夹,里面有软件和脚本。

在 rpm 的 %post 部分,我调用了我的脚本之一 (script1) 并创建了新用户 (将通过以下命令将其称为 "user1",例如 ):

groupadd user1
adduser -g user1 -c "sshuser" user1  >/dev/null

在此之后,我调用另一个脚本 (script2),其中包含以下内容:

su -c "[=11=]" user1
echo "y" | ssh-keygen -t dsa -P '' -f $HOME/.ssh/id_dsa
cat $HOME/.ssh/id_dsa.pub >> $HOME/.ssh/authorized_keys
chmod 600 $HOME/.ssh/authorized_keys
ssh-keyscan -H localhost

但是安装完成后,我做不到

su user1
ssh localhost

没有密码。 如果我手动执行

,我也无法在没有密码的情况下登录
su user1
echo "y" | ssh-keygen -t dsa -P '' -f $HOME/.ssh/id_dsa
cat $HOME/.ssh/id_dsa.pub > $HOME/.ssh/authorized_keys
chmod 600 $HOME/.ssh/authorized_keys

但是如果我之前删除 dsa-keys:

su user1
rm -f ~/.ssh/{id_dsa*,authorized*}
ssh-keygen -t dsa -P '' -f $HOME/.ssh/id_dsa
cat $HOME/.ssh/id_dsa.pub > $HOME/.ssh/authorized_keys
chmod 600 $HOME/.ssh/authorized_keys

我可以用

ssh localhost

没有问题。

此外,如果我手动 运行 script1script2 我可以成功使用 ssh。

我做错了什么?

我还检查了 $HOME/.ssh 中所有文件的权限

drwxr-xr-x. 2 user1 root 4096 Apr 17 11:44 .
drwx------. 5 user1 user1 4096 Apr 17 11:41 ..
-rw-------. 1 user1 user1 604 Apr 17 11:44 authorized_keys
-rw-------. 1 user1 user1 672 Apr 17 11:44 id_dsa
-rw-r--r--. 1 uesr1 user1 604 Apr 17 11:44 id_dsa.pub
-rw-------. 1 user1 user1 884 Apr 17 11:44 known_hosts
-rw-------. 1 user1 user1 884 Apr 17 11:44 known_hosts.old

更新:

cat /var/logs/messages | grep ssh
Apr 17 12:29:15 tivoli kernel: type=1400 audit(1429262955.308:57): avc:  denied  { read } for  pid=9711 comm="sshd" name="authorized_keys" dev=dm-0 ino=1187196 scontext=system_u:system_r:sshd_t:s0-s0:c0.c1023 tcontext=unconfined_u:object_r:user_home_dir_t:s0 tclass=file

但权限仍然

-rw-------. 1 user1 user1 604 Apr 17 11:44 authorized_keys

更新2: 还有助于仅删除 authorized_keys 并重新创建它:

rm -f $HOME/.ssh/authorized_keys
cat $HOME/.ssh/id_dsa.pub >> $HOME/.ssh/authorized_keys

通过在创建用户后添加以下行解决:

mkdir /home/user1/.ssh
restorecon -R -v /home/user1/.ssh

现在 script1 看起来像这样:

groupadd user1
adduser -g user1 -c "sshuser" user1  >/dev/null
mkdir /home/user1/.ssh
restorecon -R -v /home/user1/.ssh