本文介紹ip
、ss
、journalctl
和firewall-cmd
,它們旨在代替linux中原有的一些命令或服務。python
ip
ip [OPTIONS] OBJECT COMMAND
ip
是iproute2軟件包裏面的一個強大的網絡配置工具,它可以替代一些傳統的網絡管理工具,例如ifconfig
、route
等,使用權限爲超級用戶。OPTIONS
是修改ip行爲或改變其輸出的選項。OBJECT
是要獲取信息的對象。包括:linux
address 表示設備的協議(IPv4或IPv6)地址 link 表示網絡設備 monitor 表示監控網絡鏈接信息 neighbour 表示管理ARP緩存表 netns 表示管理網絡命名空間 route 表示路由表接口 tunnel 表示IP隧道 ....
對象名能夠是全稱或簡寫格式,好比address
能夠簡寫爲addr
或a
。COMMAND
設置針對指定對象執行的操做,它和對象的類型有關。shell
如顯示網卡ens33
的信息:編程
[root@centos7 ~]# ip addr show ens33 3: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:a4:a9:16 brd ff:ff:ff:ff:ff:ff inet 10.0.1.254/24 brd 10.0.1.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::250:56ff:fea4:a916/64 scope link valid_lft forever preferred_lft forever
選項-s
表示輸出更多的信息json
[root@centos7 ~]# ip -s addr show ens33 3: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:a4:a9:16 brd ff:ff:ff:ff:ff:ff inet 10.0.1.254/24 brd 10.0.1.255 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::250:56ff:fea4:a916/64 scope link valid_lft forever preferred_lft forever RX: bytes packets errors dropped overrun mcast 133518854 1415841 0 0 0 0 TX: bytes packets errors dropped carrier collsns 14033474 59479 0 0 0 0
爲ens33
增長一個新地址segmentfault
[root@centos7 ~]# ip addr add 192.168.0.193/24 dev ens33 [root@centos7 ~]# ip a sh ens33 3: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:50:56:a4:a9:16 brd ff:ff:ff:ff:ff:ff inet 10.0.1.254/24 brd 10.0.1.255 scope global ens33 valid_lft forever preferred_lft forever inet 192.168.0.193/24 scope global ens33 valid_lft forever preferred_lft forever inet6 fe80::250:56ff:fea4:a916/64 scope link valid_lft forever preferred_lft forever #刪除 [root@centos7 ~]# ip addr del 192.168.0.193/24 dev ens33
如查看arp表項(neighbour能夠簡寫爲neigh或n)centos
[root@centos7 ~]# ip neigh 172.20.71.253 dev ens32 lladdr 68:8f:84:03:71:e6 STALE 10.0.1.102 dev ens33 lladdr 00:50:56:a4:18:9a STALE 10.0.1.1 dev ens33 lladdr 68:8f:84:01:f1:ff STALE 10.0.1.103 dev ens33 lladdr 00:1c:7f:3b:da:b0 STALE 10.0.1.104 dev ens33 lladdr 00:50:56:a4:37:a7 DELAY 10.0.1.252 dev ens33 lladdr 00:50:56:a4:65:71 STALE
neighbour
可使用的COMMAND包括add
添加、change
修改、replace
替換、delete
刪除、flush
清除等。
如在設備ens33上爲地址10.0.1.253添加一個永久的ARP條目:緩存
[root@centos7 ~]# ip nei add 10.0.1.253 lladdr 78:A3:51:14:F7:98 dev ens33 nud permanent [root@centos7 ~]# ip nei show dev ens33 10.0.1.103 lladdr 00:1c:7f:3b:da:b0 STALE 10.0.1.1 lladdr 68:8f:84:01:f1:ff STALE 10.0.1.104 lladdr 00:50:56:a4:37:a7 REACHABLE 10.0.1.102 lladdr 00:50:56:a4:18:9a STALE 10.0.1.253 lladdr 78:a3:51:14:f7:98 PERMANENT 10.0.1.252 lladdr 00:50:56:a4:65:71 STALE
如更改ens33的MTU(最大傳輸單元)的值爲1600服務器
[root@centos7 ~]# ip link set dev ens33 mtu 1600 [root@centos7 ~]# ip link show dev ens33 3: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1600 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:50:56:a4:a9:16 brd ff:ff:ff:ff:ff:ff
關閉設備ens32網絡
[root@centos7 ~]# ip link set dev ens32 down [root@centos7 ~]# ip li ls dev ens32 2: ens32: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 1000 link/ether 00:50:56:a4:f6:f8 brd ff:ff:ff:ff:ff:ff
建立一個關聯到ens32的網橋
[root@centos7 ~]# ip link add link ens32 name br1 type bridge [root@centos7 ~]# ip link show dev br1 8: br1: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT link/ether 0e:00:3a:f2:fa:ee brd ff:ff:ff:ff:ff:ff #啓用 [root@centos7 ~]# ip link set dev br1 up #停用 [root@centos7 ~]# ip link set dev br1 down #刪除 [root@centos7 ~]# ip link del dev br1
如顯示路由表(這裏使用了命令column -t
對輸出進行了格式化)
[root@centos7 ~]# ip route show|column -t default via 10.0.1.103 dev ens33 proto static metric 100 10.0.1.0/24 dev ens33 proto kernel scope link src 10.0.1.254 metric 100 172.20.71.0/24 dev ens32 proto kernel scope link src 172.20.71.254 metric 100 192.168.78.0/24 via 10.0.1.104 dev ens33
如添加一條到192.168.0.0/16下一跳是10.0.1.101的路由
[root@centos7 ~]# ip route add 192.168.0.0/16 via 10.0.1.101 dev ens33 [root@centos7 ~]# ip route show|column -t default via 10.0.1.103 dev ens33 proto static metric 100 10.0.1.0/24 dev ens33 proto kernel scope link src 10.0.1.254 metric 100 172.20.71.0/24 dev ens32 proto kernel scope link src 172.20.71.254 metric 100 192.168.0.0/16 via 10.0.1.101 dev ens33 192.168.78.0/24 via 10.0.1.104 dev ens33 #刪除 [root@centos7 ~]# ip route del 192.168.0.0/16
還可使用change
、replace
等表示改變/替換原有路由條目。
如獲取單條路由信息
[root@centos7 ~]# ip rou get 10.0.1.0/24 broadcast 10.0.1.0 dev ens33 src 10.0.1.254 cache <local,brd>
ss
ss [options] [FILTER]
ss
命令能夠用來獲取socket統計信息,它能夠顯示和netstat
相似的內容。但ss
的優點在於它可以顯示更多詳細的有關TCP和鏈接狀態的信息,並且比netstat更高效。當服務器的socket鏈接數量變得很是大時,不管是使用netstat命令仍是直接cat /proc/net/tcp
,執行速度都會很慢。ss
命令利用了TCP協議棧中tcp_diag
,tcp_diag
是一個用於分析統計的模塊,能夠得到linux內核的第一手信息,這確保了ss的快捷高效。
選項-a
表示顯示全部鏈接狀態信息
選項-t
表示顯示TCP sockets
選項-u
表示顯示UDP sockets
選項-n
表示不轉換數字爲服務名
選項-p
表示顯示進程
[root@centos7 ~]# ss -antp|column -t State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* users:(("sshd",pid=1355,fd=3)) LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=2214,fd=13)) ESTAB 0 0 10.0.1.254:22 192.168.78.141:50332 users:(("sshd",pid=18294,fd=3)) ESTAB 0 52 10.0.1.254:22 192.168.78.178:51667 users:(("sshd",pid=18433,fd=3)) LISTEN 0 128 :::5000 :::* users:(("exe",pid=5908,fd=7)) LISTEN 0 128 :::22 :::* users:(("sshd",pid=1355,fd=4)) LISTEN 0 100 ::1:25 :::* users:(("master",pid=2214,fd=14))
選項-l
表示只顯示監聽狀態的sockets
[root@centos7 ~]# ss -lt|column -t State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:ssh *:* LISTEN 0 100 127.0.0.1:smtp *:* LISTEN 0 128 :::commplex-main :::* LISTEN 0 128 :::ssh :::* LISTEN 0 100 ::1:smtp :::*
選項-s
表示顯示彙總信息
[root@centos7 ~]# ss -s Total: 270 (kernel 575) TCP: 8 (estab 1, closed 1, orphaned 0, synrecv 0, timewait 0/0), ports 0 Transport Total IP IPv6 * 575 - - RAW 2 0 2 UDP 0 0 0 TCP 7 3 4 INET 9 3 6 FRAG 0 0 0
還可使用state STATE-FILTER [EXPRESSION]
指定過濾格式
如顯示源或目的端口爲8080,狀態爲established
的鏈接:
ss state established '( dport = :8080 or sport = :8080 )'
如來自193.233.7/24,狀態爲fin-wait-1的http或https鏈接
ss state fin-wait-1 '( sport = :http or sport = :https )' dst 193.233.7/24
journalctl
journalctl [OPTIONS...] [MATCHES...]
在基於systemd
的系統中,可使用一個新工具Journal
代替原來的系統服務Syslog
來記錄日誌。關於Journal
優越性就不在這裏敘述了,咱們來直接看它怎麼使用。
Journal
服務的配置文件是/etc/systemd/journald.conf
,在默認配置中,Journal
日誌保存在目錄/run/log/journal
內(tmpfs內存文件系統),系統重啓將不會保留,能夠手動將日誌刷到(經過命令journalctl --flush
)磁盤文件系統上(/var/log/journal
內)。
Journal
服務隨系統啓動而啓動,默認會記錄從開機到關機全過程的內核和應用程序日誌
#查看服務狀態 [root@centos7 ~]# systemctl status -l systemd-journald ● systemd-journald.service - Journal Service Loaded: loaded (/usr/lib/systemd/system/systemd-journald.service; static; vendor preset: disabled) Active: active (running) since 二 2016-12-20 11:15:22 CST; 1 weeks 0 days ago Docs: man:systemd-journald.service(8) man:journald.conf(5) Main PID: 539 (systemd-journal) Status: "Processing requests..." CGroup: /system.slice/systemd-journald.service └─539 /usr/lib/systemd/systemd-journald 12月 20 11:15:22 centos7 systemd-journal[539]: Runtime journal is using 8.0M (max allowed 391.1M, trying to leave 586.7M free of 3.8G available → current limit 391.1M). 12月 20 11:15:22 centos7 systemd-journal[539]: Runtime journal is using 8.0M (max allowed 391.1M, trying to leave 586.7M free of 3.8G available → current limit 391.1M). 12月 20 11:15:22 centos7 systemd-journal[539]: Journal started 12月 20 11:15:22 centos7 systemd-journal[539]: Runtime journal is using 8.0M (max allowed 391.1M, trying to leave 586.7M free of 3.8G available → current limit 391.1M).
當命令journalctl
不帶任何選項時會分頁顯示系統的全部日誌(從本次開機到如今時間)
選項-k
表示顯示內核kernel日誌
選項-u UNIT
表示顯示指定服務單元UNIT的日誌
#如上一篇中配置的計時器(ping252.timer)和服務(ping252.service)日誌 [root@centos7 ~]# journalctl -u ping252.timer -- Logs begin at 二 2016-12-20 11:15:19 CST, end at 二 2016-12-27 20:39:54 CST. -- 12月 23 14:27:26 centos7 systemd[1]: Started ping 252 every 30s. 12月 23 14:27:26 centos7 systemd[1]: Starting ping 252 every 30s. 12月 23 14:36:57 centos7 systemd[1]: Stopped ping 252 every 30s. .... [root@centos7 ~]# journalctl -u ping252 -- Logs begin at 二 2016-12-20 11:15:19 CST, end at 二 2016-12-27 20:41:34 CST. -- 12月 23 14:28:28 centos7 systemd[1]: Started ping 252. 12月 23 14:28:28 centos7 systemd[1]: Starting ping 252... 12月 23 14:28:28 centos7 systemd[11428]: Failed at step EXEC spawning /root/temp/ping252.sh: Exec format error 12月 23 14:28:28 centos7 systemd[1]: ping252.service: main process exited, code=exited, status=203/EXEC 12月 23 14:28:28 centos7 systemd[1]: Unit ping252.service entered failed state. 12月 23 14:28:28 centos7 systemd[1]: ping252.service failed. 12月 23 14:29:03 centos7 systemd[1]: Started ping 252. ....
選項-r
表示反向輸出日誌(從當前時間到本次開機)
選項-n N
表示輸出最新的N行日誌
[root@centos7 ~]# journalctl -n 5 -u ping252 -- Logs begin at 二 2016-12-20 11:15:19 CST, end at 二 2016-12-27 20:48:54 CST. -- 12月 23 17:27:12 centos7 systemd[1]: Starting 252... 12月 23 17:29:12 centos7 systemd[1]: Started 252. 12月 23 17:29:12 centos7 systemd[1]: Starting 252... 12月 23 17:31:12 centos7 systemd[1]: Started 252. 12月 23 17:31:12 centos7 systemd[1]: Starting 252...
選項-f
表示顯示最新的10行日誌並繼續等待輸出新日誌(相似於命令tail -f
)
選項-p n
表示過濾輸出指定級別的日誌,其中n的值能夠是:
0 表示 emerg 1 表示 alert 2 表示 crit 3 表示 err 4 表示 warning 5 表示 notice 6 表示 info 7 表示 debug
如
[root@centos7 ~]# journalctl -u ping252 -p 3 -- Logs begin at 二 2016-12-20 11:15:19 CST, end at 二 2016-12-27 21:13:34 CST. -- 12月 23 14:28:28 centos7 systemd[11428]: Failed at step EXEC spawning /root/temp/ping252.sh: Exec format error 12月 23 14:29:03 centos7 systemd[11442]: Failed at step EXEC spawning /root/temp/ping252.sh: Exec format error 12月 23 14:30:32 centos7 systemd[11452]: Failed at step EXEC spawning /root/temp/ping252.sh: Exec format error
選項--since=
和--until=
表示顯示晚於指定時間(--since=)的日誌、顯示早於指定時間(--until=)的日誌。時間格式如上一篇systemd.timer
所示:
[root@centos7 ~]# journalctl -u ping252 --since "2016-12-20 11:15:19" --until "now" -p 3 -- Logs begin at 二 2016-12-20 11:15:19 CST, end at 二 2016-12-27 21:37:14 CST. -- 12月 23 14:28:28 centos7 systemd[11428]: Failed at step EXEC spawning /root/temp/ping252.sh: Exec format error 12月 23 14:29:03 centos7 systemd[11442]: Failed at step EXEC spawning /root/temp/ping252.sh: Exec format error 12月 23 14:30:32 centos7 systemd[11452]: Failed at step EXEC spawning /root/temp/ping252.sh: Exec format error
選項--disk-usage
表示顯示日誌磁盤佔用量
[root@centos7 ~]# journalctl --disk-usage Archived and active journals take up 104.8M on disk.
選項--vacuum-size=
用於設置日誌最大磁盤使用量(值可使用K、M、G、T等後綴)。
選項--vacuum-time=
用於清除指定時間以前的日誌(可使用"s", "m", "h", "days", "weeks", "months", "years" 等後綴)
[root@centos7 ~]# journalctl --vacuum-time="1 days" Deleted archived journal /run/log/journal/9......2e.journal (48.0M). Deleted archived journal /run/log/journal/9......a1.journal (48.8M). Vacuuming done, freed 96.8M of archived journals on disk.
選項-o
表示控制輸出格式,能夠帶一個以下參數:
short 默認格式,和傳統的syslog格式類似,每條日誌一行 short-iso 和short相似,但顯示ISO 8601時間戳 short-precise 和short相似,只是將時間戳字段的秒數精確到微秒級別 short-monotonic 和short相似,只是將時間戳字段的零值從內核啓動時開始計算。 short-unix 和short相似,只是將時間戳字段顯示爲從"UNIX時間原點"(1970-1-1 00:00:00 UTC)以來的秒數。 精確到微秒級別。 verbose 以結構化的格式顯示每條日誌的全部字段。 export 將日誌序列化爲二進制字節流(大部分依然是文本)以適用於備份與網絡傳輸。 json 將日誌項按照JSON數據結構格式化, 每條日誌一行。 json-pretty 將日誌項按照JSON數據結構格式化, 可是每一個字段一行, 以便於人類閱讀。 json-sse 將日誌項按照JSON數據結構格式化,每條日誌一行,可是用大括號包圍。 cat 僅顯示日誌的實際內容, 而不顯示與此日誌相關的任何元數據(包括時間戳)。
firewall-cmd
同iptables
同樣,firewalld
也經過內核的netfilter來實現防火牆功能(netfilter的簡介),比iptables
先進的地方在於,firewalld
能夠動態修改單條規則,而不須要像iptables那樣,在修改了規則以後必須所有刷新才能夠生效。並且firewalld
在使用上更人性化,不須要理解netfilter的原理也能實現大部分功能。
firewalld
須要開啓守護進程,查看防火牆服務狀態:
[root@idc-v-71252 ~]# systemctl status firewalld ● firewalld.service - firewalld - dynamic firewall daemon Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled) Active: active (running) since 三 2016-12-14 14:07:04 CST; 1 weeks 4 days ago Main PID: 898 (firewalld) CGroup: /system.slice/firewalld.service └─898 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid 12月 14 14:07:03 centos7 systemd[1]: Starting firewalld - dynamic firewall daemon... 12月 14 14:07:04 centos7 systemd[1]: Started firewalld - dynamic firewall daemon.
或者經過自身的firewall-cmd
查看
[root@centos7 ~]# firewall-cmd --stat running [root@centos7 ~]#
firewalld
的配置文件以xml格式爲主(主配置文件firewalld.conf除外),它們有兩個存儲位置:
一、/etc/firewalld 二、/usr/lib/firewalld
使用時的規則是這樣的:當須要一個文件時,firewalld
會首先到第一個目錄中查找,若是能夠找到,那麼就直接使用,不然會繼續到第二個目錄中查找。不推薦在目錄/usr/lib/firewalld
中直接修改配置文件,最好是在/usr/lib/firewalld
中複製一份配置文件到/etc/firewalld
的相應目錄中,而後進行修改。這樣,在恢復默認配置時,直接刪除/etc/firewalld
中的文件便可。
firewalld
中引入了兩個概念:service
(服務)和zone
(區域)。
service
通用配置文件(位於目錄/usr/lib/firewalld/services
內)中定義了服務與端口的映射,firewalld
在使用時能夠直接引用服務名而不是像iptables
那樣引用端口號(就像DNS服務將域名和IP地址作了映射);
默認時firewalld
提供了九個zone
配置文件,位於/usr/lib/firewalld/zones
中:
[root@centos7 ~]# ls /usr/lib/firewalld/zones block.xml dmz.xml drop.xml external.xml home.xml internal.xml public.xml trusted.xml work.xml
每一個文件中定義了一套規則集,或者說判斷方案。firewalld
經過判斷配置文件中以下三個地方來決定具體使用哪套方案來過濾包:
一、source 原地址 二、interface 接收包的網卡 三、默認zone(可在/etc/firewalld/firewalld.conf中配置)
這三個優先級按順序依次下降,也就是說若是按照source
能夠找到就不會再按interface
去查找,若是前兩個都找不到纔會使用第三個。
public.xml
內容:
[root@centos7 ~]# cat /usr/lib/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="ssh"/> <service name="dhcpv6-client"/> </zone> [root@centos7 ~]#
zone
配置文件中能夠配置的項包括:
zone 定義zone起始和結束的標籤,只能用於zone配置文件,能夠設置兩個屬性: version 版本 target 本zone的默認規則,包括四個可選值:default、ACCEPT、%%REJECT%%、DROP,若是不設置則表示默認值default,若是默認規則不是default,除source和interface兩個配置項之外的其餘規則項都將被忽略,而直接跳轉到默認規則。 short 區域簡短描述 description 區域描述 interface 綁定一個本地接口到本zone source 綁定一個或一組源地址到本zone service 表示一個服務 port 端口,使用port能夠不經過service而直接對端口進行設置 icmp-block icmp報文阻塞,能夠按照icmp類型進行設置 masquerade ip地址假裝,也就是按照源網卡地址進行NAT轉發 forward-port 端口轉發 rule 自定義規則
firewalld
默認區域中,ACCEPT
用在trusted
區域,%%REJECT%%
用在block
區域,DROP
用在drop
區域。使用時能夠複製一份須要的文件至/etc/firewalld/zones中,而後將須要的源地址或接口配置在相應的文件中。
配置source.
source在zone的xml文件中的格式爲
<zone> <source address="address[/mask]"/> </zone>
須要注意的是相同的source
項只能在一個zone
中進行配置,也就是說同一個源地址只能對應一個zone
,另外,直接編輯xml文件以後須要執行命令firewall-cmd --reload
才能生效。
固然也可使用firewall-cmd
命令進行配置(可選項--permanent
表示是否保存到配置文件中,使用後須要--reload
才能生效;--zone
表示指定zone,不指定表示使用默認zone):
#列出指定zone的全部綁定的source地址 firewall-cmd [--permanent] [--zone=zone] --list-sources #查詢指定zone是否跟指定source地址進行了綁定 firewall-cmd [--permanent] [--zone=zone] --query-source=source[/mask] #將一個source地址綁定到指定的zone(只可綁定一次,第二次綁定到不一樣的zone會報錯) firewall-cmd [--permanent] [--zone=zone] --add-source=source[/mask] #改變source地址所綁定的zone firewall-cmd [--zone=zone] --change-source=source[/mask] #刪除source地址跟zone的綁定 firewall-cmd [--permanent] [--zone=zone] --remove-source=source[/mask]
如將源地址192.168.0.0/16添加至默認zone
[root@centos7 zones]# firewall-cmd --add-source=192.168.0.0/16 success [root@centos7 zones]# firewall-cmd --list-sources 192.168.0.0/16 [root@centos7 zones]# firewall-cmd --remove-source=192.168.0.0/16 success [root@centos7 zones]#
配置interface.
同source配置項相同,同一個interface只能對應一個zone。interface在zone的xml文件中的格式爲:
<zone> <interface name="string"/> </zone>
還能夠將zone
配置在網卡配置文件(ifcfg-*文件)中,使接口綁定到指定zone:
ZONE=public
相應命令:
#列出指定zone的綁定接口 firewall-cmd [--permanent] [--zone=zone] --list-interfaces #綁定接口到指定zone firewall-cmd [--permanent] [--zone=zone] --add-interface=interface #改變接口綁定zone firewall-cmd [--zone=zone] --change-interface=interface #查詢接口是否和指定zone綁定 firewall-cmd [--permanent] [--zone=zone] --query-interface=interface #刪除綁定 firewall-cmd [--permanent] [--zone=zone] --remove-interface=interface
如將ens32移除出默認zone
[root@centos7 zones]# firewall-cmd --list-interfaces ens32 ens33 [root@centos7 zones]# [root@centos7 zones]# firewall-cmd --remove-interface=ens32 success [root@centos7 zones]# firewall-cmd --list-interfaces ens33 [root@centos7 zones]#
配置service
同一個service能夠配置到多個不一樣的zone中
<zone> <service name="string"/> </zone>
相應命令:
firewall-cmd [--permanent] [--zone=zone] --list-services #--timeout=seconds表示生效時間,過時後自動刪除。不能和--permanent一塊兒使用 firewall-cmd [--permanent] [--zone=zone] --add-service=service [--timeout=seconds] firewall-cmd [--permanent] [--zone=zone] --remove-service=service firewall-cmd [--permanent] [--zone=zone] --query-service=service #列出全部可用服務 firewall-cmd --get-service
如增長http服務到默認zone
[root@centos7 zones]# firewall-cmd --add-service=http success [root@centos7 zones]# firewall-cmd --remove-service=http success [root@centos7 zones]#
配置port.
須要同時指定協議和端口號,端口號能夠用-鏈接表示一個範圍。
<zone> <port port="portid[-portid]" protocol="tcp|udp"/> </zone>
命令
firewall-cmd [--permanent] [--zone=zone] --list-ports firewall-cmd [--permanent] [--zone=zone] --add-port=portid[-portid]/protocol [--timeout=seconds] firewall-cmd [--permanent] [--zone=zone] --remove-port=portid[-portid]/protocol firewall-cmd [--permanent] [--zone=zone] --query-port=portid[-portid]/protocol
如限時10秒容許80端口的訪問
[root@centos7 zones]# firewall-cmd --add-port=80/tcp --timeout=10 success [root@centos7 zones]#
配置icmp-block.
<zone> <icmp-block name="string"/> </zone>
string處配置須要阻塞的ICMP類型
命令
#列出全部ICMP類型 firewall-cmd --get-icmptypes firewall-cmd [--permanent] [--zone=zone] --list-icmp-blocks firewall-cmd [--permanent] [--zone=zone] --add-icmp-block=icmptype [--timeout=seconds] firewall-cmd [--permanent] [--zone=zone] --remove-icmp-block=icmptype firewall-cmd [--permanent] [--zone=zone] --query-icmp-block=icmptype
如禁止ping
[root@centos7 zones]# firewall-cmd --add-icmp-block=echo-request success #在另外一臺機器ping本機: [root@idc-v-71252 ~]# ping 10.0.1.254 PING 10.0.1.254 (10.0.1.254) 56(84) bytes of data. From 10.0.1.254 icmp_seq=1 Destination Host Prohibited From 10.0.1.254 icmp_seq=2 Destination Host Prohibited From 10.0.1.254 icmp_seq=3 Destination Host Prohibited ^C #取消 [root@centos7 zones]# firewall-cmd --remove-icmp-block=echo-request success [root@centos7 zones]#
配置masquerade.
<zone> <masquerade/> </zone>
NAT轉發,將接收到的請求的源地址設置爲轉發請求網卡的地址。
命令
firewall-cmd [--permanent] [--zone=zone] --add-masquerade [--timeout=seconds] firewall-cmd [--permanent] [--zone=zone] --remove-masquerade firewall-cmd [--permanent] [--zone=zone] --query-masquerade
配置forward-port.
<zone> <forward-port port="portid[-portid]" protocol="tcp|udp" [to-port="portid[-portid]"] [to-addr="ipv4address"]/> </zone>
命令(其中轉發規則FORWARD
爲port=portid[-portid]:proto=protocol[:toport=portid[-portid]][:toaddr=address[/mask]]
)
firewall-cmd [--permanent] [--zone=zone] --list-forward-ports firewall-cmd [--permanent] [--zone=zone] --add-forward-port=FORWARD [--timeout=seconds] firewall-cmd [--permanent] [--zone=zone] --remove-forward-port=FORWARD firewall-cmd [--permanent] [--zone=zone] --query-forward-port=FORWARD
如將80端口接收到的請求轉發到本機的8080端口(如需轉發至其餘地址則添加:to-addr=address[/mask]
):
[root@centos7 zones]# firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080 success [root@centos7 zones]# firewall-cmd --list-forward-ports port=80:proto=tcp:toport=8080:toaddr= [root@centos7 zones]# firewall-cmd --remove-forward-port=port=80:proto=tcp:toport=8080 success [root@centos7 zones]#
配置rule.
rule
能夠用來定義一條複雜的規則,其在文件中定義以下:
<zone> <rule [family="ipv4|ipv6"]> [ <source address="address[/mask]" [invert="bool"]/> ] [ <destination address="address[/mask]" [invert="bool"]/> ] [ <service name="string"/> | <port port="portid[-portid]" protocol="tcp|udp"/> | <protocol value="protocol"/> | <icmp-block name="icmptype"/> | <masquerade/> | <forward-port port="portid[-portid]" protocol="tcp|udp" [to-port="portid[-portid]"] [to-addr="address"]/> ] [ <log [prefix="prefixtext"] [level="emerg|alert|crit|err|warn|notice|info|debug"]/> [<limit value="rate/duration"/>] </log> ] [ <audit> [<limit value="rate/duration"/>] </audit> ] [ <accept/> | <reject [type="rejecttype"]/> | <drop/> ] </rule> </zone>
這裏的rule
就至關於使用iptables
時的一條規則。
命令
firewall-cmd [--permanent] [--zone=zone] --list-rich-rules firewall-cmd [--permanent] [--zone=zone] --add-rich-rule='rule' [--timeout=seconds] firewall-cmd [--permanent] [--zone=zone] --remove-rich-rule='rule' firewall-cmd [--permanent] [--zone=zone] --query-rich-rule='rule'
如源地址爲192.168.10.0/24的http鏈接都drop掉:
[root@centos7 zones]# firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.10.0/24" service name="http" drop' success [root@centos7 zones]# firewall-cmd --query-rich-rule='rule family="ipv4" source address="192.168.10.0/24" service name="http" drop' yes [root@centos7 zones]# firewall-cmd --remove-rich-rule='rule family="ipv4" source address="192.168.10.0/24" service name="http" drop' success [root@centos7 zones]#
service
配置文件格式爲:
<service [version="string"]> [<short>short description</short>] [<description>description</description>] [<port [port="portid[-portid]"] protocol="protocol"/>] [<module name="helper"/>] [<destination ipv4="address[/mask]" ipv6="address[/mask]"/>] </service>
其中最重要的配置項是port
,表示將端口綁定到指定服務,當該端口收到包時即表示對該服務的請求,防火牆從而到對應的zone中去查找規則,判斷是否放行。
一個service中能夠配置多個port項,單個port項中能夠配置單個端口,也能夠是一個端口段,好比port=80-85表示80到85之間的端口號。
destination
表示根據目的地址綁定服務,能夠是ipv4地址也能夠是ipv6地址,可使用掩碼。module
用來設置netfilter鏈接跟蹤模塊
firewall-cmd
提供了兩個選項用於建立和刪除service,--new-service
和--delete-service
。不過直接編輯xml文件是更好的選擇。
直接使用防火牆的過濾規則,配置文件爲/etc/firewalld/direct.xml
(能夠手動建立或經過命令生成),文件結構以下:
<?xml version="1.0" encoding="utf-8"?> <direct> [ <chain ipv="ipv4|ipv6" table="table" chain="chain"/> ] [ <rule ipv="ipv4|ipv6" table="table" chain="chain" priority="priority"> args </rule> ] [ <passthrough ipv="ipv4|ipv6"> args </passthrough> ] </direct>
能夠在配置文件中直接配置iptables
規則,其中:
ipv 表示ip版本 table 表示iptables中的table chain 表示iptables中的chain,能夠是自定義的 priority 優先級,相似於iptables中規則的先後順序,數字越小優先級越高 args 表示具體規則,也能夠是自定義的chain
如自定義一個叫blacklist的鏈,而後將全部來自192.168.1.0/24和192.168.5.0/24的數據包都指向了這個鏈,指定這個鏈的規則:首先使用'blacklisted: '前綴進行日誌記錄(每分鐘記錄一次),而後drop。
<?xml version="1.0" encoding="utf-8"?> <direct> <chain ipv="ipv4" table="raw" chain="blacklist"/> <rule ipv="ipv4" table="raw" chain="PREROUTING" priority="0">-s 192.168.1.0/24 -j blacklist</rule> <rule ipv="ipv4" table="raw" chain="PREROUTING" priority="1">-s 192.168.5.0/24 -j blacklist</rule> <rule ipv="ipv4" table="raw" chain="blacklist" priority="0">-m limit --limit 1/min -j LOG --log-prefix "blacklisted: "</rule> <rule ipv="ipv4" table="raw" chain="blacklist" priority="1">-j DROP</rule> </direct>
相關命令:
firewall-cmd [--permanent] --direct --get-all-chains firewall-cmd [--permanent] --direct --get-chains { ipv4 | ipv6 | eb } table firewall-cmd [--permanent] --direct --add-chain { ipv4 | ipv6 | eb } table chain firewall-cmd [--permanent] --direct --remove-chain { ipv4 | ipv6 | eb } table chain firewall-cmd [--permanent] --direct --query-chain { ipv4 | ipv6 | eb } table chain firewall-cmd [--permanent] --direct --get-all-rules firewall-cmd [--permanent] --direct --get-rules { ipv4 | ipv6 | eb } table chain firewall-cmd [--permanent] --direct --add-rule { ipv4 | ipv6 | eb } table chain priority args firewall-cmd [--permanent] --direct --remove-rule { ipv4 | ipv6 | eb } table chain priority args firewall-cmd [--permanent] --direct --remove-rules { ipv4 | ipv6 | eb } table chain firewall-cmd [--permanent] --direct --query-rule { ipv4 | ipv6 | eb } table chain priority args firewall-cmd --direct --passthrough { ipv4 | ipv6 | eb } args firewall-cmd --permanent --direct --get-all-passthroughs firewall-cmd --permanent --direct --get-passthroughs { ipv4 | ipv6 | eb } firewall-cmd --permanent --direct --add-passthrough { ipv4 | ipv6 | eb } args firewall-cmd --permanent --direct --remove-passthrough { ipv4 | ipv6 | eb } args firewall-cmd --permanent --direct --query-passthrough { ipv4 | ipv6 | eb } args
上述例子轉化成命令即爲:
firewall-cmd --permanent --direct --add-chain ipv4 raw blacklist firewall-cmd --permanent --direct --add-rule ipv4 raw PREROUTING 0 -s 192.168.1.0/24 -j blacklist firewall-cmd --permanent --direct --add-rule ipv4 raw PREROUTING 1 -s 192.168.5.0/24 -j blacklist firewall-cmd --permanent --direct --add-rule ipv4 raw blacklist 0 -m limit --limit 1/min -j LOG --log-prefix "blacklisted: " firewall-cmd --permanent --direct --add-rule ipv4 raw blacklist 1 -j DROP #重載生效 firewall-cmd --reload
在實際生產環境中若是防火牆規則只是由root設定的話,最好將firewall-cmd
(此文件爲python腳本)的權限限制爲只有root能執行:
[root@centos7 ~]# ls -l /usr/bin/firewall-cmd -rwxr-xr-x. 1 root root 62012 11月 20 2015 /usr/bin/firewall-cmd [root@centos7 ~]# file /usr/bin/firewall-cmd /usr/bin/firewall-cmd: Python script, ASCII text executable [root@centos7 ~]# chmod 750 /usr/bin/firewall-cmd
關於firewalld
的更多內容請查看相關文檔
至此,linux基礎命令介紹系列就結束了。先後十五篇文章,記錄了百餘個經常使用命令。以後將開啓新的系列:shell編程。