无法绑定IPv4套接字:权限被拒绝

时间:2018-06-18 10:12:44

标签: postgresql postgresql-9.6

我正在尝试在一台机器上设置一个新的PostgreSQL 9.6实例。我已经在另一台机器上测试了它,并且它在该机器上正常工作。但是同样的过程并不适用于新机器。以下是我正在使用的步骤

  • 使用以下命令

    创建了一个新的数据目录
    /opt/rh/rh-postgresql96/root/bin/initdb -D /var/lib/pgsql/9.6/data/ 
    
  • 创建了一个服务文件/etc/systemd/system/rh-postgresql96-inst2.service,内容低于

    .include /lib/systemd/system/rh-postgresql96-postgresql.service
    [Service]
    Environment=PGDATA=/var/lib/pgsql/9.6/data/
    Environment=PGPORT=5433
    User=postgres
    Group=root
    
  • 使用命令systemctl enable rh-postgresql96-inst2
  • 注册的服务
  • 现在使用命令systemctl start rh-postgresql96-inst2开始服务。

所有这些步骤在一台机器上都能正常工作,但在第二台机器上却没有。

我在第二台机器上启动服务时遇到错误

rh-postgresql96-inst2.service - PostgreSQL database server
   Loaded: loaded (/etc/systemd/system/rh-postgresql96-inst2.service; enabled; vendor preset: disabled)
   Active: failed (Result: exit-code) since Mon 2018-06-18 09:59:01 UTC; 10s ago
  Process: 7552 ExecStart=/opt/rh/rh-postgresql96/root/usr/libexec/postgresql-ctl start -D ${PGDATA} -s -w -t ${PGSTARTTIMEOUT} (code=exited, status=1/FAILURE)
  Process: 7550 ExecStartPre=/opt/rh/rh-postgresql96/root/usr/libexec/postgresql-check-db-dir %N (code=exited, status=0/SUCCESS)

HINT:  Is another postmaster already running on port 5433? If not, wait a few seconds and retry.
LOG:  could not bind IPv4 socket: Permission denied
HINT:  Is another postmaster already running on port 5433? If not, wait a few seconds and retry.
WARNING:  could not create listen socket for "localhost"
FATAL:  could not create any TCP/IP sockets
LOG:  database system is shut down
systemd[1]: rh-postgresql96-inst2.service: control process exited, code=exited status=1
systemd[1]: Failed to start PostgreSQL database server.
systemd[1]: Unit rh-postgresql96-inst2.service entered failed state.
systemd[1]: rh-postgresql96-inst2.service failed.

但是,我可以使用pg_ctl启动服务。

另外,我已经使用netstat,lsof命令检查是否有任何其他postgresql实例在端口5433上运行,但事实并非如此。

事实上我尝试了5431,5434端口,但服务器没有启动

2 个答案:

答案 0 :(得分:2)

此问题与SELinux有关。 当我在两台机器上运行命令sestatus时,输出有点不同。 一台服务器有Current mode: permissive,第二台服务器有Current mode: enforcing。 所以我使用命令setenforce 0在第二台机器上将当前模式更改为permissive。

它解决了与权限相关的问题。现在我能够开始第二次实例。

答案 1 :(得分:0)

您应该允许postgres绑定到SELinux中的端口5433,而不是使用SELinux。

有一个端口参数 postgresql_port_t ,默认情况下具有端口5432和9898。

semanage port -l | grep post
postgresql_port_t              tcp      5433, 9898

您所要做的只是将端口5433添加到此列表中。

semanage port -a -t postgresql_port_t 5433 -p tcp
semanage port -l | grep post
postgresql_port_t              tcp      5433, 5432, 9898

之后,您可以启动Postgres服务器,监听端口5433

systemctl enable rh-postgresql96-postgresql
systemctl start rh-postgresql96-postgresql

netstat -tulpn
tcp        0      0 127.0.0.1:5432          0.0.0.0:*               LISTEN      2847/postgres       
tcp        0      0 127.0.0.1:5433          0.0.0.0:*               LISTEN      2775/postgres   

还有一个名为audit2allow的便捷工具,可以帮助调试selinux问题。

audit2allow -m whatiswrong < /var/log/audit/audit.log > /root/showme.te

文件 showme.te 向您展示了SELinux为什么不允许该服务执行您需要的操作。

您不应仅因为难以理解或不知道SELinux是如何工作而关闭SELinux。相反,您应该研究它:)

我推荐红帽峰会https://www.redhat.com/en/about/videos/summit-2018-security-enhanced-linux-mere-mortals

上的这次演讲