天天看點

Linux-網絡配置相關(一)

#頭條創作挑戰賽#

1.1 基本網絡配置

Linux-網絡配置相關(一)

将Linux主機接入到網絡,需要配置網絡相關設定

一般包括如下内容:

  • 主機名
  • IP/netmask
  • 路由:預設網關
  • DNS伺服器(主DNS伺服器、次DNS伺服器、第三個DNS伺服器)

1.2 CentOS 6 之前版本網卡名稱

  • 接口命名方式:CentOS 6

以太網:eth[0,1,2,...]

ppp:ppp[0,1,2,...]

  • 網絡接口識别并命名相關的udev配置檔案:

/etc/udev/rules.d/70-persistent-net.rules

  • 檢視網卡:

dmesg | grep –i eth

ethtool -i eth0

  • 解除安裝網卡驅動:

modprobe -r e1000

rmmod e1000

  • 裝載網卡驅動:

modprobe e1000

案例:臨時修改網卡名稱

[root@centos6 ~]#ip link set eth0 down
[root@centos6 ~]#ip link set eth0 name abc
[root@centos6 ~]#ip link set abc up           

1.3 網絡配置指令

1.3.1 網絡配置方式

  • 靜态指定:

    ifconfig, route, netstat

    ip: object {link, addr, route}, ss, tc

    system-config-network-tui,setup

    配置檔案

  • 動态配置設定:DHCP: Dynamic Host Configuration Protocol

1.3.2 ifconfig指令

  • ifconfig指令來自于net-tools包,在一些新的Linux發行版中已被棄用,推薦使用ip指令來替代,在CentOS 6中,ifconfig仍然可用。
  • ifconfig指令是一個用于配置和顯示網絡接口資訊的指令,在CentOS 6中,ifconfig指令已被ifconfig工具替代。

案例:以下是ifconfig指令的一些常見用法和示例:

#顯示所有網絡接口資訊:
ifconfig

#顯示指定網絡接口資訊:
ifconfig eth0

#激活或停用網絡接口:
ifconfig eth0 up    # 激活eth0接口
ifconfig eth0 down  # 停用eth0接口

#配置IP位址和子網路遮罩:
ifconfig eth0 192.168.1.100 netmask 255.255.255.0

#配置啟用廣播和禁用廣播:
ifconfig eth0 broadcast 192.168.1.255    # 啟用廣播
ifconfig eth0 -broadcast                  # 禁用廣播

#配置啟用多點傳播和禁用多點傳播:
ifconfig eth0 multicast    # 啟用多點傳播
ifconfig eth0 -multicast   # 禁用多點傳播

#檢視net-tools包的安裝詳情
rpm -qi net-tools           

格式:

ifconfig [interface]
ifconfig -a
ifconfig IFACE [up|down]
ifconfig interface [aftype] options | address ...
ifconfig IFACE IP/netmask [up]
ifconfig IFACE IP netmask NETMASK           
  • 注意:立即生效
  • 啟用混雜模式:[-]promisc

1.3.3 route指令

路由表管理指令

路由表主要構成:

  • Destination: 目标網絡ID,表示可以到達的目标網絡ID,0.0.0.0/0 表示所有未知網絡,又稱為預設路由,優先級最低
  • Genmask:目标網絡對應的netmask
  • Iface: 到達對應網絡,應該從目前主機哪個網卡發送出來
  • Gateway: 到達非直連的網絡,将資料發送到臨近(下一個)路由器的臨近本主機的接口的IP位址,如果是直連網絡,gateway是0.0.0.0
  • Metric: 開銷cost,值越小,路由記錄的優先級最高

檢視路由表:

route #顯示目前路由表
route -n #檢視詳細的路由表資訊(包括接口和Metric)           

案例:

[root@centos7 ~]#route 
[root@centos7 ~]#route -n           

添加:route add

route add [-net|-host|default] target [netmask Nm] [gw GW] [[dev] If]

#添加一個預設路由:
route add default gw 192.168.1.1           

删除:route del

route del [-net|-host] target [gw Gw] [netmask Nm] [[dev] If]

#删除一個路由表項
route del default gw 192.168.1.1           

案例:

#目标:192.168.1.3 網關:172.16.0.1
route add -host 192.168.1.3 gw 172.16.0.1 dev eth0

#目标:192.168.0.0 網關:172.16.0.1
route add -net 192.168.0.0 netmask 255.255.255.0 gw 172.16.0.1 dev eth0
route add -net 192.168.0.0/24 gw 172.16.0.1 dev eth0
route add -net 192.168.8.0/24 dev eth1 metric 200

#預設路由,網關:172.16.0.1
route add -net 0.0.0.0 netmask 0.0.0.0 gw 172.16.0.1
route add -net 0.0.0.0/0 gw 172.16.0.1
route add default gw 172.16.0.1

#目标:192.168.1.3 網關:172.16.0.1
route del -host 192.168.1.3

#目标:192.168.0.0 網關:172.16.0.1
route del -net 192.168.0.0 netmask 255.255.255.0           

案例:實作靜态路由

環境:

四台主機:
A主機:eth0 NAT模式
R1主機:eth0 NAT模式,eth1 僅主機模式
R2主機:eth0 橋接模式,eth1 僅主機模式
B主機:eth0 橋接模式           
Linux-網絡配置相關(一)
#配置A主機
ifconfig eth0 10.0.0.123/8
route add -net 10.0.0.0/8 dev eth0
route add default gw 10.0.0.200 dev eth0

#配置R1
ifconfig eth0 10.0.0.200/8
ifconfig eth1 192.168.0.200/24
route add -net 10.0.0.0/8 dev eth0
route add -net 192.168.0.0/24 dev eth1
route add -net 172.16.0.0/16 gw 192.168.0.201 dev eth1
echo 1 > /proc/sys/net/ipv4/ip_forward

#配置R2
ifconfig eth0 172.16.0.200/16
ifconfig eth1 192.168.0.201/24
route add -net 192.168.0.0/24 dev eth1
route add -net 172.16.0.0/16 dev eth0
route add -net 10.0.0.0/8 gw 10.0.0.200 dev eth1
echo 1 > /proc/sys/net/ipv4/ip_forward

#配置B
ifconfig eth0 172.16.0.123/16
route add -net 172.16.0.0/16 dev eth0
route add default gw 172.16.0.200 dev eth0           
Linux-網絡配置相關(一)

案例:

[root@router ~]#echo 1 > /proc/sys/net/ipv4/ip_forward           

1.3.4 配置動态路由

通過守護程序擷取動态路由,安裝quagga包,通過指令vtysh配置

支援多種路由協定:

  • RIP:Routing Information Protocol,路由資訊協定
  • OSPF:Open Shortest Path First,開放式最短路徑優先
  • BGP:Border Gateway Protocol,邊界網關協定
  • RIP、OSPF和BGP

1.3.5 netstat指令

來自于net-tools包,建議使用 ss 代替

顯示網絡連接配接:

netstat [--tcp|-t] [--udp|-u] [--raw|-w] [--listening|-l] [--all|-a] [--
numeric|-n] [--extend|-e[--extend|-e]] [--program|-p]           

常用選項

-t: tcp協定相關
-u: udp協定相關
-w: raw socket相關
-l: 處于監聽狀态
-a: 所有狀态
-n: 以數字顯示IP和端口
-e:擴充格式
-p: 顯示相關程序及PID           

常用組合:

-tan, -uan, -tnl, -unl           

顯示路由表:

netstat {--route|-r} [--numeric|-n]
-r: 顯示核心路由表
-n: 數字格式           

1.3.6 顯示接口統計資料

netstat {--interfaces|-I|-i} [iface] [--all|-a] [--extend|-e] [--program|-p] [-
-numeric|-n]
netstat -i
netstat –I=IFACE
ifconfig -s IFACE           

案例:

[root@centos7 ~]#netstat -Ieth0
[root@centos7 ~]#ifconfig -s eth0
[root@centos7 ~]#netstat -nt           

1.3.7 ip指令

來自于iproute包,可用于代替ifconfig

1.3.7.1 配置Linux網絡屬性

ip [ OPTIONS ] OBJECT { COMMAND | help }           

ip 指令說明:

