10.11 Linux網絡相關html
10.12 firewalld和netfilterlinux
10.13 netfilter5表5鏈介紹shell
10.14 iptables語法vim
(拓展:末尾有端口映射,jpg)windows
'centos
10.11 Linux網絡相關:服務器
~1. ifconfig 查看網卡ip (yum install net-tools)網絡
centos7默認沒有ifconfig這個命令,須要安裝。ifconfig查看起來更加清爽dom
可是能夠用ip addr 來查看tcp
ifconfig -a 當你的網卡down掉的時候,或者沒有ip的時候,ifconfig是不顯示的。加上-a就能夠顯示
~2.ifup ens33/ifdown ens33
ifdown ens33 ens33這個網卡斷掉(也叫down掉)
ifup ens33 down掉以後,ifup從新鏈接
用在增長一個網卡或者更改一個網關,但不想重啓所有的網絡服務,就能夠用ifdown ifup指定一個來用。遠程鏈接的時候不要ifdown掉,可是能夠這樣用ifdown ens33 && ifup ens33
~3. 設定虛擬網卡ens33:1
後面lvs keepalived的時候,會用到這個虛擬網卡
操做步驟詳見實例3.
~4.mii-tool ens33 查看網卡是否鏈接
咱們鏈接了網線,不在機房(可看燈有沒有亮),遠程鏈接的時候,檢查是否網卡有沒有插入網線
mii-tool ens33 結果是否是link ok
~5. ethtool ens33 也能夠查看網卡是否鏈接
若是沒有mii-tool能夠用 ethtool ens33 ,檢查最後一行是否是Link detected: yes
~6.更改主機名 hostnamectl set-hostname aminglinux
centos7使用的
更改以後,他的配置文件在 /etc/hostname
~7. DNS配置文件 /etc/resolv.conf
若是咱們更改DNS,就更改網卡的配置文件就能夠了。由於/etc/resolv.conf也是網卡的配置文件(/etc/sysconfig/network-scripts/ens33)定義的。若是在/etc/resolv.conf更改DNS,固然也能夠vim,可是重啓以後會被網卡的配置文件所覆蓋掉,適合臨時更改。這個須要注意
~8. /etc/hosts文件
這個文件是linux和windows都有的。後面的lamp作實驗,訪問一個自定義的域名的時候就用到這個文件,可是在windows上更改的
假如,咱們ping www.qq.123.com,他是一個公網IP。可是我想讓他在咱們的本機不要訪問那個公網IP,要讓他訪問到192.168.159.150來,能夠直接vim /etc/hosts,在最下面一行直接寫 192.168.159.150 www.qq123.com。咱們在ping就會到咱們設定這個IP來,當即生效,但只在本機生效。可支持一個IP多個域名。同一個域名設置了多個IP,以最後一次設置的IP爲準。以行爲單位,左邊IP,右邊域名,以空格分割
實例:
3.
[root@axinlinux-01 ~]# cd /etc/sysconfig/network-scripts/ 咱們先cd進網卡的配置文件
[root@axinlinux-01 network-scripts]# cp ifcfg-ens33 ifcfg-ens33\:1 cp ens33重命名爲ens:1。要脫意一下冒號
[root@axinlinux-01 network-scripts]# vim ifcfg-ens33:1 修改一下ens:1的配置
vim ifcfg-ens33\:0
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
NAME=ens33:0 找到NAME這一行名字改成ens:1(配置文件裏不須要脫意)
UUID=d116f724-9bfb-4573-9f5b-7dace5640272
DEVICE=ens33 找到DEVICE這一行名字改成ens:1(配置文件裏不須要脫意)
ONBOOT=yes:o
IPADDR=192.168.159.150
NETMASK=255.255.255.0
GATEWAY=192.168.159.2 將網關刪掉
DNS33=159.29.29.29 將DNS33刪掉,由於已經有一個DNS33了
[root@axinlinux-01 network-scripts]# ifdown ens33:1 && ifup ens33:1 指定重啓ens33:1這個網卡
[root@axinlinux-01 network-scripts]# ifconfig 咱們在ifconfig一下
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.159.128 netmask 255.255.255.0 broadcast 192.168.159.255
inet6 fe80::20c:29ff:fe87:4224 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:87:42:24 txqueuelen 1000 (Ethernet)
RX packets 1446 bytes 152000 (148.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1168 bytes 161981 (158.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 就發現多了ens33:1了
inet 192.168.159.150 netmask 255.255.255.0 broadcast 192.168.159.255 並且也是能夠pin通的
ether 00:0c:29:87:42:24 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 40 bytes 3168 (3.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 40 bytes 3168 (3.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
4.5.
[root@axinlinux-01 network-scripts]# mii-tool ens33
ens33: negotiated 1000baseT-FD flow-control, link ok
[root@axinlinux-01 network-scripts]# 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
6.
[root@axinlinux-01 network-scripts]# cat /etc/hostname
axinlinux-01
8.
[root@axinlinux-01 network-scripts]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
[root@axinlinux-01 network-scripts]# vim !$
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.159.150 www.qq123.com
[root@axinlinux-01 network-scripts]# ping www.qq123.com
PING www.qq123.com (192.168.159.150) 56(84) bytes of data.
64 bytes from www.qq123.com (192.168.159.150): icmp_seq=1 ttl=64 time=0.133 ms
[root@axinlinux-01 network-scripts]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.159.150 www.qq123.com www.12.com www.wangxin.com
192.168.159.128 www.wangxin.com
[root@axinlinux-01 network-scripts]# ping www.wangxin.com
PING www.qq123.com (192.168.159.128) 56(84) bytes of data.
64 bytes from www.wangxin.com (192.168.159.128): icmp_seq=1 ttl=64 time=0.108 ms
----------------------------------------------------------------------------------------------------------------------------------------------------
10.12 firewalld和netfilter:
linux防火牆-netfilter
~1. selinux臨時關閉 setenforce o
咱們以前密鑰認證的時候就要臨時關閉
~2. selinux永久關閉 vi /etc/selinux/config
vi以後,將 SELINUX=enforcing 改成 disabled。getenforce一下顯示enforcing。這個時候再setenforce o,再去getenforce一下,發現已經爲permissive
permissive表示,雖然selinux已經開啓了。可是僅僅是,遇到須要發生阻斷的時候,他不須要真正的去阻斷,僅僅是有一個提醒。咱們不會從屏幕上看到,可是有個地方會去記錄
~3. centos7以前使用netfilter防火牆
都是能夠用iptables來配置端口的
~4. centos7開始使用firewalld防火牆
都是能夠用iptables來配置端口的
咱們能夠在7上將firewalld關閉,將netfiter打開。是能夠在7上使用netfilter的。方法以下:
先執行~6.(把firewalld停掉,不讓他開機啓動) > 在執行~5.(把這個服務關掉) > 而後再 執行~7. 把netfilter開啓(實際上就是安裝一下) > 再執行~8. (產生這個服務) > 最後執行~9. (開始這個服務)
iptables -nvL能夠查看他的規則
iptables是netfilter的工具,而名字是叫netfilter
~5. systemctl stop firewalld
~6. systemctl disable firewallded
~7. yum install -y iptables-services
~8. systemctl enable iptables
~9. systemctl start iptables
實例:
2.
[root@axinlinux-01 network-scripts]# vi /etc/selinux/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 改成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
[root@axinlinux-01 network-scripts]# getenforce
Enforcing
[root@axinlinux-01 network-scripts]# setenforce 0
[root@axinlinux-01 network-scripts]# getenforce
Permissive
4.(將firewalld關閉,開啓netfilter)
[root@axinlinux-01 network-scripts]# systemctl disable firewallded
Failed to execute operation: No such file or directory
[root@axinlinux-01 network-scripts]# systemctl stop firewalld
[root@axinlinux-01 network-scripts]# yum install -y iptables-services
[root@axinlinux-01 network-scripts]# systemctl enable iptables
Created symlink from /etc/systemd/system/basic.target.wants/iptables.service to /usr/lib/systemd/system/iptables.service.
[root@axinlinux-01 network-scripts]# systemctl start iptables
----------------------------------------------------------------------------------------------------------------------------------------------------
10.13 netfilter5表5鏈介紹:
linux防火牆—netfilter
~1. netfilter的5個表
~2. filter表用於過濾包,最經常使用的表,有INPUT、PORWARD、OUTPUT三個鏈
~3. nat表用於網絡地址轉換,有PREROUTING、OUTPUT、POSTROUTING三個鏈
~4. managle表用於給數據包表標記,幾乎用不到
~5. raw表能夠實現不追蹤某些數據包,阿明歷來不用
~6.security表在centos6中並無,用於強制訪問控制(MAC)的網絡規則,阿銘沒用過
~7. 參考文章 http:www.cnblogs.com/metoy/4320813.html
linux防火牆-netfilter
~1. 數據包流向與netfilter的5個鏈
~2. PREROUTING:數據包進入路由表以前
~3. INPUT:經過路由表後目的地爲本機
~4. FORWARD:經過路由表後,目的地不爲本機
~5. OUTPUT:由本機產生,向外發出
~6. POSTROUTING:發送到網卡接口以前
----------------------------------------------------------------------------------------------------------------------------------------------------
10.14 iptables語法:
linux防火牆—netfilter
iptables相關的命令與用法:
~1. 查看iptables規則:iptables -nvL
service iptables restart 重啓iptables
/etc/sysconfig/iptables 規則保存位置
詳見實例1.
~2. iptables -F 清空規則
感受規則不合適的時候可所有清除。但需注意便是清除了規則,他的文件裏(/etc/sysconfig/iptables)仍是有的
~3. service iptables save 保存規則
把當前的規則保存到文件裏面去
可是咱們剛剛已經刪除了,清空規則以後,咱們在保存規則保存的僅僅是空規則。咱們能夠重啓他的服務,讓他從新加載回來(service iptables restart),
咱們寫完的規則僅僅只是在內存中生效的,想讓他重啓後依然生效,就要service iptables save
也就是說,重啓服務器或者是重啓iptables,他都會從新加載配置文件裏的規則
~4. iptables -t nat -t 指定表(不加-t就是默認filter表)
以上1 2 3 針對的都是filter表
寫法爲 iptables -t nat -nvL
固然net表裏面沒有規則
~5. iptables -Z 能夠把計數器清零
iptables -nvL後,第二行是有數字的。pkts表示的是有多少包,byts表示的是數據量,數據大小(字節)
-Z以後數據清零,再次查看時,又會產生一些數據,是由於每時每刻都是在產生的
~6. iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
增長DROP的規則,來源IP是某某某IP的,訪問咱們某一個端口的。或者來源端口是什麼的。咱們給他DROP(扔掉
)掉
沒有加-t,就是默認的filter表
-A 增長一條規則。後面的INPUT就是增長的鏈是INPUT鏈。
-s指定一個來源IP
-p指定協議。是tcp仍是udp或者icmp
sport 1234 來源的端口是1234
-d目標的IP
dport 80 目標的端口是80
最後操做 -j DROP ,DROP就是把數據直接給扔掉。還有一個 REJECT拒絕(來時會告訴他被拒絕了,比較有禮貌。DORP是看都不看直接扔掉)DORP用的比較多。扔掉和拒絕的效果都是同樣的,都是讓這個數據過不來。至關於把這個IP給封掉了
~7. iptables -I/-A/-D INPUT -s 1.1.1.1 -j DROP
iptables -I INPUT -p tcp --dport 80 -j DORP (在用-dport或-sport的時候,前面要指定哪一個協議)
根據~6. -A爲增長一條規則,即排在後面。-I爲插入一條規則,即爲排在最前面(置頂)。排在最前面,咱們在匹配的時候回優先匹配最前面的規則,而後纔會一條條的往下執行。若是你的這個規則,這個數據包已經匹配了第一條規則,好比咱們訪問80端口的而且知足下面的一條規則,這兩條規則同時知足。那麼同時知足他會匹配第一條,一旦匹配了第一條這個數包就DORP掉了,就不會往下走了。一旦匹配直接執行
-D 刪除一條規則,好比iptables -D INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP(刪除~6.那條規則)
~8. iptables -IINPUT -s 192.168.1.0/24 -i eth0 -j ACCEPT
指定(-i)網卡爲etho的
~9. iptables -nvL --line-numbers
假如,咱們好久之間寫了一條很長的規則,就像~6.。咱們想把它刪了,可是又想不起來這條規則是怎麼寫的。沃恩能夠執行這條命令,在前面加上序號,用序號把他刪掉。寫法爲 iptables -D INPUT 7
~10. iptables -D INPUT 1
利用序號,把規則刪掉
~11. iptables -P INPUT DROP
-P指定鏈的默認的規則。像OUTPUT沒有任何的規則。那麼他就有默認的策略(ACCEPT)來決定。默認的策略就是ACCEPT。全部的數據包,只要是沒有具體的規則來匹配,那麼就走默認的策略。那麼這個默認的策略也能夠改,也能夠改爲DORP。寫法爲:iptables -P OUTPUT DROP。可是不要這麼改默認的規則。一旦這麼改了,就會連不上服務器,由於DORP掉了,數據回傳不過來,xshell只能掉了。就只能回到服務器在改回來 iptables -P OUTPUT ACCEPT
默認的規則不要改,保持默認就能夠了
實例:
1.
[root@axinlinux-01 ~]# iptables -nvL 紅色部分是他的規則
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
61 7858 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
1 60 INPUT_direct all -- * * 0.0.0.0/0 0.0.0.0/0
1 60 INPUT_ZONES_SOURCE all -- * * 0.0.0.0/0 0.0.0.0/0
1 60 INPUT_ZONES all -- * * 0.0.0.0/0 0.0.0.0/0
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
0 0 REJECT all -- * * 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
--
-
2.
[root@axinlinux-01 ~]# iptables -F 清除規則
[root@axinlinux-01 ~]# iptables -nvL 會發現就沒有規則了
Chain INPUT (policy ACCEPT 23 packets, 1924 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 15 packets, 1608 bytes)
pkts bytes target prot opt in out source destination
3.
[root@axinlinux-01 ~]# service iptables restart 重啓iptables 在查看規則
Redirecting to /bin/systemctl restart iptables.service 發現已加載回來
[root@axinlinux-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
22 1872 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
4.
[root@axinlinux-01 ~]# 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
6.
[root@axinlinux-01 ~]# iptables -A INPUT -s 192.168.188.1 -p tcp --sport 1234 -d 192.168.188.128 --dport 80 -j DROP
[root@axinlinux-01 ~]# iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
197 16288 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 458 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
以上-選項相對應的。 即爲新增長的DORP規則
擴展:
圖爲 端口映射: