CentOS安全方面的设置


开启防火墙firewalld

centos7默认使用firewall防火墙规则,这个比之前的iptalbes好多了

一、开启firewalld

[root@timophp.com ~]# systemctl start firewalld

二、开机自启动

[root@timophp.com ~]# systemctl enable firewalld

二、添加http服务

firewalld默认只添加了ssh和dhcpv6-client两个服务,我们搭建网站肯定要开启80端口,也就是http服务
[root@timophp.com ~]# firewall-cmd --add-service=http --permanent
--permanent参数表示永久有效,如果不加该参数,那么重启firewalld后添加的规则将失效

三、指定ip可以连接MySQL

firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.1.250" port protocol="tcp" port="3306" accept"

上面的什么意思呢?其实你看下面的内容就明白了:

zone文件:/etc/firewalld/zones/public.xml

<?xml version="1.0" encoding="utf-8"?>
<zone>
  <short>Public</short>
  <description>For use in public areas. You do not trust the other computers on networks to not harm your computer. Only selected incoming connections are accepted.</description>
  <service name="dhcpv6-client"/>
  <service name="http"/>
  <service name="ssh"/>
  <rule family="ipv4">
    <source address="192.168.1.250"/>
    <port protocol="tcp" port="3306"/>
    <accept/>
  </rule>
</zone>
有--permanent参数,记得要让firewalld重新加载规则

[root@timophp.com ~]# firewall-cmd --reload

具体可以参考:Linux Firewall详细讲解

修改ssh默认端口22

vim /etc/ssh/sshd_config

Port 22

改为:

Port 10086

端口号随便你改

修改防火墙规则

cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/

vim /etc/firewalld/services/ssh.xml

<port protocol="tcp" port="22"/>
修改为:
<port protocol="tcp" port="10837"/>
[root@timophp.com ~]# firewall-cmd --reload
[root@timophp.com ~]# systemctl restart sshd.service

禁止root登录

新增普通用户

[root@timophp.com ~]# adduser timophp
[root@timophp.com ~]# passwd timophp

修改ssh配置文件

[root@timophp.com ~]# vim /etc/ssh/sshd_config
PermitRootLogin yes

改为:

PermitRootLogin no

[root@timophp.com ~]# systemctl restart sshd.service

使用公钥验证登录