OBJECT := { link | addr | route }
ip link - network device configuration
set dev IFACE,可設定屬性:up and down:激活或禁用指定接口,相當于 ifup/ifdown
show [dev IFACE] [up]::指定接口 ,up 僅顯示處于激活狀态的接口
man幫助:ip(8), ip-address(8), ip-link(8), ip-route(8)           

案例:檢視幫助

[root@centos7 ~]#ip help
[root@centos7 ~]#ip link help
[root@centos7 ~]#ip addr help
[root@centos7 ~]#ip route help           

ip 位址管理

ip addr { add | del } IFADDR dev STRING [label LABEL] [scope {global|link|host}]
[broadcast ADDRESS]
[label LABEL]:添加位址時指明網卡别名
[scope {global|link|host}]:指明作用域,global: 全局可用.link: 僅連結可用,host: 本機可用
[broadcast ADDRESS]:指明廣播位址
ip address show
ip addr flush           

案例:

#禁用網卡
ip link set eth1 down

#網卡改名
ip link set eth1 name newname

#啟用網卡
ip link set newname up

#網卡别名
ip addr add 172.16.100.100/16 dev eth0 label eth0:0
ip addr del 172.16.100.100/16 dev eth0 label eth0:0

#清除網絡位址
ip addr flush dev eth0           

1.3.7.2 管理路由

Linux-網絡配置相關(一)

ip route 用法

#添加路由:
ip route add TARGET via GW dev IFACE src SOURCE_IP
	TARGET:
		主機路由:IP
		網絡路由:NETWORK/MASK

#添加網關:
ip route add default via GW dev IFACE

#删除路由:
ip route del TARGET

#顯示路由:
ip route show|list

#清空路由表:
ip route flush [dev IFACE] [via PREFIX]           

案例:

ip route add 192.168.0.0/24 via 172.16.0.1
ip route add 192.168.1.100 via 172.16.0.1
ip route add default via 172.16.0.1
ip route flush dev eth0           

案例:檢視路由過程

#檢視到達10.0.0.7所使用的路由
[root@centos7 ~]#ip route get 10.0.0.7
10.0.0.7 dev eth0 src 10.0.0.8 uid 0
cache
 
#檢視到達8.8.8.8所使用的路由
[root@centos7 ~]#ip route get 8.8.8.8
8.8.8.8 via 10.0.0.2 dev eth0 src 10.0.0.8 uid 0
cache           

1.3.8 ss 指令

來自于iproute包,代替netstat,netstat 通過周遊 /proc來擷取 socket資訊,ss 使用 netlink與核心tcp_diag 子產品通信擷取 socket 資訊

Linux-網絡配置相關(一)

格式:

ss [OPTION]... [FILTER]           

選項:

-t: tcp協定相關
-u: udp協定相關
-w: 裸套接字相關
-x:unix sock相關
-l: listen狀态的連接配接
-a: 所有
-n: 數字格式
-p: 相關的程式及PID
-e: 擴充的資訊
-m:記憶體用量
-o:計時器資訊           

格式說明

FILTER :  [ state TCP-STATE ]  [ EXPRESSION ]

#TCP的常見狀态:
		tcp finite state machine:
			LISTEN: 監聽
			ESTABLISHED:已建立的連接配接
			FIN_WAIT_1
			FIN_WAIT_2
			SYN_SENT
			SYN_RECV
			CLOSED
EXPRESSION:
		dport =
		sport =           

常用組合:

-tan, -tanl, -tanlp, -uan           

案例:常見用法

#顯示本地打開的所有端口
ss -l

#顯示每個程序具體打開的socket
ss -pl

#顯示所有tcp socket
ss -t -a

#顯示所有的UDP Socekt
ss -u -a

#顯示所有已建立的ssh連接配接
ss -o state established '( dport = :ssh or sport = :ssh )'

#顯示所有已建立的HTTP連接配接
ss -o state established '( dport = :http or sport = :http )'
[root@centos7 ~]#ss -no state established '( dport = :21 or sport = :21 )'
 
 #列出目前socket詳細資訊
ss -s           
Linux-網絡配置相關(一)

繼續閱讀