更新: 前几天又遇到一次permission denied,忙活了老半天,一直百思不得其解,花了两天才解决,原来是selinux不知道什么时候打开了,还觉得自己又学到了东西,今天整理文章时才发现,这种事情以前早就解决过了,还写了这个教程。。。。。。。真是好记性不如烂笔头啊。

今天服务器用着用着突然掉线,然后一直连不上,提示permission denied,而且ssr和网站也挂了。因为用的是vultr的vps,所以上官网看了一下,发现有个通知:

无标题.png

但是已经修好了,开始一直以为是这次维护把我服务器给弄出问题了,差点提交工单,只因笔记本没电关机导致没提交成功,然后在回家的路上就一直在手机上查原因。

后来通过vultr的网页终端查看到nginx和sshd启动不成功都有一个共同的原因:permission denied,配置文件之类的肯定是没改过,因为服务器断开之前我都没碰过这俩的文件,iptables防火墙的端口放行也正常,那就是考虑是外部问题了。

后来ssr能用了(就只有ssr能用,不知道为什么),网上一查看到这个

A quick look into the logfile will reveal the following messages:

$ tail /var/log/secure

Sep 18 13:27:50 server1 sshd[13095]: Received signal 15; terminating. Sep 18 13:27:50 server1 sshd[13798]: error: Bind to port 1234 on 0.0.0.0 failed: Permission denied. Sep 18 13:27:50 server1 sshd[13798]: error: Bind to port 1234 on :: failed: Permission denied. Sep 18 13:27:50 server1 sshd[13798]: Server listening on 0.0.0.0 port 22. Sep 18 13:27:50 server1 sshd[13798]: Server listening on :: port 22.```

This is SELinux in action. SSH is not supposed to listen on port 1234 as far as the SELinux rules are concerned. So we need to modify the SELinux configuration to allow sshd to listen on our new port 1234.

赶紧看了一下SELinux的状态:

1sestatus

果然,SELinux不知道什么时候打开了:

1SELinux status:                 enabled

赶紧关上:

 1# 临时关闭
 2setenforce 0
 3
 4# 让SELinux永久关闭
 5vim /etc/selinux/config
 6
 7# 将
 8SELINUX=enforcing
 9# 改为
10SELINUX=disabled
11
12# 重启
13reboot

然后问题就解决了。

  • 后话:不知道什么原因SELinux会打开,不过以后要是遇到permission denied,在没有修改过配置文件的前提下,应该先看看SELinux是不是被打开了。