标签:script packet prot 解析 0.11 slot 改ip iproute 管理
计算机网络可以简单分为局域网和广域网。国际标准组织ISO制定了一个层次化的网络通信参考模型--OSI(open system interconnection),每一层定义了其实现的功能以实现设备间网络通信。
但在实际中使用的是TCP/IP协议,(本身包含TCP传输控制协议,IP互联网络协议),tcp?ip模型范围4层将多个网络进行无缝连接。
IP协议:在相互连接的网络之间传递IP数据报、分配唯一IP地址。
IP地址:网络号+主机号
[root@xt ~]# ip addr
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
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:20:b5:fd brd ff:ff:ff:ff:ff:ff
inet 192.168.0.112/24 brd 192.168.0.255 scope global dynamic eno16777736
valid_lft 83592sec preferred_lft 83592sec
inet6 fe80::20c:29ff:fe20:b5fd/64 scope link
valid_lft forever preferred_lft forever
[root@xt ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.0.112 netmask 255.255.255.0 broadcast 192.168.0.255
inet6 fe80::20c:29ff:fe20:b5fd prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:20:b5:fd txqueuelen 1000 (Ethernet)
RX packets 11796 bytes 926808 (905.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7634 bytes 5484873 (5.2 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@xt ~]# ifconfig ens36 192.168.175.10/24 up #配置IP并激活
[root@xt ~]# ifconfig ens36
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.175.10 netmask 255.255.255.0 broadcast 192.168.175.255
inet6 fe80::20c:29ff:fe20:b507 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:20:b5:07 txqueuelen 1000 (Ethernet)
RX packets 4 bytes 989 (989.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 20 bytes 2076 (2.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@xt ~]# ifconfig ens36 down #关闭后网卡信息
[root@xt ~]# ifconfig
eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
......
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
....
[root@xt ~]# systemctl restart network #重启网络服务
[root@xt ~]# ifconfig
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.175.133 netmask 255.255.255.0 broadcast 192.168.175.255
inet6 fe80::20c:29ff:fe20:b507 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:20:b5:07 txqueuelen 1000 (Ethernet)
RX packets 5 bytes 1331 (1.2 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 32 bytes 3252 (3.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
注:这种方式修改,立即生效,只是临时有效,重启服务后失效
[root@xt ~]# netstat -tunlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1168/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1246/master
tcp6 0 0 :::22 :::* LISTEN 1168/sshd
tcp6 0 0 ::1:25 :::* LISTEN 1246/master
udp 0 0 0.0.0.0:68 0.0.0.0:* 3069/dhclient
udp 0 0 0.0.0.0:68 0.0.0.0:* 2717/dhclient
-显示接口的统计数据
显示所有接口:netstat -i
指定接口:netstat -I<INTERFACE>
[root@xt ~]# netstat -i
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
eno16777 1500 15549 0 0 0 10242 0 0 0 BMRU
ens36 1500 18 0 0 0 51 0 0 0 BMRU
lo 65536 0 0 0 0 0 0 0 0 LRU
[root@xt ~]# netstat -Iens36
Kernel Interface table
Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg
ens36 1500 18 0 0 0 51 0 0 0 BMRU
[root@xt ~]#
[root@xt ~]# ip addr show ens36
3: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:20:b5:07 brd ff:ff:ff:ff:ff:ff
inet 192.168.175.133/24 brd 192.168.175.255 scope global dynamic ens36
valid_lft 1672sec preferred_lft 1672sec
inet6 fe80::20c:29ff:fe20:b507/64 scope link
valid_lft forever preferred_lft forever
[root@xt ~]# ip link set ens36 down
[root@xt ~]# ip addr show ens36
3: ens36: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:0c:29:20:b5:07 brd ff:ff:ff:ff:ff:ff
inet 192.168.175.133/24 brd 192.168.175.255 scope global dynamic ens36
valid_lft 1648sec preferred_lft 1648sec
[root@xt ~]# ip addr li ens36
3: ens36: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN qlen 1000
link/ether 00:0c:29:20:b5:07 brd ff:ff:ff:ff:ff:ff
ip netns:网络名称空间管理
ip netns list:
ip netns add Name
ip netns del Name
ip [-all] netns exec [NAME] cmd ...#在指定名称空间中运行命令
设置网络名称步骤:在云计算中使用
1.设置名称
ip netns add mynet
2.添加至每个网卡
ip link set ens36 netns mynet
3.运行查看
ip netns exec mynet ip llink show
ip address :协议地址管理
ip addr add :添加地址
ip addr {add|change|replace} if_address/prefix dev STRING [scope ..] [...] #[broadcast address]: 广播地址,会根据ip和netmask自动计算 [scope scope_value]: 值有global、link、host
ip addr del :删除IP地址
ip addr del if_address dev IFACE
ip addr show :查看地址
ip addr show dev IFACE
ip addr flush :清空地址
ip addr flush dev IFACE
[root@xt ~]# ip addr show ens36
3: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:20:b5:07 brd ff:ff:ff:ff:ff:ff
inet 192.168.175.133/24 brd 192.168.175.255 scope global dynamic ens36
valid_lft 1049sec preferred_lft 1049sec
inet 172.16.0.10/32 scope global ens36
valid_lft forever preferred_lft forever
[root@xt ~]# ip addr del 172.16.0.10 dev ens36 #删除ip操作
[root@xt ~]# ip addr show ens36 #查看ip
3: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:20:b5:07 brd ff:ff:ff:ff:ff:ff
inet 192.168.175.133/24 brd 192.168.175.255 scope global dynamic ens36
valid_lft 1703sec preferred_lft 1703sec
[root@xt ~]# ip addr add 172.16.0.11 dev ens36 #添加ip地址
[root@xt ~]# ip addr list ens36
3: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:20:b5:07 brd ff:ff:ff:ff:ff:ff
inet 192.168.175.133/24 brd 192.168.175.255 scope global dynamic ens36
valid_lft 1647sec preferred_lft 1647sec
inet 172.16.0.11/32 scope global ens36
valid_lft forever preferred_lft forever
[root@xt ~]# ip addr flush dev ens36 #清空ens36ip地址
[root@xt ~]# ip addr list ens36
3: ens36: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:20:b5:07 brd ff:ff:ff:ff:ff:ff
ip route:路由管理
ip route add - add new route
ip route change - change route
ip route replace - change or add new one
ip route add [TYPE] PREFIX via gw_ip [dev IFACE] [src src_ip]
ip route del ip_addr - delete route 删除路由
ip route show - list routes 查看路由
ip route flush - flush routing tables清空路由表
# ip route add 192.168.0.0/24 via 10.0.0.1 dev eth1 src 10.0.20.100
# ip route add default via 192.168.0.1
# ip route del 192.168.0.0/24
[root@xt ~]# ss -tan state ESTABLISHED
Recv-Q Send-Q Local Address:Port Peer Address:Port
0 0 192.168.0.112:22 192.168.0.106:55499
0 0 192.168.0.112:22 192.168.0.106:54701
0 0 192.168.0.112:22 192.168.0.106:55573
0 52 192.168.0.112:22 192.168.0.106:55575
0 0 192.168.0.112:22 192.168.0.106:54864
[root@xt ~]# ss -tan ‘( sport = :22 )‘
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
ESTAB 0 0 192.168.0.112:22 192.168.0.106:55499
ESTAB 0 0 192.168.0.112:22 192.168.0.106:54701
ESTAB 0 0 192.168.0.112:22 192.168.0.106:55573
ESTAB 0 52 192.168.0.112:22 192.168.0.106:55575
ESTAB 0 0 192.168.0.112:22 192.168.0.106:54864
LISTEN 0 128 :::22 :::*
以上使用ifconfig/ip配置IP地址等只是对当前有效,关机或者重启服务等操作配置失效,若需要永久生效则可以通过文件配置、nmcli/nmtui有NetworkManage提供的工具配置。
文件配置
例如添加一块网卡,网卡信息如下:通过配置文件方式
IP地址:192.168.1.77
子网掩码:255255.255.0
网关:192.168.1.254
DNS:192.168.1.254
要求:重启网卡后生效,分别查看IP,网关和DNS是否配置成功
注:以下内容是通过新添加一块网卡ens36来实现,但新添加网卡并没有配置文件,通过复制原有的文件来实现:
[root@xt network-scripts]# cp ifcfg-eno16777736 ifcfg-ens36
[root@xt network-scripts]#vim ifcfg-ens36
BOOTPROTO="none"
NAME="ens36"
DEVICE="ens36"
ONBOOT="yes"
IPADDR=192.168.1.77
NETMASK=255.255.255.0
GATEWAY=192.168.1.254
DNS1=192.168.1.254
[root@xt network-scripts]# systemctl restart network
[root@xt network-scripts]# ifconfig
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.77 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe20:b507 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:20:b5:07 txqueuelen 1000 (Ethernet)
RX packets 4 bytes 989 (989.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 20 bytes 2076 (2.0 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@xt network-scripts]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.0.1 0.0.0.0 UG 100 0 0 eno16777736
0.0.0.0 192.168.1.254 0.0.0.0 UG 101 0 0 ens36
192.168.0.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736
192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens36
[root@xt network-scripts]# cat /etc/resolv.conf
# Generated by NetworkManager
search xt.com
nameserver 221.7.128.68
nameserver 221.7.136.68
nameserver 192.168.1.254
nmcli命令:nmcli - command‐line管理NetworkManage服务,命令参数可以补齐,创建或修改 的会话配置信息会自动保存为网卡配置文件,无需手动编辑,重启依然生效。
nmcli [ OPTIONS ] OBJECT { COMMAND | help }
标签:script packet prot 解析 0.11 slot 改ip iproute 管理
原文地址:http://blog.51cto.com/12580678/2339122