linux route 刷新_linux基础命令介绍十五:推陈出新
本文介紹ip、ss、journalctl和firewall-cmd,它們旨在代替linux中原有的一些命令或服務。
1、ip
ip [OPTIONS] OBJECT COMMAND
ip是iproute2軟件包里面的一個強大的網絡配置工具,它能夠替代一些傳統的網絡管理工具,例如ifconfig、route等,使用權限為超級用戶。
OPTIONS是修改ip行為或改變其輸出的選項。
OBJECT是要獲取信息的對象。包括:
address 表示設備的協議(IPv4或IPv6)地址
link 表示網絡設備
monitor 表示監控網絡連接信息
neighbour 表示管理ARP緩存表
netns 表示管理網絡命名空間
route 表示路由表接口
tunnel 表示IP隧道
....
對象名可以是全稱或簡寫格式,比如address可以簡寫為addr或a。
COMMAND設置針對指定對象執行的操作,它和對象的類型有關。
address
如顯示網卡ens33的信息:
[root@centos7 ~]# ip addr show ens33
3: ens33: 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表示輸出更多的信息
[root@centos7 ~]# ip -s addr show ens33
3: ens33: 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增加一個新地址
[root@centos7 ~]# ip addr add 192.168.0.193/24 dev ens33
[root@centos7 ~]# ip a sh ens33
3: ens33: 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
neighbour
如查看arp表項(neighbour可以簡寫為neigh或n)
[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
link
如更改ens33的MTU(最大傳輸單元)的值為1600
[root@centos7 ~]# ip link set dev ens33 mtu 1600
[root@centos7 ~]# ip link show dev ens33
3: ens33: 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: 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: 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
route
如顯示路由表(這里使用了命令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
2、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
3、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 僅顯示日志的實際內容, 而不顯示與此日志相關的任何元數據(包括時間戳)。
4、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除外),它們有兩個存儲位置:
1、/etc/firewalld
2、/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通過判斷配置文件中如下三個地方來決定具體使用哪套方案來過濾包:
1、source 原地址
2、interface 接收包的網卡
3、默認zone(可在/etc/firewalld/firewalld.conf中配置)
這三個優先級按順序依次降低,也就是說如果按照source可以找到就不會再按interface去查找,如果前兩個都找不到才會使用第三個。
zone
public.xml內容:
[root@centos7 ~]# cat /usr/lib/firewalld/zones/public.xml
Public
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.
[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文件中的格式為
需要注意的是相同的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配置在網卡配置文件(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中
相應命令:
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.
需要同時指定協議和端口號,端口號可以用-連接表示一個范圍。
命令
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.
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.
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.
命令(其中轉發規則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可以用來定義一條復雜的規則,其在文件中定義如下:
[ ]
[ ]
[
|
|
|
|
|
]
[ [] ]
[ [] ]
[ | | ]
這里的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配置文件格式為:
[short description]
[description]
[]
[]
[]
其中最重要的配置項是port,表示將端口綁定到指定服務,當該端口收到包時即表示對該服務的請求,防火墻從而到對應的zone中去查找規則,判斷是否放行。
一個service中可以配置多個port項,單個port項中可以配置單個端口,也可以是一個端口段,比如port=80-85表示80到85之間的端口號。
destination表示根據目的地址綁定服務,可以是ipv4地址也可以是ipv6地址,可以使用掩碼。
module用來設置netfilter連接跟蹤模塊
firewall-cmd提供了兩個選項用于創建和刪除service,--new-service和--delete-service。不過直接編輯xml文件是更好的選擇。
direct
直接使用防火墻的過濾規則,配置文件為/etc/firewalld/direct.xml(可以手動創建或通過命令生成),文件結構如下:
[ ]
[ args ]
[ args ]
可以在配置文件中直接配置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。
-s 192.168.1.0/24 -j blacklist
-s 192.168.5.0/24 -j blacklist
-m limit --limit 1/min -j LOG --log-prefix "blacklisted: "
-j DROP
相關命令:
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編程。
總結
以上是生活随笔為你收集整理的linux route 刷新_linux基础命令介绍十五:推陈出新的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: flash大作业一分钟源文件_初中生写作
- 下一篇: linux系统安装金蝶_linux 操作