主機防火牆(一般是軟件防火牆):針對單個主機進行防護
網絡防火牆(一般是硬件防火牆):作爲網絡的分界點,防護內網之外的***
無論是iptables
還是firewalld
都只是配置防火牆的工具,真正實現數據包連接和轉發的是系統內核中的netfilter
模塊
iptables
規則鏈
iptables有5個規則鏈,如果流量經過規則鏈,就會從該鏈由上至下匹配遇到的第一條規則。
- INPUT控制進入的流量
- OUTPUT控制發出的流量
- FORWORD控制轉發的流量
- POSTROUTING負責SNAT
- PREROUTING負責DNAT
查看規則
#查看 INPUT OUTPUT FORWORD 規則
[root@localhost ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
# 查看 POSTROUTING和PREROUTING snat和dnat 規則
[root@localhost ~]# iptables -L -t nat
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
Chain POSTROUTING (policy ACCEPT)
target prot opt source destination
設置規則
iptables -A INPUT -s 192.168.1.0/24 -p tcp --dport 22 -j ACCEPT
# 22是目標ssh端口, -s 192.168.1.0/24表示允許這個網段的機器來連接,其它網段的ip地址是登陸不了你的機器的。 -j ACCEPT表示接受這樣的請求
# -A INPUT 添加到INPUT鏈中
# -s 源地址
# -p 協議
# --dport 表示目標端口
# -j 表示動作 ACCEPT表示接受 REJECT表示拒絕 DROP表示丟棄
# REJECT會回覆拒絕,DROP不進行回覆
更改默認規則
默認規則是ACCECP,允許所有流量通過
iptables -P INPUT DROP # 配置默認的不讓進
iptables -P FORWARD DROP # 默認的不允許轉發
iptables -P OUTPUT ACCEPT # 默認的可以出去
清空當前的所有規則和計數
iptables -F # 清空所有的防火牆規則
iptables -X # 刪除用戶自定義的空鏈
iptables -Z # 清空計數
firewalld
firewalld服務,實際上是將防火牆配置命令轉換爲iptables規則。
防火牆(iptables/firewalld)只能應用於內核管理的網卡。
Zone
firewalld提供了zone的概念,zone就是區域,firewalld將系統劃分成多個zone。一個網卡只能屬於一個zone,一個zone中可以有多個網卡。zone中的規則應用於該zone中的所有網卡。
firewall-cmd添加規則必須要指定zone。不指定zone的操作就是對缺省zone進行操作
Zone相關操作
# 查看缺省zone
firewall-cmd --get-default-zone
# 設置缺省zone
firewall-cmd --set-default-zone=public
# 列出zone中的網卡
firewall-cmd --list-interfaces --zone=public
# 移除zone中的網卡
firewall-cmd --remove-interface=ens33
# 添加網卡到zone中
firewall-cmd --add-interface=ens33
規則
規則生效時間
默認配置的規則是臨時的,需要使用--permanent
讓規則永久生效--permanent
表示該規則永久生效,但是不是立刻生效
# 重新加載規則
# 會讓--permanent規則立刻生效,並清空臨時規則
firewall-cmd --reload
# 將所有臨時規則配置爲永久規則
firewall-cmd --runtime-to-permanent
查看規則
# 查看所有zone的所有規則
firewall-cmd --list-all-zones
# 列出缺省zone的規則
# 如果一個zone裏面沒有網卡,那麼這個zone就是非active狀態
firewall-cmd --list-all
public (active)
target: default
icmp-block-inversion: no
interfaces: ens33
sources:
services: ssh dhcpv6-client
ports:
protocols:
masquerade: no
forward-ports:
source-ports:
icmp-blocks:
rich rules:
規則類別和配置方法
service規則
# 列出service規則
firewall-cmd --list-services
# 添加service規則
firewall-cmd --add-service=http
# 去除service規則
firewall-cmd --remove-service=http
service和端口的對應關係,在/usr/lib/firewalld/services
文件夾的xml
文件裏。
# 示例 ssh是service名稱 port="22"是對應的端口號
[root@localhost ~]# cat ssh.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>SSH</short>
<description>Secure Shell (SSH) is a protocol for logging into and executing commands on remote machines. It provides secure encrypted communications. If you plan on accessing your machine remotely via SSH over a firewalled interface, enable this option. You need the openssh-server package installed for this option to be useful.</description>
<port protocol="tcp" port="22"/>
</service>
port規則
# 查看端口規則
firewall-cmd --list-port
# 添加端口規則
firewall-cmd --add-port=3333/tcp
rich規則(富規則)
# 添加rich規則
firewall-cmd --add-rich-rule="rule family="ipv4" source address="192.168.142.166" port protocol="tcp" port="6379" accept" --permanent
# 查看rich規則語法
man firewalld.richlanguage
SELinux
SELinux可以限制進程可以訪問的資源。但是除了特殊安全需求較高的環境,一般在生產環境中不開啓SELinux。
SELinux的開啓和關閉
# 查看SELinux的狀態
# 默認SELinux是開啓的,但是一般生產環境會將其禁用
[root@localhost ~]# getenforce
Enforcing
# 臨時關閉
[root@localhost ~]# setenforce 0
[root@localhost ~]# getenforce
Permissive
# 實際上Permissive狀態還是開啓的,只是不對訪問進行控制,但是會記錄到日誌中
# 日誌文件位置:/var/log/audit/audit.log
# 臨時開啓,只有在Permissive狀態下可以開啓,disabled狀態不能直接開啓
[root@localhost ~]# setenforce 1
[root@localhost ~]# getenforce
Enforcing
# 永久關閉
# 更改配置文件後重啓服務器
# 必須重啓才能改成disabled,
# 從disabled更改爲其他狀態也要重啓服務器,並且重啓後系統會對所有文件重新成安全上下文,佔用時間較長
[root@localhost ~]# vim /etc/selinux/config
SELINUX=disabled
安全上下文
安全上下文是控制進程對文件的訪問權限的
查看SELinux安全上下文
# 查看文件的安全上下文
# 9位權限列後面有.表示存在SELinux上下文
[root@localhost html]# ll -lZ
-rw-r--r--. root root unconfined_u:object_r:httpd_sys_content_t:s0 test
# 查看進程的安全上下文
[root@localhost html]# ps auxZ |grep httpd
system_u:system_r:httpd_t:s0 root 42322 0.0 0.5 224056 5000 ? Ss 00:25 0:00 /usr/sbin/httpd -DFOREGROUND
# 關注安全上下文倒數第二列,httpd_t進程只能訪問上下文是httpd_sys_content_t的文件
默認安全上下文
在不同位置創建的文件安全上下文是不同的
查看默認安全上下文
# 如果沒有默認安全上下文,會定義爲unconfined_t
[root@localhost html]# semanage fcontext -l|grep httpd|head
/usr/.*\.cgi regular file system_u:object_r:httpd_sys_script_exec_t:s0
/opt/.*\.cgi regular file system_u:object_r:httpd_sys_script_exec_t:s0
/srv/([^/]*/)?www(/.*)? all files system_u:object_r:httpd_sys_content_t:s0
/srv/([^/]*/)?www/logs(/.*)? all files system_u:object_r:httpd_log_t:s0
更改默認安全上下文
# 添加默認安全上下文(永久生效)
semanage fcontext -a -t http_sys_content_t '/web(/.*)?'
# 臨時更改安全上下文
chcon -t http_sys_content_t file
# 默認創建文件的時候可能會沒有匹配到詳細的安全上下文
# 重新生成安全上下文
restorecon -Rv logs/
# -v顯示詳細信息
# -R遞歸
# 在系統重啓後重新生成所有安全上下文
touch /.autorelable
對端口進行設置
控制進程能否使用某些端口
# 查看端口
semanage port -l
# 給進程添加端口權限
semanage port -a -t http_port_t -p tcp 12345
# -a 添加
# -t 類型
# -p tcp/udp 端口號
SELinux的布爾值
布爾值控制進程能否進行某些行爲
# 查看
[root@localhost html]# semanage boolean -l |head
SELinux boolean State Default Description
privoxy_connect_any (on , on) Allow privoxy to connect any
smartmon_3ware (off , off) Allow smartmon to 3ware
mpd_enable_homedirs (off , off) Allow mpd to enable homedirs
xdm_sysadm_login (off , off) Allow xdm to sysadm login
xen_use_nfs (off , off) Allow xen to use nfs
mozilla_read_content (off , off) Allow mozilla to read content
ssh_chroot_rw_homedirs (off , off) Allow ssh to chroot rw homedirs
mount_anyfile (on , on) Allow mount to anyfile
# 設置布爾值的開關
# 開啓
setsebool -P smartmon_3ware on
# 關閉
setsebool -P smartmon_3ware off