防火墙的功能:简单地释放或阻止某些服务和端口
[En]
The function of firewall: simply to release or block certain services and ports
1、防火墙的简单操作
查看防火墙状态
systemctl status firewalld
开启防火墙
systemctl start firewalld
关闭防火墙
systemctl stop firewalld
设置防火墙开机自启
systemctl enable firewalld
设置防火墙取消开机自启
systemctl disable firewalld
2、firewall的直接规则
注意:要在防火墙开启状态下才可设置
1、查看防火墙放行的服务
firewall-cmd --list-all
2、在防火墙中放行某服务,并设为永久生效
firewall-cmd --permanent --add-service=&协议名
3、在防火墙中放行某端口,并设为永久生效
firewall-cmd --permanent --add-port=8088/tcp
4、刷新(重新加载)防火墙配置
firewall-cmd --reload
网络服务和协议名称之间的对应关系:
[En]
Correspondence between network services and protocol names:
服务名 协议名 vsftpd ftp NFS nfs SAMBA windows:cifs linux:smb、nmb APACHE http/https
3、firewall的富规则
注意:要在防火墙开启状态下才可设置
1、添加一条富规则(以172.25.1.0/24网段,ftp服务为例)
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=172.25.1.0/24 service name=ftp accept'
2、删除一条富规则
firewall-cmd --permanent --remove-rich-rule='rule family=ipv4 source address=172.25.1.0/24 service name=ftp accept'
3、笼统的设置一个攻击域
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=172.25.1.0/24 reject'
4、为某个具体的服务设置一个攻击域
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=172.25.1.0/24 service name=ssh reject'
5、添加端口到防火墙中:
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=172.25.1.0/24 port port=80 protocol=tcp accept'
6、添加端口转发:(要先添加端口才能端口转发)
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=172.25.1.0/24 forward-port port=8080 protocol=tcp to-port=80'
tcp:有去有回,类似于打电话
udp:有去无回,类似于发传真
SElinux也是Linux操作系统的一种安全访问规则。用于确定哪个进程可以访问哪些文件、目录和端口的一组安全规则。保护的对象是 服务(进程)、 服务对应的文件(目录)、 服务对应的端口。
SElinux可以被看作是与标准权限系统并行的权限系统,如果selinux开启,以root身份运行进程,访问文件不光要受 用户对文件访问权限的限制,还要受 进程对文件selinux上下文类型的限制,否则,就算是root用户运行的进程,也不一定能访问某个文件。
1、selinux的三种模式(状态)
名称 模式 作用 enforcing 强制模式 拒绝非法访问并录入日志 permissive 许可模式(警告模式) 暂时允许非法访问并录入日志 disabled 禁用模式 允许非法访问且不录入日志
如何切换selinux的状态:
#获取selinux状态
[root@localhost ~]# getenforce
临时切换:
[root@localhost ~]# setenforce 0 #临时关闭selinux策略 enforcing -> permissive
[root@localhost ~]# setenforce 1 #临时开启selinux策略 permissive -> enforcing
永久切换:
[root@localhost ~]# vim /etc/selinux/config
SELINUX=enforcing/permissive/disabled
[root@localhost ~]# reboot
2、SELinux的上下文
在linux系统里面,每个文件、进程、端口都具有SELinux上下文,它是一种安全策略,用来判断某个进程能否访问文件、目录或端口的工具。
[root@localhost /]# ll -Z
lrwxrwxrwx. root root system_u:object_r:bin_t:s0 bin -> usr/bin
dr-xr-xr-x. root root system_u:object_r:boot_t:s0 boot
drwxr-xr-x. root root system_u:object_r:device_t:s0 dev
drwxr-xr-x. root root system_u:object_r:etc_t:s0 etc
drwxr-xr-x. root root system_u:object_r:home_root_t:s0 home
...
第四列(用户:角色:类型:敏感度)
[En]
Fourth column (user: role: type: sensitivity)
用户 -> 系统用户(system_u);root及普通用户组成的未指定用户(unconfined_u)
角色 -> 系统角色(system_r);未指定角色(unconfined_r);对象角色(object_r)
类型 -> 以_t结尾,每个服务它的三个方面的类型要一一对应,即 服务对应的文件和端口要与服务本身的SELinux上下文类型一致
敏感度 -> s0,指的是安全等级,有0、1、2三种,数值越大,灵敏度越高
查看文件的上下文
方法一:ll -Z filename
[root@localhost etc]# ll -Z samba/
方法二:semanage fcontext -l | grep filename
filename要写绝对路径,且不一定能查看所有文件
[root@localhost etc]# semanage fcontext -l | grep /etc/ssh
查看进程的上下文
ps -auxZ | grep 进程
[root@localhost ~]# ps -auxZ | grep sshd
查看所有端口上下文
semamage port -l | grep 端口号
[root@localhost ~]# semanage port -l | grep 22
查看已经开放的端口上下文
[root@localhost ~]# netstat -pantZ
修改文件的上下文类型
临时修改:
chcon -t 上下文类型 filename
将selinux设置成disabled后reboot,然后再设置成enforcing后reboot,修改会失效,将还原成原始默认类型 -> 不推荐使用
[root@localhost ~]# chcon -t httpd_sys_content_t /opt/testfile
[root@localhost ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
[root@localhost ~]# reboot
[root@localhost ~]# sed -i 's/SELINUX=disabled/SELINUX=enforcing/g' /etc/selinux/config
[root@localhost ~]# reboot
[root@localhost ~]# ll -dZ /opt/testfile
永久修改:
semanage fcontext -a -t 上下文类型 ‘/filename(/.*)?’ #注意:这里的filename要写绝对路径
restorecon -RFv /filename #强制递归刷新上下文类型并显示刷新过程
[root@localhost ~]# semanage fcontext -a -t httpd_sys_content_t '/opt/test(/.*)?'
[root@localhost ~]# restorecon -RFv /opt/test/
修改端口的上下文类型(添加selinux上下文类型)
semanage port -a -t 端口上下文类型 -p tcp/udp 端口号
[root@localhost ~]# semanage port -a -t ssh_port_t -p tcp 22022
[root@localhost ~]# semanage port -l | grep ssh
3、selinux布尔值
当selinux开启时,系统默认会设置很多服务功能的开关,而且默认都是关闭的,sebool就是那个开关
getsebool -a(| grep 布尔值) #查看
setsebool bool名 on/off #设置开启或关闭
semanage boolean -l(| grep 布尔值) #查看布尔值是否永久开启(括号中右边那个值),并显示该布尔值状态的简短描述
注意:
1、文件会默认继承父文件夹的selinux类型;
2、文件被cp到新的文件夹下,会自动继承新文件夹的selinux上下文类型,但mv不会这样,仍会保留原上下文类型;
3、如果修改了某服务的配置文件位置,则必须重新修改该文件的selinux上下文类型,以重新匹配服务,否则服务无法访问该配置文件。
声明:未经许可,不得转载
Original: https://www.cnblogs.com/wzgwzg/p/15602936.html
Author: 王智刚
Title: Linux——防火墙、SELinux规则
原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/524319/
转载文章受原作者版权保护。转载请注明原作者出处!