by Brian Fitzgerald
Scenario
You have double checked everything, but ssh to postgres does not work.
Conditions
- You did a postgres package install
- ssh to other accounts works
- ssh to postgres using a key does not work
- Directory .ssh mode is 700
- File authorized_keys mode is 600
Selinux
Check whether selinux is enforcing acccess controls.
[postgres@test ~]$ getenforce Enforcing
Note in /var/log/audit/audit.log, “avc: denied…scontext…sshd…tcontext…postressql_db_t”
type=AVC msg=audit(1562467696.927:316): avc: denied { read } for pid=2282 comm="sshd" name="authorized_keys" dev="sda2" ino=56806 scontext=system_u:system_r:sshd_t:s0-s0:c0.c1023 tcontext=unconfined_u:object_r:postgresql_db_t:s0 tclass=file permissive=0
Note that the inode is 56806. Check ls -i:
[postgres@test ~]$ cd .ssh [postgres@test .ssh]$ ls -i authorized_keys 56806 authorized_keys
Check the authorized_keys selinux type:
[postgres@test .ssh]$ ls -Z authorized_keys -rw-------. postgres postgres unconfined_u:object_r:postgresql_db_t:s0 authorized_keys
The type is “postgresql_db_t”. In order for ssh to work, the type needs to be “ssh_home_t”.
Solution
Issue restorecon -R .ssh
[postgres@test ~]$ restorecon -R .ssh [postgres@test ~]$ ls -RZ .ssh .ssh: -rw-------. postgres postgres unconfined_u:object_r:ssh_home_t:s0 authorized_keys
Check:
[postgres@pgstby .ssh]$ ssh test date Sun Jul 7 02:57:21 UTC 2019
ssh works!
Note that new files created under directory .ssh will inherit the necessary ssh_home_t type.
chcon
If the home directory is an incompatible subtype then restorecon will not work. In that case, run chcon. i.e.:
[postgres@test ~]$ chcon -R unconfined_u:object_r:ssh_home_t:s0 ~/.ssh
or
[postgres@test ~]$ chcon -R system_u:object_r:usr_t:s0 ~/.ssh
semamage
If restorecon did not work, you can configure it to work in the future:
[root@test ~]# semanage fcontext -a -t ssh_home_t ~postgres/'.ssh(/.+)?'
Cause
The linux system was built from Azure image Redhat Enterprise Linux 7.6, which has selinux enabled by default.
Postgres was installed from package rh-postgresql10 created the user postgres, and then set the selinux type of most files and directories to postressql_db_t.
The .ssh directory inherited its type from its parent.
Normal home directory
The normal type of a home directory is user_home_dir_t. Subdirectory .ssh gets created as type ssh_home_t
[normal@test ~]$ ls -dZ drwx------. normal normal unconfined_u:object_r:user_home_dir_t:s0 . [normal@test ~]$ mkdir .ssh [normal@test ~]$ ls -dZ .ssh drwxrwxr-x. normal normal unconfined_u:object_r:ssh_home_t:s0 .ssh
Affected systems
In this example, the system was Redhat Linux on Azure, but it can happen on any system running selinux. The issue was noticed after a PostgreSQL package install. The same problem could appear with other package installs that create home directories.
Recommended practice
On selinux-enabled systems, create the .ssh directory this way:
[postgres@test ~]$ mkdir .ssh [postgres@test ~]$ chmod 700 .ssh [postgres@test ~]$ restorecon -R .ssh