无法绑定 IPv4 套接字:权限被拒绝
could not bind IPv4 socket: Permission denied
我正在尝试在一台机器上设置一个新的 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 命令检查端口 5433 上是否有任何其他 postgresql 实例 运行,但事实并非如此。
事实上我也尝试了 5431、5434 端口但是服务器没有启动
此问题与 SELinux 有关。
当我在两台机器上 运行 命令 sestatus
时,输出有点不同。
一台服务器有 Current mode: permissive
,第二台服务器有 Current mode: enforcing
。
所以我在第二台机器上使用命令 setenforce 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 难以理解或者您不知道它是如何工作的就关闭它。相反,你应该学习它:)
我推荐 Red Hat 峰会上的这个讲座 https://www.redhat.com/en/about/videos/summit-2018-security-enhanced-linux-mere-mortals
我正在尝试在一台机器上设置一个新的 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 命令检查端口 5433 上是否有任何其他 postgresql 实例 运行,但事实并非如此。
事实上我也尝试了 5431、5434 端口但是服务器没有启动
此问题与 SELinux 有关。
当我在两台机器上 运行 命令 sestatus
时,输出有点不同。
一台服务器有 Current mode: permissive
,第二台服务器有 Current mode: enforcing
。
所以我在第二台机器上使用命令 setenforce 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 难以理解或者您不知道它是如何工作的就关闭它。相反,你应该学习它:)
我推荐 Red Hat 峰会上的这个讲座 https://www.redhat.com/en/about/videos/summit-2018-security-enhanced-linux-mere-mortals