linux網絡相關 |防火墻 |netfilter5表5鏈
ifconfig 如果沒有ifconfig,需要安裝包
[root@centos-01 ~]# yum install net-tools
如果需要顯示所有的網卡信息,包括down掉的或者沒有IP地址的網絡,使用-a命令
[root@centos-01 ~]# ifconfig -a
有時候會單獨針對一個網卡做一些更改(如改網關,或者增加DNS),但是不想把所有的網卡都重啟,只需要重啟指定的網卡,為了避免down掉後無法啟動,我們需要2個命令一起執行
[root@centos-01 ~]# ifdown ens33 && ifup ens33 成功斷開設備 ‘ens33‘。 成功激活的連接(D-Bus 激活路 徑:/org/freedesktop/NetworkManager/ActiveConnection/1)
設定虛擬網卡
1、切換至網卡配置文件
[root@centos-01 ~]# cd /etc/sysconfig/network-scripts
2、復制網卡
[root@centos-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:0
3、編輯配置文件
[root@centos-01 network-scripts]# vi !$ TYPE=Ethernet BOOTPROTO=dhcp DEFROUTE=yes PEERDNS=yes PEERROUTES=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_FAILURE_FATAL=no IPV6_ADDR_GEN_MODE=stable-privacy NAME=ens33:0 UUID=584a55df-e887-4d4e-a11a-2ea0b5f6f5ed DEVICE=ens33:0 ONBOOT=YES IPADDR=192.168.187.150 NETMASK=255.255.255.0
說明:更改了NAME、DEVICE、IPADDR,刪除了DNS和網關,之前已經設定過。
4、重啟系統網卡
[root@centos-01 network-scripts]# ifdown ens33 && ifup ens33
成功斷開設備 ‘ens33‘。
成功激活的連接(D-Bus 激活路徑:/org/freedesktop/NetworkManager/ActiveConnection/2)
5、查看網卡信息
[root@centos-01 network-scripts]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.187.130 netmask 255.255.255.0 broadcast 192.168.187.255 inet6 fe80::9b87:5d89:4bd9:8e53 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:02:6a:c4 txqueuelen 1000 (Ethernet) RX packets 3124 bytes 244651 (238.9 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 689 bytes 85729 (83.7 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.187.150 netmask 255.255.255.0 broadcast 192.168.187.255 ether 00:0c:29:02:6a:c4 txqueuelen 1000 (Ethernet) lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 1 (Local Loopback) RX packets 20 bytes 1700 (1.6 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 20 bytes 1700 (1.6 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
6、檢測虛擬網卡IP是否能ping通 在cmd中ping一下就可以
查看網卡是否連接
方法1:mii-tool [網卡名]
[root@centos-01 ~]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok
查看:link ok說明網卡連接OK!
方法2:ethtool [網卡名]
[root@centos-01 ~]# ethtool ens33
Settings for ens33:
Supported ports: [ TP ]
Supported link modes: 10baseT/Half 10baseT/Full
100baseT/Half 100baseT/Full
……
Wake-on: d
Current message level: 0x00000007 (7)
drv probe link
Link detected: yes
查看:Link detected: yes說明網卡連接OK!
更改主機名
更改:
[root@centos-01 ~]# hostnamectl set-hostname aminglinux-001
查看主機名:
[root@centos-01 ~]# hostname
aminglinux-001
不重啟的時候更改主機名
[root@centos-01 ~]# bash
[root@aminglinux-001 ~]# exit
exit
DNS配置
DNS配置文件:/etc/resolv.conf
[root@centos-01 ~]# cat /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 192.168.187.2
註: 更改網卡配置即可更改DNS配置文件,更改後需要重啟網卡(ifdown/ifup)後生效,也可編輯‘/etc/resolv.conf’臨時更改DNS配置,該辦法在重啟網卡後會被網卡配置文件中的DNS覆蓋!
本地域名配置文件文件:/etc/hosts
[root@centos-01 ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
說明: 使用vi命令可在該配置文件下可以自定義IP所對應的域名(一個IP對應多個域名或一個域名對應多個IP,用空格隔開,當一個域名對應多個IP時,以配置文件中靠後面的配置為準),但是該域名配置只在本機生效!
10.12 firewalld和netfilter
永久關閉防火墻
[root@aminglinux-001 ~]# vi etc/setlinux/config
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
# enforcing - SELinux security policy is enforced.
# permissive - SELinux prints warnings instead of enforcing.
# disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three two values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
將SELINUX=enforcing改為disabled保存,重啟系統即可!
查看防火墻狀態
[root@aminglinux-001 ~]# getenforce
Enforcing
臨時關閉防火墻
[root@aminglinux-001 ~]# setenforce 0
[root@aminglinux-001 ~]# getenforce
Permissive
Centos7中的防火墻叫firewalld,而7之前的防火墻叫netfilter.內部的工具iptables是一樣的(可以打開或者關閉端口)
停用firewalld
關閉防火墻
[root@aminglinux-001 ~]# systemctl disable firewalld
Removed symlink /etc/systemd/system/dbus- org.fedoraproject.FirewallD1.service.
Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service.
停止服務
[root@aminglinux-001 ~]# systemctl stop firewalld
開啟netfilter
開啟前先安裝iptables工具包:
[root@aminglinux-001 ~]# yum install -y iptables-services
開啟iptables服務:
[root@aminglinux-001 ~]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@aminglinux-001 ~]# systemctl start iptables
查看iptables規則:iptables -nvL
[root@aminglinux-001 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
37 2432 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
25 1450 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT 21 packets, 2204 bytes)
pkts bytes target prot opt in out source destination
10.13 netfilter5表5鏈介紹
表名
filter:包過濾,用於防火墻規則。 nat:地址轉換,用於網關路由器。 mangle:用於給數據包打標記,然後根據標記去操作那些表。(不常用) 還有兩個不常用的表:raw和security,在此不多講述。 規則鏈名
filter的三種鏈:
INPUT鏈:作用於輸入本機的數據包。 OUTPUT鏈:作用於本機輸出的數據包。 FORWARD鏈:作用於與本機無關的包。 nat的三種鏈:
PREROUTING鏈:作用是在包剛剛到達防火墻時改變它的目的地址,如果需要的話。 OUTPUT鏈:改變本地產生的包的目的地址。 POSTROUTING鏈:在包離開防火墻之前改變其源地址。 nat表應用(了解內容)
環境: 假設有三臺機器(A,B,C),A機器上有一塊網卡連接公網IP1;B機器上有兩塊網卡b1、b2,b1、b2分別連接公網和私網IP2,、IP3;C機器上有一塊網卡連接私網IP4。很明顯此時A&B,B&C之間都分別能互通,但是A和C之間是不能直接連通的,只能借助B做媒介才能連通。那麽如何設置才能使得A和C之間互通呢?
方法:
先打開路由轉發功能:
[root@aminglinux-001 ~]# echo "1" > /proc/sys/net/ipv4_forward
然後對nat表做IP轉發配置:
[root@aminglinux-001 ~]# iptables -t nat -A POSTROUTING -s IP2(此處IP在同一個網段即可) -o ensA(A機器的網卡名稱) -j MASQUERADE
說明: -o 選項後面跟設備名稱,表示出口網卡,MASQUERADE是偽裝、冒充的意思。
10.14 iptables語法
iptables命令是Linux上常用的防火墻軟件,是netfilter項目的一部分。可以直接配置,也可以通過許多前端和圖形界面配置。
語法: iptables [options] [參數]
Options:
-n:不針對IP反解析主機名
-v:顯示更詳細的信息
-t:指定表(iptables命令默認作用於filter表)
-L:顯示信息
-F:清空所有規則
-A/D:=add/delete,添加/刪除一條規則
-I:插入一條規則
-p:指定協議,可以是tcp,udp或icmp --sport:跟-p一起使用,指定源端口
--dport:跟-p一起使用,指定目標端口
-s:指定源IP(可以是一個IP段)
-d:指定目的IP(可以是一個IP段)
-j:後面跟動作(ACCEPT表示允許包;DROP表示丟掉包;REJECT表示拒絕包)
-i:指定網卡
-Z:把包以及流量計數器清零
-P:=pre,預設策略
-
iptables命令選項輸入順序:
iptables -t 表名 <-A/I/D/R> 規則鏈名 [規則號] <-i/o 網卡名> -p 協議名 <-s 源IP/源子網> --sport 源端口 <-d 目標IP/目標子網> --dport 目標端口 -j 動作
-
查看規則
[root@aminglinux-001 ~]# iptables -nvL
iptables規則配置文件:/etc/sysconfig/iptables -
清空規則
[root@aminglinux-001 ~]# iptables -F
註: 該命令不會清除配置文件內的規則!當更改規則後需要執行命令‘service iptables save’將其保存到配置文件。 -
增加一條規則(-A/I)
[root@aminglinux-001 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
說明: 增加一條規則,當IP192.168.188.1、協議為tcp、端口為‘1234’的向IP192.168.188.128、端口為80的機器發送包時執行操作:drop(丟掉包)。 註: 該命令也可以把-A換成-I,兩者的區別類似於排隊和插隊,兩種方法插入的規則優先級不同。 -
刪除一條規則(-D)
-
方法1: 知道規則的內容
[root@aminglinux-001 ~]# iptables -D INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
註: 要刪除一條規則時,必須和插入的規則一致,也就是說,兩條iptables命令除了-A/I和-D不一樣外,其他地方都一樣。 -
方法2: 忘記規則內容
首先所以用以下命令查看規則序號:[root@aminglinux-001 ~]# iptables -nvL --line-number
然後再執行刪除命令:[root@aminglinux-001 ~]# iptables -D INPUT [序號]
更改預設策略(-P)
執行命令:
#iptables -P OUTPUT DROP
註意: 盡量不要隨意更改該配置,尤其是在進行遠程登錄時,一旦執行該命令後將會斷開連接。這個策略設定後只能用命令:‘iptables -P OUTPUT ACCEPT’來恢復成原始狀態,不能使用-F參數。
linux網絡相關 |防火墻 |netfilter5表5鏈