我试图让这个命令作为用户postgres(所以我可以运送wal文件)工作:
rsync -a /tmp/test postgres@server2:/tmp/test
但是我得到的错误:
Permission denied (publickey).
我运行ssh-keygen eval `ssh-agent`和ssh-add作为server1上的postgres用户。 keygen创build了/var/lib/postgresql/.ssh/id_rsa和id_rsa.pub ,我可以看到它是通过使用ssh -vvv postgres@server2 。
在server2上,我创build了/var/lib/postgresql/.ssh/authorized_keys把id_rsa.pub的内容放在server1里面。 它由postgres用户和组以及chmod 600拥有.ssh目录也由postgres和chmod 700拥有。
我可以从详细sshd日志loggingserver2上看到Failed publickey for postgres...
postgres用户在两个服务器上: postgres:x:106:114:PostgreSQL administrator,,,:/var/lib/postgresql:/bin/bash
ssh -vvv postgres @ server2
... debug1: Found key in /var/lib/postgresql/.ssh/known_hosts:1 debug1: ssh_ecdsa_verify: signature correct debug2: kex_derive_keys debug2: set_newkeys: mode 1 debug1: SSH2_MSG_NEWKEYS sent debug1: expecting SSH2_MSG_NEWKEYS debug2: set_newkeys: mode 0 debug1: SSH2_MSG_NEWKEYS received debug1: Roaming not allowed by server debug1: SSH2_MSG_SERVICE_REQUEST sent debug2: service_accept: ssh-userauth debug1: SSH2_MSG_SERVICE_ACCEPT received debug2: key: /var/lib/postgresql/.ssh/id_rsa (0x7f468e434000) debug2: key: /var/lib/postgresql/.ssh/id_dsa ((nil)) debug2: key: /var/lib/postgresql/.ssh/id_ecdsa ((nil)) debug1: Authentications that can continue: publickey debug3: start over, passed a different list publickey debug3: preferred gssapi-keyex,gssapi-with-mic,publickey,keyboard-interactive,password debug3: authmethod_lookup publickey debug3: remaining preferred: keyboard-interactive,password debug3: authmethod_is_enabled publickey debug1: Next authentication method: publickey debug1: Offering RSA public key: /var/lib/postgresql/.ssh/id_rsa debug3: send_pubkey_test debug2: we sent a publickey packet, wait for reply debug1: Authentications that can continue: publickey debug1: Trying private key: /var/lib/postgresql/.ssh/id_dsa debug3: no such identity: /var/lib/postgresql/.ssh/id_dsa debug1: Trying private key: /var/lib/postgresql/.ssh/id_ecdsa debug3: no such identity: /var/lib/postgresql/.ssh/id_ecdsa debug2: we did not send a packet, disable method debug1: No more authentication methods to try. Permission denied (publickey).
server2 sshd_config(删除了注释行)
Port 22 Protocol 2 HostKey /etc/ssh/ssh_host_rsa_key HostKey /etc/ssh/ssh_host_dsa_key HostKey /etc/ssh/ssh_host_ecdsa_key UsePrivilegeSeparation yes KeyRegenerationInterval 3600 ServerKeyBits 768 SyslogFacility AUTH LogLevel VERBOSE LoginGraceTime 120 PermitRootLogin yes StrictModes yes RSAAuthentication yes PubkeyAuthentication yes IgnoreRhosts yes RhostsRSAAuthentication no HostbasedAuthentication no PermitEmptyPasswords no ChallengeResponseAuthentication no PasswordAuthentication no X11Forwarding yes X11DisplayOffset 10 PrintMotd no PrintLastLog yes TCPKeepAlive yes AcceptEnv LANG LC_* Subsystem sftp /usr/lib/openssh/sftp-server UsePAM yes
server2validation日志
Jan 16 03:54:21 ip-10-28-26-251 sshd[7972]: Set /proc/self/oom_score_adj to 0 Jan 16 03:54:21 ip-10-28-26-251 sshd[7972]: Connection from 10.28.123.97 port 49377 Jan 16 03:54:21 ip-10-28-26-251 sshd[7972]: Failed publickey for postgres from 10.28.123.97 port 49377 ssh2 Jan 16 03:54:21 ip-10-28-26-251 sshd[7972]: Connection closed by 10.28.123.97 [preauth]
我错过了什么? 我猜测,sshd没有看我的server2上的authorized_keys文件
您需要在server2的sshd_config进行以下input:
AuthorizedKeysFile .ssh/authorized_keys
假设你的从服务器允许密钥authentication,你只需要更新/etc/ssh/sshd_config如果你设置了AllowedUsers ,那么你需要确保postgres在这个列表中。
除此之外,只需将ssh-keygen (保留私钥密码空白),然后将~/.ssh/authorized_keys目录/文件添加到从属服务器。 postgres的主目录是/var/lib/postgresql ,但是如果你以postgres用户身份执行这些操作,那么你可以使用~ ,更不用说你不需要任何东西了,因为postgres会拥有主服务器上生成的ssh密钥, postgres将拥有从服务器上创build的目录/文件。
请务必在主服务器和从服务器上安全地设置文件权限:
# On master chmod 700 ~/.ssh chmod 600 ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa.pub chmod 600 ~/.ssh/known_hosts # this one won't exist until you SSH once # On slave chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys
禁用selinux系统是一个糟糕的解决scheme。
创build一个允许您需要的特定操作的策略模块会更好。
这是我在RHEL6中做的:
我清除了我的审计日志,重新启动rsyslogd,并重复了这个问题。
接下来,使用audit2allow来查看人类可读的问题:
# audit2allow -w -a type=AVC msg=audit(1438288591.000:8525): avc: denied { open } for pid=6063 comm="sshd" path="/var/lib/pgsql/.ssh/authorized_keys" dev="dm-0" ino=920234 scontext=unconfined_u:system_r:sshd_t:s0-s0:c0.c1023 tcontext=unconfined_u:object_r:postgresql_db_t:s0 tclass=file Was caused by: Missing type enforcement (TE) allow rule. You can use audit2allow to generate a loadable module to allow this access. type=AVC msg=audit(1438288591.000:8525): avc: denied { read } for pid=6063 comm="sshd" name="authorized_keys" dev="dm-0" ino=920234 scontext=unconfined_u:system_r:sshd_t:s0-s0:c0.c1023 tcontext=unconfined_u:object_r:postgresql_db_t:s0 tclass=file Was caused by: Missing type enforcement (TE) allow rule. You can use audit2allow to generate a loadable module to allow this access. type=AVC msg=audit(1438288591.000:8526): avc: denied { getattr } for pid=6063 comm="sshd" path="/var/lib/pgsql/.ssh/authorized_keys" dev="dm-0" ino=920234 scontext=unconfined_u:system_r:sshd_t:s0-s0:c0.c1023 tcontext=unconfined_u:object_r:postgresql_db_t:s0 tclass=file Was caused by: Missing type enforcement (TE) allow rule. You can use audit2allow to generate a loadable module to allow this access.
在确定没有额外的拒绝事件发生之后,并且这些事件是特定于问题的,请创build一个selinux模块以允许sshd为postgres authorized_keys读取,打开和getattr:
# audit2allow -a -M sshd_read_postgres_ssh_authorized_keys
现在安装结果模块:
# semodule -i sshd_read_postgres_ssh_authorized_keys.pp
我将这个模块复制到对等postgres服务器,并在那里安装。 我现在可以使用postgres将ssh作为box之间的公钥authentication,而我仍处于selinux执行状态。
SELinux在Redhat 6.5上对我造成了这个问题。 修正使用:
setenforce宽容
您可以在现有的ssh服务器上打开debugging日志。 在文件/ etc / ssh / sshd_config中更改LogLevel DEBUG3如果login失败的原因是Could not open authorized keys '/var/lib/pgsql/.ssh/authorized_keys': Permission denied ,访问权限authorized_keys似乎没问题,那么这个命令将有所帮助
restorecon -FRvv /var/lib/pgsql/.ssh/
说明
我发现格雷戈里的回答只是部分为我工作,虽然它指出我在正确的方向。 我发现需要一些规则/政策,只能按特定顺序生成。
由于ssh-copy-id命令只有在另一端有密码的情况下才有效,您需要scp公钥并相应地调整用户和权限。 这样就不需要创build密码,并且一个可能的访问点被closures。
要创build一个postgres用户密钥,并允许它自己ssh,但如果你把这个密钥到另一台服务器,它也将工作。
# su postgres $ cd $ ssh-keygen # [enter....] * $ cd .ssh/ $ cp id_rsa.pub authorized_keys $ chmod 0600 authorized_keys
每个错误都需要在为其生成规则之前发生,因此在每个策略添加了另一个尝试ssh之后,只需input密码提示即可。
$ ssh [email protected]
为简单起见,自我介入,但它仍然有效。 出错后:
$ exit # audit2allow -a -M sshd_open_postgres_ssh_authorized_keys # semodule -i sshd_open_postgres_ssh_authorized_keys.pp # su postgres $ ssh [email protected] $ exit # audit2allow -a -M sshd_read_postgres_ssh_authorized_keys # semodule -i sshd_read_postgres_ssh_authorized_keys.pp # su postgres $ ssh [email protected] $ exit # audit2allow -a -M sshd_getattr_postgres_ssh_authorized_keys # semodule -i sshd_read_postgres_ssh_authorized_keys.pp # su postgres $ ssh [email protected]
应该这次工作
注意:'#'表示root,但是使用sudo,我这样写就是为了方便 – 不是指出最佳实践