若是系統沒有該命令可使用yum安裝:前端
[root@cham2 ~]#yum install -y net-tools
查看網卡IP還可使用命令:ip addr,這個顯示有點亂!linux
顯示全部網卡信息(包括down掉的或者沒有IP地址的網卡)centos
應用環境:更改單個指定網卡配置後須要重啓才能生效,爲了不關閉或重啓全部網卡能夠對單個網卡執行該命令。bash
[root@cham2 ~]# ifdown ens33 && ifup ens33 成功斷開設備 'ens33'。 成功激活的鏈接(D-Bus 激活路徑:/org/freedesktop/NetworkManager/ActiveConnection/6)
注: 若是網卡正在使用中,不要單獨使用ifdown命令!!!解決辦法: 「# ifdown ens33 && ifup ens33」,使兩個命令一塊兒執行!網絡
[root@cham2 ~]# cd /etc/sysconfig/network-scripts/ [root@cham2 network-scripts]# ls ifcfg-ens33 ifdown-ippp ifdown-sit ifup-bnep ifup-plip ifup-Team network-functions-ipv6 ifcfg-lo ifdown-ipv6 ifdown-Team ifup-eth ifup-plusb ifup-TeamPort ifdown ifdown-isdn ifdown-TeamPort ifup-ib ifup-post ifup-tunnel ifdown-bnep ifdown-post ifdown-tunnel ifup-ippp ifup-ppp ifup-wireless ifdown-eth ifdown-ppp ifup ifup-ipv6 ifup-routes init.ipv6-global ifdown-ib ifdown-routes ifup-aliases ifup-isdn ifup-sit network-functions [root@cham2 network-scripts]# cp ifcfg-ens33 ifcfg-ens37 [root@cham2 network-scripts]# vi !$ vi ifcfg-ens33\:0 TYPE=Ethernet BOOTPROTO=static 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=ad4d8fdd-1916-47fb-a43e-ee8685229fc4 DEVICE=ens33:0 ONBOOT=yes IPADDR=192.168.230.150 NETMASK=255.255.255.0 ~ ~ 說明:更改NAME、DEVICE、IPADDR,系統已經設定網關可DNS,因此在此能夠刪除GATEWAY和DNS,也能夠保留,可是,若是要保留該選項則必須保持其與系統網卡配置一致! [root@cham2 network-scripts]# ifdown ens33 && ifup ens33 成功斷開設備 'ens33'。 成功激活的鏈接(D-Bus 激活路徑:/org/freedesktop/NetworkManager/ActiveConnection/7) [root@cham2 network-scripts]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.230.135 netmask 255.255.255.0 broadcast 192.168.230.255 inet6 fe80::6f15:52d3:ebeb:e193 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:b6:9f:e3 txqueuelen 1000 (Ethernet) RX packets 100550 bytes 46379597 (44.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 112074 bytes 40382050 (38.5 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.230.150 netmask 255.255.255.0 broadcast 192.168.230.255 ether 00:0c:29:b6:9f:e3 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 128 bytes 10604 (10.3 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 128 bytes 10604 (10.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@cham2 network-scripts]# 檢測虛擬網啊IP是否通暢 在Windows系統cmd下ping該IP便可!
查看網卡是否鏈接less
[root@cham2 ~]# mii-tool ens33 ens33: negotiated 1000baseT-FD flow-control, link ok 查看:link ok說明網卡鏈接OK! [root@cham2 ~]# ethtool ens33 Settings for ens33: Supported ports: [ TP ] Supported link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Supported pause frame use: No Supports auto-negotiation: Yes Advertised link modes: 10baseT/Half 10baseT/Full 100baseT/Half 100baseT/Full 1000baseT/Full Advertised pause frame use: No Advertised auto-negotiation: Yes Speed: 1000Mb/s Duplex: Full Port: Twisted Pair PHYAD: 0 Transceiver: internal Auto-negotiation: on MDI-X: off (auto) Supports Wake-on: d Wake-on: d Current message level: 0x00000007 (7) drv probe link Link detected: yes 查看:Link detected: yes說明網卡鏈接OK!
更改: [root@cham2 ~]# hostnamectl set-hostname cham002 查看主機名: [root@cham2 ~]# hostname cham002 更改後的主機名要在系統重啓以後纔會顯示! 主機名的配置文件: [root@cham002 ~]# cat /etc/hostname cham002 變成主機名,也能夠從新登陸 [root@cham2 ~]# bash [root@cham002 ~]#
[root@cham002 ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 119.29.29.29 [root@cham002 ~]# vi /etc/sysconfig/network-scripts/ifcfg-ens33 添加DNS2=8.8.8.8 [root@cham002 ~]# ifdown ens33 && ifup ens33 成功斷開設備 'ens33'。 成功激活的鏈接(D-Bus 激活路徑:/org/freedesktop/NetworkManager/ActiveConnection/8) [root@cham002 ~]# cat /etc/resolv.conf # Generated by NetworkManager nameserver 119.29.29.29 nameserver 8.8.8.8
注: 更改網卡配置便可更改DNS配置文件,更改後須要重啓網卡(ifdown/ifup)後生效,也可編輯‘/etc/resolv.conf’臨時更改DNS配置,該辦法在重啓網卡後會被網卡配置文件中的DNS覆蓋!dom
本地域名配置文件文件:/etc/hoststcp
[root@cham002 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 [root@cham002 ~]# vi /etc/hosts 增長一行192.168.230.150 www.qq123.com [root@cham002 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.168.230.150 www.qq123.com [root@cham002 ~]# !ping ping www.qq123.com PING www.qq123.com (192.168.230.150) 56(84) bytes of data. 64 bytes from www.qq123.com (192.168.230.150): icmp_seq=1 ttl=64 time=0.042 ms 64 bytes from www.qq123.com (192.168.230.150): icmp_seq=2 ttl=64 time=0.034 ms 64 bytes from www.qq123.com (192.168.230.150): icmp_seq=3 ttl=64 time=0.033 ms 64 bytes from www.qq123.com (192.168.230.150): icmp_seq=4 ttl=64 time=0.089 ms ^C --- www.qq123.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3000ms rtt min/avg/max/mdev = 0.033/0.049/0.089/0.024 ms
說明: 使用vi命令可在該配置文件下能夠自定義IP所對應的域名(一個IP對應多個域名或一個域名對應多個IP,用空格隔開,當一個域名對應多個IP時,以配置文件中靠後面的配置爲準),可是該域名配置只在本機生效!工具
臨時關閉SELinux防火牆:oop
[root@cham002 ~]# setenforce 0 setenforce: SELinux is disabled
永久關閉SELinux防火牆:
編輯配置文件‘/etc/selinux/config’
將SELINUX=enforcing改成disabled保存,重啓系統便可!
# 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防火牆狀態
[root@cham002 ~]# getenforce Disabled
>Centos7中默認將原來(centos5/6)的防火牆netfileter升級爲了firewalld。iptables是它們實現防火牆功能的工具。
爲了方便學習,暫時停用firewalld,開啓centos6/5的防火牆機制netfilter。
一、關閉firewalld(禁止開機啓動) [root@cham002 ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service. Removed symlink /etc/systemd/system/basic.target.wants/firewalld.service. 二、中止firewalld服務 [root@cham002 ~]# systemctl stop firewalld
開啓前先安裝iptables工具包: [root@cham002 ~]# yum install -y iptables-services 已加載插件:fastestmirror 開啓iptables服務: [root@cham002 ~]# systemctl enable iptables Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service. [root@cham002 ~]# systemctl start iptables
說明: 安裝完成後默認開啓iptables服務。
[root@cham002 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 29 1940 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 0 0 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 17 packets, 1508 bytes) pkts bytes target prot opt in out source destination
filter的三種鏈:
nat的三種鏈:
環境:
假設有三臺機器(A,B,C),A機器上有一塊網卡鏈接公網IP1;B機器上有兩塊網卡b一、b2,。b一、b2分別鏈接公網和私網IP2,、IP3;C機器上有一塊網卡鏈接私網IP4。很明顯此時A&B,B&C之間都分別能互通,可是A和C之間是不能直接連通的,只能藉助B作媒介才能連通。那麼如何設置才能使得A和C之間互通呢?
>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,預設策略
service iptables restart重啓服務
[root@cham002 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 28 1848 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 0 0 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 15 packets, 1428 bytes) pkts bytes target prot opt in out source destination [root@cham002 ~]# iptables -t nat -nvL Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination
iptables規則配置文件:/etc/sysconfig/iptables
[root@cham002 ~]# iptables -F
注: 該命令不會清除配置文件內的規則!當更改規則後須要執行命令‘service iptables save’將其保存到配置文件。
[root@cham002 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -jDROP [root@cham002 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 304 20360 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 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited 0 0 DROP tcp -- * * 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80 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 23 packets, 2324 bytes) pkts bytes target prot opt in out source destination [root@cham002 ~]# iptables -I INPUT -p tcp --dport 80 -j DROP [root@cham002 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 0 0 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80 532 37656 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 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited 0 0 DROP tcp -- * * 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80 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 17 packets, 1508 bytes) pkts bytes target prot opt in out source destination
說明: 增長一條規則,當IP~192.168.188.一、協議爲tcp、端口爲‘1234’的向IP~192.168.188.12八、端口爲80的機器發送包時執行操做:drop(丟掉包)。
注: 該命令也能夠把-A換成-I,二者的區別相似於排隊和插隊,兩種方法插入的規則優先級不一樣。
[root@cham002 ~]# iptables -D INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP[root@cham002 ~]# iptables -nvL Chain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination 918 70764 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 2 473 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 4 packets, 720 bytes) pkts bytes target prot opt in out source destination
注: 要刪除一條規則時,必須和寫入的規則一致,也就是說,兩條iptables命令除了-A/I和-D不同外,其餘地方都同樣。
[root@cham002 ~]# iptables -nvL --line-number Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 1018 77688 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 2 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 3 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 4 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 5 2 473 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited 6 0 0 DROP tcp -- * * 192.168.188.1 192.168.188.128 tcp spt:1234 dpt:80 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 41 packets, 4984 bytes) num pkts bytes target prot opt in out source destination [root@cham002 ~]# iptables -D INPUT 6 [root@cham002 ~]# iptables -nvL --line-number Chain INPUT (policy ACCEPT 0 packets, 0 bytes) num pkts bytes target prot opt in out source destination 1 1083 82112 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED 2 0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0 3 0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0 4 0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22 5 2 473 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) num pkts bytes target prot opt in out source destination 1 0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited Chain OUTPUT (policy ACCEPT 5 packets, 572 bytes) num pkts bytes target prot opt in out source destination
執行命令:
# iptables -P OUTPUT DROP
結果:
注意: 儘可能不要隨意更改該配置,尤爲是在進行遠程登陸時,一旦執行該命令後將會斷開鏈接。這個策略設定後只能用命令:‘iptables -P OUTPUT ACCEPT’來恢復成原始狀態,不能使用-F參數。