Linux网络属性管理 局域网:以太网,令牌环网
Ethernet:CSMA/CD
冲突域
广播域
路由器隔离广播域
MAC:Media Access Control
48bits:
24bits:IANA分配
24bits:厂商分配
IP:Internet Protocol
ifconfig 启用混杂模式:[-]promisc 抓包时使用,“-”表示不开启,不加“-”表示开启 route 02-2 25''
[root@www ~]# netstat -rne Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0 0.0.0.0 172.18.0.1 0.0.0.0 UG 0 0 0 eth0 [root@www ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 172.18.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0 0.0.0.0 172.18.0.1 0.0.0.0 UG 0 0 0 eth0 [root@www ~]# netstat -i Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 0 54212 0 0 0 3277 0 0 0 BMRU eth1 1500 0 390 0 0 0 3 0 0 0 BMRU lo 65536 0 304 0 0 0 304 0 0 0 LRU [root@www ~]# netstat --interfaces Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 0 54299 0 0 0 3295 0 0 0 BMRU eth1 1500 0 390 0 0 0 3 0 0 0 BMRU lo 65536 0 304 0 0 0 304 0 0 0 LRU [root@www ~]# netstat -Ieth0 Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 0 54344 0 0 0 3315 0 0 0 BMRU [root@www ~]# netstat -Ieth1 Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth1 1500 0 390 0 0 0 3 0 0 0 BMRU [root@www ~]# netstat -I eth1 usage: netstat [-veenNcCF] [<Af>] -r netstat {-V|--version|-h|--help} netstat [-vnNcaeol] [<Socket> ...] netstat { [-veenNac] -I[<Iface>] | [-veenNac] -i | [-cnNe] -M | -s } [delay]
-r, --route display routing table
-I, --interfaces=<Iface> display interface table for <Iface>
-i, --interfaces display interface table
-g, --groups display multicast group memberships
-s, --statistics display networking statistics (like SNMP)
-M, --masquerade display masqueraded connections
-v, --verbose be verbose
-n, --numeric don't resolve names
--numeric-hosts don't resolve host names
--numeric-ports don't resolve port names
--numeric-users don't resolve user names
-N, --symbolic resolve hardware names
-e, --extend display other/more information
-p, --programs display PID/Program name for sockets
-c, --continuous continuous listing
-l, --listening display listening server sockets
-a, --all, --listening display all sockets (default: connected)
-o, --timers display timers
-F, --fib display Forwarding Information Base (default)
-C, --cache display routing cache instead of FIB
-T, --notrim stop trimming long addresses
-Z, --context display SELinux security context for sockets
<Iface>: Name of interface to monitor/list. <Socket>={-t|--tcp} {-u|--udp} {-S|--sctp} {-w|--raw} {-x|--unix} --ax25 --ipx --netrom <AF>=Use '-A <af>' or '--<af>'; default: inet List of possible address families (which support routing): inet (DARPA Internet) inet6 (IPv6) ax25 (AMPR AX.25) netrom (AMPR NET/ROM) ipx (Novell IPX) ddp (Appletalk DDP) x25 (CCITT X.25) [root@www ~]# netstat -I Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 0 54499 0 0 0 3346 0 0 0 BMRU eth1 1500 0 390 0 0 0 3 0 0 0 BMRU lo 65536 0 304 0 0 0 304 0 0 0 LRU [root@www ~]# netstat -Ieth0 Kernel Interface table Iface MTU Met RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg eth0 1500 0 54532 0 0 0 3353 0 0 0 BMRU
ip show / manipulate routing, devices, policy routing and tunnels
ip [ OPTIONS ] OBJECT { COMMAND | help }
OBJECT := { link | address | route }
link OBJECT:
ip-link network device configuration
- set 设置网络接口属性,比如接口自身的启用或禁用,也包括接口上的属性启用或禁用 o up and down
- show • [dev IFACE]:指定接口 • [up]:仅显示处于激活状态的接口 [tzx@www ~]$ ip link show 显示每个接口对应的简要描述信息 link:主要是管理二层信息的 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c2:cb:4b brd ff:ff:ff:ff:ff:ff <大写>:已经启用的属性 BROADCAST:支持广播 MULTICAST:支持组播、多播 UP:处于启用状态 LOWER_UP:??? mtu 1500:最大协议传输单元 maximum transport unit 一般以太网的最大传输单元为1500字节 qdisc pfifo_fast:流控算法 state UP:状态为启用 qlen 1000:传输队列 link/ether:MAC地址 brd:广播地址
[root@www ~]# ip link show dev eth0 显示指定接口的信息 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c2:cb:4b brd ff:ff:ff:ff:ff:ff
[root@www ~]# ip link show up 仅显示当前启用的接口信息,包括unknown 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c2:cb:4b brd ff:ff:ff:ff:ff:ff 3: eth1: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c2:cb:55 brd ff:ff:ff:ff:ff:ff [root@www ~]# ip link set dev ens34 down [root@www ~]# ip link show dev ens34 3: ens34: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000 link/ether 00:0c:29:32:5b:76 brd ff:ff:ff:ff:ff:ff
ip addr 协议地址管理工具
ip addr { add | del } IFADDR dev IFACE
注:如果原本某网卡已经有地址,则在这个地址之外添加一个辅助地址;
[ label LABEL ]:添加地址时指明网卡别名
[ scope {global|link|host} ]:指明作用域
global:全局可用;
link:仅链接可用;
host:本机可用;
[ broadcat ADDRESS ]:指明广播地址
ip addr show - look at protocol addresses
[ dev DEVICE ]
[ label PATTERN ]
[ primary and secondary ]
ip addr flush - flush protocol addresses
使用格式同show
addr show
[root@www ~]# ip addr show dev eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:c2:cb:4b brd ff:ff:ff:ff:ff:ff
inet 172.18.120.120/16 brd 172.18.255.255 scope global eth0
inet6 fe80::20c:29ff:fec2:cb4b/64 scope link
valid_lft forever preferred_lft forever
link信息
inet:IPv4地址,brd广播地址,scope [global|host] 作用域,global 表示可以拿来真正实现通信的,eth0接口名称
inet6:IPv6地址
addr add
[root@www ~]# ip addr add 172.16.100.13/16 dev eth0
使用add给eth0添加一个辅助地址
[root@www ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:c2:cb:4b brd ff:ff:ff:ff:ff:ff
inet 172.18.120.120/16 brd 172.18.255.255 scope global eth0
inet 172.16.100.13/16 scope global eth0
inet6 fe80::20c:29ff:fec2:cb4b/64 scope link
valid_lft forever preferred_lft forever
[root@www ~]# ip addr replace 172.16.100.14/16 dev eth0 使用replace,但未指定eth0中的哪一个地址,所以又添加了一个地址上去了 [root@www ~]# ip addr show dev eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c2:cb:4b brd ff:ff:ff:ff:ff:ff inet 172.18.120.120/16 brd 172.18.255.255 scope global eth0 inet 172.16.100.13/16 scope global eth0 inet 172.16.100.14/16 scope global secondary eth0 inet6 fe80::20c:29ff:fec2:cb4b/64 scope link valid_lft forever preferred_lft forever
[root@www ~]# ip addr replace 172.16.100.15/16 172.16.100.14/16 dev eth0 ??? Error: either "local" is duplicate, or "172.16.100.14/16" is a garbage.
[root@www ~]# ip addr add 172.16.100.15/16 dev eth0:1 [root@www ~]# ip addr show eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c2:cb:4b brd ff:ff:ff:ff:ff:ff inet 172.18.120.120/16 brd 172.18.255.255 scope global eth0 inet 172.16.100.13/16 scope global eth0 inet 172.16.100.14/16 scope global secondary eth0 inet 172.16.100.15/16 scope global secondary eth0 inet6 fe80::20c:29ff:fec2:cb4b/64 scope link valid_lft forever preferred_lft forever addr del [root@www ~]# ip addr del 172.16.100.14/16 dev eth0 使用del 删除指定接口上的某个IP [root@www ~]# ip addr show eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c2:cb:4b brd ff:ff:ff:ff:ff:ff inet 172.18.120.120/16 brd 172.18.255.255 scope global eth0 inet 172.16.100.13/16 scope global eth0 inet 172.16.100.15/16 scope global secondary eth0 inet6 fe80::20c:29ff:fec2:cb4b/64 scope link valid_lft forever preferred_lft forever
[root@www ~]# ip addr del 172.16.100.13/16 dev eth0 删除主IP,附加IP一同被删除 [root@www ~]# ip addr show dev eth0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c2:cb:4b brd ff:ff:ff:ff:ff:ff inet 172.18.120.120/16 brd 172.18.255.255 scope global eth0 inet6 fe80::20c:29ff:fec2:cb4b/64 scope link valid_lft forever preferred_lft forever addr add label NAME [root@www ~]# ip addr add 172.16.100.13/16 dev eth0 label 'eth0:0' 新加一个地址,但没有加在原来的名字上,而是给这块网卡取了一个别名,新地址加在了这个别名上,不过地址却属于同一块网卡; [root@www ~]# ip addr show 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c2:cb:4b brd ff:ff:ff:ff:ff:ff inet 172.18.120.120/16 brd 172.18.255.255 scope global eth0 inet 172.16.100.13/16 scope global eth0:0 inet6 fe80::20c:29ff:fec2:cb4b/64 scope link valid_lft forever preferred_lft forever
[root@www ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:C2:CB:4B
inet addr:172.18.120.120 Bcast:172.18.255.255 Mask:255.255.0.0
inet6 addr: fe80::20c:29ff:fec2:cb4b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:93819 errors:0 dropped:0 overruns:0 frame:0
TX packets:2773 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:6873912 (6.5 MiB) TX bytes:550400 (537.5 KiB)
eth0:0 Link encap:Ethernet HWaddr 00:0C:29:C2:CB:4B
inet addr:172.16.100.13 Bcast:0.0.0.0 Mask:255.255.0.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
addr add
• scope SCOPE_VALUE
o global -- the address is globally valid. 全局有效
o link -- the address is link local, i.e. it is valid only on this device. 仅链接可用,在当前设备上有效,自己能ping自己,别人无法ping你
o host -- the address is valid only inside this host. 仅对当前主机有效
网络地址不是属于网卡接口的,而是属于内核的
内核中有两个IP地址,不论是否开启转发功能,1.2都能ping同2.1
IP1.1 1网络 IP1.2
内 核 不通
IP2.1 不通
[root@www ~]# ip addr show dev eth0 primary
显示eth0主IP地址
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:c2:cb:4b brd ff:ff:ff:ff:ff:ff
inet 172.18.120.120/16 brd 172.18.255.255 scope global eth0
inet 172.16.100.13/16 scope global eth0:0
inet 172.16.100.15/16 scope global secondary eth0:0
inet6 fe80::20c:29ff:fec2:cb4b/64 scope link
valid_lft forever preferred_lft forever
[root@www ~]# ip addr show dev eth0 secondary
显示eth0辅助IP地址
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:c2:cb:4b brd ff:ff:ff:ff:ff:ff
inet 172.16.100.15/16 scope global secondary eth0:0
addr flush dev eth0 label 'eth0:0'
[root@www ~]# ip addr flush dev eth0 label 'eth0:0'
[root@www ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:c2:cb:4b brd ff:ff:ff:ff:ff:ff
inet 172.18.120.120/16 brd 172.18.255.255 scope global eth0
ip route - routing table management
ip route add
添加路由:ip route add TARGET via GW dev IFACE src SOURCE_IP
TARGET:
主机路由:IP
网络路由:NETWORK/MASK
add TARGET 指明到哪一个位置(网络)
via gateway 指明下一跳,从本网络哪个网关出去
dev IFACE 指明流出的接口
添加网关:ip route add default via GW dev IFACE
表示本网络通过默认网关(本网络的)到达另一个网络
[root@www ~]# ip route add 192.168.1.3 via 172.18.0.1 dev eth0
[root@www ~]# ip route show
192.168.1.3 via 172.18.0.1 dev eth0
172.18.0.0/16 dev eth0 proto kernel scope link src 172.18.120.120
169.254.0.0/16 dev eth0 scope link metric 1002
default via 172.18.0.1 dev eth0 proto static
[root@www ~]# ip route add default via 172.18.0.1 proto static
[root@www ~]# ip route show
172.18.0.0/16 dev eth0 proto kernel scope link src 172.18.120.120
169.254.0.0/16 dev eth0 scope link metric 1002
default via 172.18.0.1 dev eth0 proto static
ip route delete 删除路由:ip route del TARGET [root@www ~]# ip route del 192.168.1.3 [root@www ~]# ip route show 192.168.0.0/24 via 172.18.0.1 dev eth0 172.18.0.0/16 dev eth0 proto kernel scope link src 172.18.120.120 169.254.0.0/16 dev eth0 scope link metric 1002 default via 172.18.0.1 dev eth0 proto static [root@www ~]# ip route del 192.168.0.0/24 [root@www ~]# ip route list 172.18.0.0/16 dev eth0 proto kernel scope link src 172.18.120.120 169.254.0.0/16 dev eth0 scope link metric 1002 default via 172.18.0.1 dev eth0 proto static [root@www ~]# ip route del default [root@www ~]# ip route list 172.18.0.0/16 dev eth0 proto kernel scope link src 172.18.120.120 169.254.0.0/16 dev eth0 scope link metric 1002 [root@www ~]# ip route add default via 172.18.0.1 proto static [root@www ~]# ip route show 172.18.0.0/16 dev eth0 proto kernel scope link src 172.18.120.120 169.254.0.0/16 dev eth0 scope link metric 1002 default via 172.18.0.1 dev eth0 proto static
ip route flush ip route show [dev IFACE]:只清空或显示某接口的路由信息 [via PREFIX]:只清空或显示某网关的路由信息 [root@www ~]# ip route show dev eth0 172.18.0.0/16 proto kernel scope link src 172.18.120.120 169.254.0.0/16 scope link metric 1002 default via 172.18.0.1 proto static [root@www ~]# ip route show dev eth1 [root@www ~]# ip route add 172.16.0.0/16 via 192.168.100.12 [root@www ~]# ip route show dev eth1 192.168.100.0/24 proto kernel scope link src 192.168.100.12 172.16.0.0/16 via 192.168.100.12 [root@www ~]# ip route flush dev eth1 [root@www ~]# ip route show 172.18.0.0/16 dev eth0 proto kernel scope link src 172.18.120.120 169.254.0.0/16 dev eth0 scope link metric 1002 default via 172.18.0.1 dev eth0 proto static
使用命令配置IP地址及路由信息以后,IP等信息在内核的TCP/IP协议栈上,禁用或重启会清空。
ss命令:
格式:ss [OPTION]... [FILTER]
选项:
-t:tcp协议相关
-u:udp协议相关
-w:裸套接字相关
-x:unix sock相关
-l:listen状态的链接
-a:所有
-n:数字格式
-p:相关的程序及PID
-e:扩展的信息
-m:内存用量
-o: 计时器信息
常用组合:
-tan,-tanl,-tanlp,-uan
FILTER:- [ state TCP-STATE ] [ EXPRESSION ]
TCP的常见状态:
tcp finite state machine:tcp有限状态机
LISTEN:监听
ESTABLISHED:已建立的连接
FIN_WAIT_1:结束等待1
FIN_WAIT_2:结束等待2
SYN_SENT:SYN已发送
SYN_RECV:SYN已接收
CLOSED:已关闭
[root@www ~]# ss -tan state ESTABLISHED
Recv-Q Send-Q Local Address:Port Peer Address:Port
0 0 172.18.120.120:22 172.18.118.94:60735
0 0 172.18.120.120:22 172.18.118.94:63366
[root@www ~]# ss -tan state LISTENING
Recv-Q Send-Q Local Address:Port Peer Address:Port
0 128 :::58018 :::*
0 128 :60425 :
EXPRESSION:
dport =
sport =
示例:'( dport = :ssh or sport = :ssh )'
[root@www ~]# ss -tan 'dport = :60735'
State Recv-Q Send-Q Local Address:Port Peer Address:Port
ESTAB 0 160 172.18.120.120:22 172.18.118.94:60735
[root@www ~]# ss -tan 'sport = :22'
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 :::22 :::
LISTEN 0 128 *:22 :
ESTAB 0 64 172.18.120.120:22 172.18.118.94:60735
ESTAB 0 0 172.18.120.120:22 172.18.118.94:63366
Linux网络属性配置(3):修改配置文件
IP、MASK、GW、DNS相关配置文件:
/etc/sysconfig/network-scritps/ifcfg-IFACE
路由相关的配置文件:
/etc/sysconfig/network-scripts/route-IFACE
/etc/sysconfig/network-scritps/ifcfg-IFACE: DEVICE:此配置文件应用到的设备; HWADDR:对应的设备的MAC地址; BOOTPROTO:激活此设备时使用的地址配置协议,常用的dhcp,static,none,boot; NM_CONTROLLED:NM是NetworkManager的简写:此网卡是否接受NM控制;Centos6建议为"no"; ONBOOT:在系统引导时是否激活此设备; TYPE:接口类型:常见的有Ethernet,Bridge UUID:设备的唯一标识;
如果BOOTPROTO=static;则自行配置IP等信息:
IPADDR:指明IP地址;主地址;
NETMASK:子网掩码;或者 PREFIX=
GATEWAY:默认网关;
DNS1:第一个DNS服务器指向;
DNS2:第二个DNS服务器指向;
USERCTL:普通用户是否可控制此设备;
PEERDNS:如果BOOTPROTO值为"dhcp",是否允许dhcp server 分配的dns服务器指向信息直接覆盖至/etc/resolv.conf文件中的;
此处的DNS比/etc/resolv.conf中的优先级高,因为这里是直接作用在网卡接口上。
为什么在此处可以修改DNS,因为普通用户是没有权限修改/etc/resolv.conf文件。
/etc/sysconfig/network-scripts/route-IFACE 两种风格
- TARGET via GW [root@www ~]# vim /etc/sysconfig/network-scripts/route-eth0 192.168.0.0/24 via 172.16.0.1
- 每三行定义一条路由 ADDRESS#=TARGET NETMASK#=mask GATEWAY#=GW [root@www ~]# vim /etc/sysconfig/network-scripts/route-eth0 ADDRESS0=192.168.20.0 NETMASK0=255.255.255.0 GATEWAY0=172.16.0.1
ADDRESS1=192.168.30.0 NETMASK1=255.255.255.0 GATEWAY1=172.16.0.1 两种风格不能同时使用;
给网卡配置多个地址:
-
ifconfig [root@www ~]# ifconfig eth0:0 192.168.0.22/24 up [root@www ~]# ifconfig eth0:0 Link encap:Ethernet HWaddr 00:0C:29:C2:CB:4B
inet addr:192.168.0.22 Bcast:192.168.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 [root@www ~]# ip addr show dev eth0:0 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c2:cb:4b brd ff:ff:ff:ff:ff:ff inet 172.18.120.120/16 brd 172.18.255.255 scope global eth0 inet 192.168.0.22/24 brd 192.168.0.255 scope global eth0:0 inet6 fe80::20c:29ff:fec2:cb4b/64 scope link valid_lft forever preferred_lft forever -
ip addr add IP dev eth0 label 'eth0:0'
-
配置文件: ifcfg-IFACE_ALIAS DEVICE=IFACE_ALIAS [root@www network-scripts]# cp ifcfg-eth0 ifcfg-eth0:0 [root@www network-scripts]# cat ifcfg-eth0:0 DEVICE=eth0:0 TYPE=Ethernet ONBOOT=yes NM_CONTROLLED=no BOOTPROTO=static IPADDR=192.168.0.22 NETMASK=255.255.255.0
注意:别名是不能使用dhcp协议引导;必须指定IP地址;
Linux网络属性配置的tui(text user interface) system-config-network-tui 也可以使用setup找到
注意:记得启动重启网络服务方能生效;
配置当前主机的主机名: hostname [HOSTNAME] /etc/sysconfig/network [root@www ~]# cat /etc/sysconfig/network NETWORKING=yes 网络总开关 HOSTNAME=www.tzx.com 主机名,FQDN
网络接口识别并命名相关的udev配置文件: /etc/udev/rules.d/70-persistent-net.rules
PCI device 0x8086:0x100f (e1000)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?", ATTR{address}=="00:0c:29:c2:cb:4b", ATTR{type}=="1", KERNEL=="eth", NAME="eth0" 卸载网卡驱动: lsmod modprobe -r e1000
装载网卡驱动:
modprobe e1000
注意:改完后,需改对应的网卡配置文件;
Centos 7 网络属性配置
传统命名:以太网eth[0,1,2,...]
可预测功能
udev支持多种不同的命名方案;
Firmware(固件),拓扑结构
-
网卡命名机制 systemd对网络设备的命名方式:
-
如果Firmware或BIOS为主板上集成的设备提供的索引信息可用,且可预测则根据此索引进行命名,例如 eno1;
-
如果Firmware或BIOS为PCI-E扩展槽所提供的索引信息可用,且可预测,则根据此索引进行命名,例如 ens1;
-
如果硬件接口的物理位置信息可用,则根据此信息进行命名, 例如 enp2s0;
-
如果用户显示启动,也可根据MAC地址进行命名,enx2387a1dc56;
-
上述均不可用时,则使用传统命名机制;
上述命名机制中,有的需要biosdevname程序的参与
-
名称组成格式 en:Ethernet wl:wlan wireless local area network 无线局域网设备 ww:wwan wireless wide area network 无线广域网设备
名称类型: o<index>:Onboard集成设备的设备索引号; s<slot>:扩展槽的索引号;支持虚拟设备,而且后面支持跟上设备ID,也会非常的长; x<MAC>:基于MAC地址的命名; p<bus>s<slot>:enp2s1;p后面跟上PCI总线的标号,每一个PCI总线上还有多个插槽s<slot>;
网卡设备的命名过程: 第一步: udev,辅助工具程序/lib/udev/rename_device,/usr/lib/udev/rules.d/60-net.rules udev是内核中的一种机制,它能够将内核所识别的每一个硬件设备及其相关信息通过sys这么一个伪文件系统向用户空间进行输出,用户空间的工具根据这些信息能够判断出硬件设备的接口型号,并根据这些型号可以给它关联特定的驱动甚至加载额外的辅助功能;sys的这些功能是至关重要的,而udev是主要用来根据sys中的这些功能创建设备文件的;像/dev目录下为什么会有sda,sdb?有些在内核启动时就能识别,这是就可以通过devtmpfs来实现输出,而剩余的则有可能就需要用到用户空间的一些辅助工具来帮忙探测并加载驱动程序的,所以udev就是这么一款工具; 第一步:这个辅助文件会去查找/etc/sysconfig/network-scripts/ifcfg-开头的网络接口配置文件,在配置文件中查HWADDR=这一项,界定了每一块网卡的MAC地址,然后拿着MAC地址看哪个配置文件跟这个MAC一样,就去读取这个文件中的DEVICE所填写的名称,将这个名称设置成这个网卡的名称。 第一步成功则使用第一种命名机制;后面则进行;第一步不成功则执行第二步,以此类推;如果以上三步都没有匹配成功,接口不再被重命名,则使用传统命名。 第二步: biosdevname 会根据/usr/lib/udev/rules.d/71-biosdevname.rules
第三步:
通过检测网络接口设备,根据/usr/lib/udev/rules.d/75-net-description
ID_NET_NAME_ONBOARD,ID_NET_NAME_SLOT,ID_NET_NAME_PATH
要想彻底使用传统命名方式,则将这些文件统统删了,再将/dev/null链接到这些文件上;
回归传统命名方式: [root@www ~]# vim /etc/default/grub GRUB_TIMEOUT=5 GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)" GRUB_DEFAULT=saved GRUB_DISABLE_SUBMENU=true GRUB_TERMINAL_OUTPUT="console" GRUB_CMDLINE_LINUX="rhgb quiet" GRUB_DISABLE_RECOVERY="true"
GRUB_CMDLINE_LINUX="net.ifnames=0 rhgb quiet" 禁止修改网络接口名称,默认是修改,0为禁止;改完之后不会立即生效,还需要使用grub2-mkconfig生成工具,来生成新的配置文件
[root@www ~]# grub2-mkconfig -o /etc/grub2.cfg -o 指定需生成的配置文件 Generating grub configuration file ... Found linux image: /boot/vmlinuz-3.10.0-862.el7.x86_64 Found initrd image: /boot/initramfs-3.10.0-862.el7.x86_64.img Found linux image: /boot/vmlinuz-0-rescue-0263441c05d84ee385ad53362a383086 Found initrd image: /boot/initramfs-0-rescue-0263441c05d84ee385ad53362a383086.img done
重启系统 重启后,系统没有给网络接口取别名,还原为传统网卡名称ethX,此时的地址是通过dhcp获取的,系统并未给出配置文件,需自行编辑网卡接口配置文件。
nmcli IP地址配置工具 - command-line tool for controlling NetworkManager nmcli [ OPTIONS ] OBJECT { COMMAND | help }
device - show and manage network interfaces
connection - start, stop, and manage network connections
nmcli connection { COMMAND | help }
COMMAND := { show | up | down | add | modify | clone | edit | delete | monitor | reload | load | import | export }
如何修改IP地址等属性:
nmcli connection modify IFACE ([+|-]<setting>.<property> <value>)+
setting.property:
ipv4.addresses 设置ipv4地址
ipv4.gateway 设置默认网关
ipv4.dns1 设置默认DNS
ipv4.method ipv4地址配置方式
manual 手动配置方式
dhcp
使用connection modify IFACE +ipv4.addresses IPADDR/PREEFIX给已知网卡加新地址 [root@www ~]# nmcli device show ens33 GENERAL.DEVICE: ens33 GENERAL.TYPE: ethernet GENERAL.HWADDR: 00:0C:29:32:5B:6C GENERAL.MTU: 1500 GENERAL.STATE: 100 (connected) GENERAL.CONNECTION: ens33 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1 WIRED-PROPERTIES.CARRIER: on IP4.ADDRESS[1]: 172.18.119.119/16 IP4.GATEWAY: 172.18.0.1 IP4.ROUTE[1]: dst = 172.18.0.0/16, nh = 0.0.0.0, mt = 100 IP4.ROUTE[2]: dst = 0.0.0.0/0, nh = 172.18.0.1, mt = 100 IP6.ADDRESS[1]: fe80::57fd:69c8:aa8b:8cd7/64 IP6.GATEWAY: -- IP6.ROUTE[1]: dst = ff00::/8, nh = ::, mt = 256, table=255 IP6.ROUTE[2]: dst = fe80::/64, nh = ::, mt = 256 IP6.ROUTE[3]: dst = fe80::/64, nh = ::, mt = 100
[root@www ~]# nmcli connection modify ens33 +ipv4.addresses 172.18.21.120/16 给ens33增加一个IPADDR,不写+,表示修改当前IPADDR [root@www ~]# nmcli device show ens33 GENERAL.DEVICE: ens33 GENERAL.TYPE: ethernet GENERAL.HWADDR: 00:0C:29:32:5B:6C GENERAL.MTU: 1500 GENERAL.STATE: 100 (connected) GENERAL.CONNECTION: ens33 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/1 WIRED-PROPERTIES.CARRIER: on IP4.ADDRESS[1]: 172.18.119.119/16 此时第二个地址并没有显示 IP4.GATEWAY: 172.18.0.1 IP4.ROUTE[1]: dst = 172.18.0.0/16, nh = 0.0.0.0, mt = 100 IP4.ROUTE[2]: dst = 0.0.0.0/0, nh = 172.18.0.1, mt = 100 IP6.ADDRESS[1]: fe80::57fd:69c8:aa8b:8cd7/64 IP6.GATEWAY: -- IP6.ROUTE[1]: dst = ff00::/8, nh = ::, mt = 256, table=255 IP6.ROUTE[2]: dst = fe80::/64, nh = ::, mt = 256 IP6.ROUTE[3]: dst = fe80::/64, nh = ::, mt = 100
[root@www ~]# nmcli connection down ens33;nmcli connection up ens33 必须先禁用再启用网卡ens33,新地址才生效,通过重启NetworkManager无效 Connection 'ens33' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/1) Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5)
[root@www ~]# nmcli device show ens33 GENERAL.DEVICE: ens33 GENERAL.TYPE: ethernet GENERAL.HWADDR: 00:0C:29:32:5B:6C GENERAL.MTU: 1500 GENERAL.STATE: 100 (connected) GENERAL.CONNECTION: ens33 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/5 WIRED-PROPERTIES.CARRIER: on IP4.ADDRESS[1]: 172.18.119.119/16 IP4.ADDRESS[2]: 172.18.21.120/16 新地址生效 IP4.GATEWAY: 172.18.0.1 IP4.ROUTE[1]: dst = 172.18.0.0/16, nh = 0.0.0.0, mt = 102 IP4.ROUTE[2]: dst = 172.18.0.0/16, nh = 0.0.0.0, mt = 102 IP4.ROUTE[3]: dst = 0.0.0.0/0, nh = 172.18.0.1, mt = 102 IP6.ADDRESS[1]: fe80::57fd:69c8:aa8b:8cd7/64 IP6.GATEWAY: -- IP6.ROUTE[1]: dst = ff00::/8, nh = ::, mt = 256, table=255 IP6.ROUTE[2]: dst = fe80::/64, nh = ::, mt = 256 IP6.ROUTE[3]: dst = fe80::/64, nh = ::, mt = 102 删除IP地址IPADDR [root@www ~]# nmcli connection modify ens33 -ipv4.addresses 172.18.21.120/16 删除刚刚新增的IPADDR2
[root@www ~]# nmcli connection down ens33;nmcli connection up ens33 禁用,启用ens33接口 Connection 'ens33' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/5) Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)
[root@www ~]# nmcli device show ens33 显示ens33接口信息,IPADDR2已删除 GENERAL.DEVICE: ens33 GENERAL.TYPE: ethernet GENERAL.HWADDR: 00:0C:29:32:5B:6C GENERAL.MTU: 1500 GENERAL.STATE: 100 (connected) GENERAL.CONNECTION: ens33 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/6 WIRED-PROPERTIES.CARRIER: on IP4.ADDRESS[1]: 172.18.119.119/16 IP4.GATEWAY: 172.18.0.1 IP4.ROUTE[1]: dst = 172.18.0.0/16, nh = 0.0.0.0, mt = 102 IP4.ROUTE[2]: dst = 0.0.0.0/0, nh = 172.18.0.1, mt = 102 IP6.ADDRESS[1]: fe80::57fd:69c8:aa8b:8cd7/64 IP6.GATEWAY: -- IP6.ROUTE[1]: dst = ff00::/8, nh = ::, mt = 256, table=255 IP6.ROUTE[2]: dst = fe80::/64, nh = ::, mt = 256 IP6.ROUTE[3]: dst = fe80::/64, nh = ::, mt = 102 修改/增加网关,增加DNS [root@www ~]# nmcli connection modify ens33 ipv4.gateway 172.18.0.1 修改默认网关 [root@www ~]# nmcli connection modify ens33 ipv4.dns 223.5.5.5 增加DNS [root@www ~]# nmcli connection modify ens33 +ipv4.dns 8.8.8.8 增加DNS [root@www ~]# nmcli connection down ens33 ;nmcli connection up ens33 禁用,启用ens33 Connection 'ens33' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/7) Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/8) [root@www ~]# nmcli device show ens33 显示ens33信息 GENERAL.DEVICE: ens33 GENERAL.TYPE: ethernet GENERAL.HWADDR: 00:0C:29:32:5B:6C GENERAL.MTU: 1500 GENERAL.STATE: 100 (connected) GENERAL.CONNECTION: ens33 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/8 WIRED-PROPERTIES.CARRIER: on IP4.ADDRESS[1]: 172.18.119.119/16 IP4.GATEWAY: 172.18.0.1 IP4.ROUTE[1]: dst = 172.18.0.0/16, nh = 0.0.0.0, mt = 102 IP4.ROUTE[2]: dst = 0.0.0.0/0, nh = 172.18.0.1, mt = 102 IP4.DNS[1]: 223.6.6.6 IP4.DNS[2]: 8.8.8.8 IP6.ADDRESS[1]: fe80::57fd:69c8:aa8b:8cd7/64 IP6.GATEWAY: -- IP6.ROUTE[1]: dst = ff00::/8, nh = ::, mt = 256, table=255 IP6.ROUTE[2]: dst = fe80::/64, nh = ::, mt = 256 IP6.ROUTE[3]: dst = fe80::/64, nh = ::, mt = 102
nmtui:网络接口配置tui工具
hostnamectl:主机名称配置工具 [root@www ~]# hostnamectl set-hostname www.tzx.com
参考资料:http://www.redhat.com/hdocs Network Administration Guide 网络管理指南,Centos7 nmap,ncat,tcpdump 工具使用 nmap 网络扫描器 ncat 文件服务器工具 tcpdump 抓包解析工具
网络客户端工具 lftp,ftp,lftpget,wget
lftp [ -p port ] [ -u user[,password]] SERVER
子命令:
get
mget
ls
help
lftpget URL
wget
wget [option]... [URL]...
-q:静默模式
-c:断点续传
-D:保存位置
--limit-rates=:指定传输速率