一、网络属性概述
在计算机网络中,某台主机要想接入网络实现通信的话,一般要配置IP地址、子网掩码、网关、DNS服务器地址等属性。其中IP地址是主机在互联网上的逻辑地址,通过IP地址可定位互联网上的某一台主机以实现网络的三层通信;而子网掩码与IP地址按位进行“与”运算得到主机所在网段地址,可快速确定用户主机所在网络(网段)。配置了IP地址和子网掩码,就可以实现本地通信了,此外实现本地通信还需要主机的MAC地址,因为IP地址之间的通信最终要映射到MAC地址才能完成,而MAC地址一般在厂商生产时就提供了,且不可改变。能够实现本地通信之后,如果要跨网络与互联网上的其他主机通信,则需要(默认)网关,通过网关可以将本地主机的数据报文发送出去并将外网的数据包接收返回本地局域网的某台主机。在此基础上,指向DNS服务器地址可实现基于主机名通信,例如,用户要访问网站只要键入主机名(例如:www.baidu.com)即可,而不需要知道互联网上各个服务器的IP地址。
以下介绍在Linux上配置网络属性以实现将Linux主机接入网络中。
二、网络属性配置
实现网络属性配置的两种方式:
- 命令行配置(ifcfg家族,iproute家族,nm家族)
- 配置文件
- 文本图形化配置
命令行配置:
ifcfg家族相关命令:ifconfig, iproute, netstat
iproute家族相关命令:ip, ss
nm家族相关命令:nmcli, nmtui
这里先介绍ifcfg家族相关命令。
ifconfig命令:
ifconfig可用于查看和配置网络接口的属性。
ifconfig命令用法:
ifconfig [-v] [-a] [-s] [interface]
ifconfig [-v] interface [aftype] options | address ...
常用选项:
[-]promisc:开启或关闭混杂模式;
示例:
查看当前处于激活状态的网络接口信息:
[[email protected] ~]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 从flags可知该接口已启用,支持广播、组播,mtu值 inet 10.10.10.140 netmask 255.255.255.0 broadcast 10.10.10.255 IPv4地址 子网掩码 广播地址 inet6 fe80::20c:29ff:fec8:ff4e prefixlen 64 scopeid 0x20<link> IPv6地址 掩码长度 作用域,link表示仅该接口有效 ether 00:0c:29:c8:ff:4e txqueuelen 1000 (Ethernet) 网卡接口的MAC地址 传输队列长度 接口类型为Ethernet RX packets 266 bytes 26083 (25.4 KiB) 上行表示此接口接收的报文个数,总字节数 RX errors 0 dropped 0 overruns 0 frame 0 接收报文错误数,丢弃数,溢出数,冲突的帧数 TX packets 141 bytes 20086 (19.6 KiB) 上行表示此接口发送的报文个数,总字节数 TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 发送报文错误数,丢弃数,溢出数,载荷数,冲突数 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 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
如果要显示所有网卡接口的信息(包括未激活的网卡接口),则可加上-a选项:
[[email protected] ~]# ifconfig -a ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.10.10.140 netmask 255.255.255.0 broadcast 10.10.10.255 inet6 fe80::20c:29ff:fec8:ff4e prefixlen 64 scopeid 0x20<link> ether 00:0c:29:c8:ff:4e txqueuelen 1000 (Ethernet) RX packets 318 bytes 30857 (30.1 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 192 bytes 24612 (24.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens38: flags=4098<BROADCAST,MULTICAST> mtu 1500 ether 00:0c:29:c8:ff:58 txqueuelen 1000 (Ethernet) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 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 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
可以显示指定网卡接口的信息:
[[email protected] ~]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 10.10.10.140 netmask 255.255.255.0 broadcast 10.10.10.255 inet6 fe80::20c:29ff:fec8:ff4e prefixlen 64 scopeid 0x20<link> ether 00:0c:29:c8:ff:4e txqueuelen 1000 (Ethernet) RX packets 341 bytes 32973 (32.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 214 bytes 28076 (27.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
给未激活的网卡ens38配置IP地址,配置后即可启用:
[[email protected] ~]# ifconfig ens38 192.168.10.1/24 up [[email protected] ~]# ifconfig ens38 ens38: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 #UP表示已启用 inet 192.168.10.1 netmask 255.255.255.0 broadcast 192.168.10.255 inet6 fe80::20c:29ff:fec8:ff58 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:c8:ff:58 txqueuelen 1000 (Ethernet) RX packets 1 bytes 60 (60.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8 bytes 648 (648.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
需要注意的是,上述配置IP地址是以掩码长度进行配置的,也可直接写出掩码配置IP地址:
[[email protected] ~]# ifconfig ens38 192.168.10.1 netmask 255.255.255.0 up
可开启混杂模式:
[[email protected] ~]# ifconfig ens33 promisc [[email protected] ~]# ifconfig ens33 ens33: flags=4419<UP,BROADCAST,RUNNING,PROMISC,MULTICAST> mtu 1500 混杂模式已开启 inet 10.10.10.140 netmask 255.255.255.0 broadcast 10.10.10.255 inet6 fe80::20c:29ff:fec8:ff4e prefixlen 64 scopeid 0x20<link> ether 00:0c:29:c8:ff:4e txqueuelen 1000 (Ethernet) RX packets 762 bytes 71714 (70.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 538 bytes 65928 (64.3 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
关闭混杂模式:
[[email protected] ~]# ifconfig ens33 -promisc [[email protected] ~]# ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 #混杂模式已关闭 inet 10.10.10.140 netmask 255.255.255.0 broadcast 10.10.10.255 inet6 fe80::20c:29ff:fec8:ff4e prefixlen 64 scopeid 0x20<link> ether 00:0c:29:c8:ff:4e txqueuelen 1000 (Ethernet) RX packets 836 bytes 78408 (76.5 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 593 bytes 72218 (70.5 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
route命令:
用于查看和管理路由
路由条目类型:
主机路由:目标地址为单个IP地址;
网络路由:目标地址为IP网络/网段;
默认路由:目标地址为任意主机/网段;
示例:
查看路由表信息:
[[email protected] ~]# route Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.10.10.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 link-local 0.0.0.0 255.255.0.0 U 1002 0 0 ens33 192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 ens38 default gateway 0.0.0.0 UG 0 0 0 ens33
可以感觉到在键入‘route‘后卡了一下才显示出来,因为在显示路由条目时route会自动尝试解析路由条目;一般加上-n选项使route不自动解析:
[[email protected] ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.10.10.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 ens33 192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 ens38 0.0.0.0 10.10.10.2 0.0.0.0 UG 0 0 0 ens33 #Destination:目标路由; #Gateway:网关;如果是默认路由的网关则可称为默认网关;其中Gateway为0.0.0.0的路由条目为无需 网关之意,表示该目标路由为本地主机路由,又称直连路由。 #Genmask:子网掩码; #Flags:路由的标志位;U表示启用状态,G表示默认网关,H表示主机路由; #Metric:度量值;表示到达该目标路由的开销; #Iface:使用哪个接口将报文发送出去;
添加路由条目的语法格式为:
route add [-net|-host] target [netmask Nm] [gw Gw] [[dev] If]
示例:
添加主机到达10.0.0.0/8的路由条目,经由下一跳为192.168.10.1:
[[email protected] ~]# route add -net 10.0.0.0/8 gw 192.168.10.1 #注意:下一跳(网关)必须与本地网络上某个主机IP地址处在同一网段;接口会自动识别,可不指定; [[email protected] ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.0.0.0 192.168.10.1 255.0.0.0 UG 0 0 0 ens38 10.10.10.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 ens33 192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 ens38 0.0.0.0 10.10.10.2 0.0.0.0 UG 0 0 0 ens33
添加默认网关:
方式一: [[email protected] ~]# route add -net 0.0.0.0/0 gw 10.10.10.2 方式二: [[email protected] ~]# route add default gw 10.10.10.2
删除路由条目的语法格式为:
route del [-net|-host] target [gw Gw] [netmask Nm] [[dev] If]
示例:
删除刚添加的路由条目10.0.0.0/8:
[[email protected] ~]# route del -net 10.0.0.0/8 [[email protected] ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.10.10.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 ens33 192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 ens38 0.0.0.0 10.10.10.2 0.0.0.0 UG 0 0 0 ens33
删除默认网关:
方式一: [[email protected] ~]# route del default [[email protected] ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.10.10.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 ens33 192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 ens38 #默认路由已删除; 方式二: [[email protected] ~]# route del -net 0.0.0.0/0
netstat命令:
用于显示网络连接状态、路由表信息、接口的统计数据、伪装连接、多播成员关系等。
常用选项:
-t:显示tcp协议相关的连接;tcp连接均有其连接状态,而所有连接状态在FSM内进行转换;
-u:显示udp协议相关的连接,但无状态之说;
-l:显示处于监听状态的连接;
-a:显示所有状态的连接;
-r:显示路由表信息;
-n:以数字格式显示IP和Port;
-p:显示相关进程和PID;
-e:以扩展格式显示;
-w:显示raw socket相关的连接;
-numeric-hosts:以数字格式显示主机IP地址;
-numeric-ports:以数字格式显示Port;
传输层协议:
TCP:面向连接的协议;双方通信开始前要建立虚链路,通信完成后还要拆除连接;
UDP:面向无连接的协议;直接发送数据报文而无需实现建立虚链路;
示例:
①显示路由表信息:
[[email protected] ~]# netstat -rn Kernel IP routing table Destination Gateway Genmask Flags MSS Window irtt Iface 10.10.10.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 ens33 192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 ens38 0.0.0.0 10.10.10.2 0.0.0.0 UG 0 0 0 ens33
显示结果类似‘route -n‘.
②显示网络连接状态:
语法格式:
netstat [--tcp|-t] [--udp|-u] [--raw|-w] [--listening|-l] [--all|-a] [--numeric|-n] [--numeric-hosts] [--numeric-ports] [--extend|-e[--extend|-e]] [--program|-p]
示例:
显示所有状态的连接(包括监听和未监听的):
[[email protected] ~]# netstat -a Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 *:54569 *:* LISTEN tcp 0 0 *:sunrpc *:* LISTEN tcp 0 0 *:ssh *:* LISTEN tcp 0 0 localhost:ipp *:* LISTEN tcp 0 0 localhost:smtp *:* LISTEN .....(省略)..... Active UNIX domain sockets (servers and established) Proto RefCnt Flags Type State I-Node Path unix 18 [ ] DGRAM 14606 /dev/log unix 2 [ ACC ] STREAM LISTENING 15536 @/var/run/hald/dbus-Rat1Ns9j6z unix 2 [ ACC ] STREAM LISTENING 16112 /var/run/pcscd.comm unix 2 [ ACC ] STREAM LISTENING 14153 @ISCSID_UIP_ABSTRACT_NAMESPACE .....(省略).....
显示tcp协议相关的所有状态的连接(socket):
[[email protected] ~]# netstat -ta Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 *:54569 *:* LISTEN tcp 0 0 *:sunrpc *:* LISTEN tcp 0 0 *:ssh *:* LISTEN tcp 0 0 localhost:ipp *:* LISTEN tcp 0 0 localhost:smtp *:* LISTEN tcp 0 64 10.10.10.139:ssh 10.10.10.1:52401 ESTABLISHED tcp 0 0 10.10.10.139:ssh 10.10.10.1:56538 ESTABLISHED tcp 0 0 *:sunrpc *:* LISTEN tcp 0 0 *:44980 *:* LISTEN tcp 0 0 *:ssh *:* LISTEN tcp 0 0 localhost:ipp *:* LISTEN tcp 0 0 localhost:smtp *:* LISTEN
显示tcp协议相关的所有状态的连接(socket):
[[email protected] ~]# netstat -ua Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 *:sunrpc *:* udp 0 0 *:756 *:* udp 0 0 *:ipp *:* udp 0 0 *:ldaps *:* udp 0 0 *:36110 *:* udp 0 0 localhost:814 *:* udp 0 0 *:sunrpc *:* udp 0 0 *:756 *:* udp 0 0 *:55832 *:*
以数字格式显示tcp所有状态的连接(socket):
[[email protected] ~]# netstat -tan Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:54569 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 64 10.10.10.139:22 10.10.10.1:52401 ESTABLISHED tcp 0 0 10.10.10.139:22 10.10.10.1:56538 ESTABLISHED tcp 0 0 :::111 :::* LISTEN tcp 0 0 :::44980 :::* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:631 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN
以数字格式显示udp所有状态的连接(socket):
[[email protected] ~]# netstat -uan Active Internet connections (servers and established) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 0.0.0.0:111 0.0.0.0:* udp 0 0 0.0.0.0:756 0.0.0.0:* udp 0 0 0.0.0.0:631 0.0.0.0:* udp 0 0 0.0.0.0:636 0.0.0.0:* udp 0 0 0.0.0.0:36110 0.0.0.0:* udp 0 0 127.0.0.1:814 0.0.0.0:* udp 0 0 :::111 :::* udp 0 0 :::756 :::* udp 0 0 :::55832 :::*
显示处于监听状态的tcp连接(socket):
[[email protected] ~]# netstat -tnl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:54569 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 0 :::111 :::* LISTEN tcp 0 0 :::44980 :::* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:631 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN
显示处于监听状态的udp连接(socket):
[[email protected] ~]# netstat -unl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State udp 0 0 0.0.0.0:111 0.0.0.0:* udp 0 0 0.0.0.0:756 0.0.0.0:* udp 0 0 0.0.0.0:631 0.0.0.0:* udp 0 0 0.0.0.0:636 0.0.0.0:* udp 0 0 0.0.0.0:36110 0.0.0.0:* udp 0 0 127.0.0.1:814 0.0.0.0:* udp 0 0 :::111 :::* udp 0 0 :::756 :::* udp 0 0 :::55832 :::*
显示处于监听状态的tcp/udp连接(socket):
[[email protected] ~]# netstat -tunl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 0.0.0.0:54569 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN tcp 0 0 :::111 :::* LISTEN tcp 0 0 :::44980 :::* LISTEN tcp 0 0 :::22 :::* LISTEN tcp 0 0 ::1:631 :::* LISTEN tcp 0 0 ::1:25 :::* LISTEN udp 0 0 0.0.0.0:111 0.0.0.0:* udp 0 0 0.0.0.0:756 0.0.0.0:* udp 0 0 0.0.0.0:631 0.0.0.0:* udp 0 0 0.0.0.0:636 0.0.0.0:* udp 0 0 0.0.0.0:36110 0.0.0.0:* udp 0 0 127.0.0.1:814 0.0.0.0:* udp 0 0 :::111 :::* udp 0 0 :::756 :::* udp 0 0 :::55832 :::*
显示处于监听状态的所有连接(scoket),并显示相应的进程及PID号:
[[email protected] ~]# 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:54569 0.0.0.0:* LISTEN 1910/rpc.statd tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1853/rpcbind tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 2223/sshd tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 1985/cupsd tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2305/master tcp 0 0 :::111 :::* LISTEN 1853/rpcbind tcp 0 0 :::44980 :::* LISTEN 1910/rpc.statd tcp 0 0 :::22 :::* LISTEN 2223/sshd tcp 0 0 ::1:631 :::* LISTEN 1985/cupsd tcp 0 0 ::1:25 :::* LISTEN 2305/master udp 0 0 0.0.0.0:111 0.0.0.0:* 1853/rpcbind udp 0 0 0.0.0.0:756 0.0.0.0:* 1853/rpcbind udp 0 0 0.0.0.0:631 0.0.0.0:* 1985/cupsd udp 0 0 0.0.0.0:636 0.0.0.0:* 1781/portreserve udp 0 0 0.0.0.0:36110 0.0.0.0:* 1910/rpc.statd udp 0 0 127.0.0.1:814 0.0.0.0:* 1910/rpc.statd udp 0 0 :::111 :::* 1853/rpcbind udp 0 0 :::756 :::* 1853/rpcbind udp 0 0 :::55832 :::* 1910/rpc.statd
③查看接口的统计数据:
语法格式:
netstat {--interfaces|-I|-i} [--all|-a] [--extend|-e] [--verbose|-v] [--program|-p] [--numeric|-n] [--numeric-hosts] [--numeric-ports]
示例:
显示所有接口的统计数据:
[[email protected] ~]# netstat -i Kernel Interface table Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg ens33 1500 5128 0 0 0 3702 0 0 0 BMRU ens38 1500 449 0 0 0 8 0 0 0 BMRU lo 65536 3 0 0 0 3 0 0 0 LRU 或者: [[email protected] ~]# netstat -I
显示指定接口的统计数据:
[[email protected] ~]# netstat -Iens33 #注意-I选项和指定接口之间不能有空格; Kernel Interface table Iface MTU RX-OK RX-ERR RX-DRP RX-OVR TX-OK TX-ERR TX-DRP TX-OVR Flg ens33 1500 5155 0 0 0 3720 0 0 0 BMRU
接下来介绍iproute2家族相关命令。因为iproute家族是与Linux内湖密切相关的程序包,因此iproute家族中的很多命令功能都是直接倒置到Linux内核上的,所以iproute家族的命令在执行起来相对效率较高。
ip命令:用于管理/查看路由、接口、策略路由和隧道等。
ip addr:用于管理IP地址;
ip link:用于网络接口的配置;
ip netns:用于管理网络名称空间;
ip route:用于管理路由表;
ip link命令:用于网络接口的配置
ip link set:修改设备属性;
ip link list|show:查看二层设备属性;
ip link help:查看简要使用帮助;
①ip link|show:查看二层设备属性
示例:
查看主机上各网卡接口的二层属性:
[[email protected] ~]# ip link #此时list或show可省略; 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:c8:ff:4e brd ff:ff:ff:ff:ff:ff 3: ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff
查看ens38接口的二层属性:
[[email protected] ~]# ip link show ens38 #查看指定接口属性时,show或list不能省略; 3: ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff
②ip link set:修改设备属性
主要属性:
dev NAME (default):指定要管理的设备;dev关键字可省略;
up | down:启用或禁用网络接口;
multicast { on | off }:启用或禁用组播功能;
name NEWNAME:重命名接口;
mtu MTU_NUMBER:设置MTU的大小;默认为1500;
netns { PID | NAME }:将接口移动至指定的网络名称空间中;要求Linux内核支持该功能(CentOS 7支持)
示例:
禁用ens38接口的使用:
先查看ens38接口的状态: [[email protected] ~]# ip link show ens38 3: ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 #ens38接口处于UP状态; link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff
开始禁用ens38接口:
[[email protected] ~]# ip link set ens38 down [[email protected] ~]# ip link show ens38 3: ens38: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 1000 #ens38接口处于DOWN状态; link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff
重新启用ens38接口:
[[email protected] ~]# ip link set ens38 up [[email protected] ~]# ip link show ens38 3: ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 #ens38接口已重新启用; link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff
重命名ens38接口为ens30:
[[email protected] ~]# ip link set ens38 name ens30 RTNETLINK answers: Device or resource busy #显示繁忙,接口必须在DOWN状态下才可重命名; [[email protected] ~]# ip link set ens38 down [[email protected] ~]# ip link set ens38 name ens30 #命令键入后没有任何提示,说明修改完成; [[email protected] ~]# ip link show ens30 3: ens30: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT qlen 1000 link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff #查看ens30接口二层属性; [[email protected] ~]# ip link show ens38 Device "ens38" does not exist. #原来的ens38已不存在; [[email protected] ~]# ip link set ens30 up #启用修改完成的ens30接口; [[email protected] ~]# ip link show ens30 3: ens30: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff
将接口ens30的MTU改为1400,且关闭multicast功能:
[[email protected] ~]# ip link set ens30 mtu 1400 multicast off [[email protected] ~]# ip link show ens30 3: ens30: <BROADCAST,UP,LOWER_UP> mtu 1400 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 #ens的flags中已无MULTICAST属性,且MTU已修改为1400; link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff
创建名称空间mynet,并将ens30接口移至名称空间mynet中:
[[email protected] ~]# ip netns add mynet #先创建名称空间mynet; [[email protected] ~]# ip netns list #查看已有的名称空间; mynet [[email protected] ~]# ip link set ens30 netns mynet #将ens30接口移至名称空间mynet中; [[email protected] ~]# ip netns exec mynet ip link show #在名称空间mynet之上运行命令"ip link show",而"ip netns exec mynet"类似于进入虚拟的名称空间中,在此之上运行COMMAND. 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 3: ens30: <BROADCAST> mtu 1400 qdisc noop state DOWN mode DEFAULT qlen 1000 link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff [[email protected] ~]# ip link show #重新查看接口属性; 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:c8:ff:4e brd ff:ff:ff:ff:ff:ff #可以发现ens30已经消失了!因为它已经跑到名称空间mynet里去了。 #基于netns这种机制可以搭建复杂的内部网络/虚拟网络。
ip netns:用于管理网络名称空间
ip netns list:列出所有的netns;
ip netns add NAME:创建指定的netns;
ip netns del NAME:删除指定的netns;
ip netns exec COMMAND:在指定的netns上运行COMMAND;
①ip netns add NAME:创建指定的netns
示例:
创建名为Jack的netns:
[[email protected] ~]# ip netns add Jack
②ip netns list:列出所有的netns
示例:
[[email protected] ~]# ip netns list Jack mynet #这里显示刚刚创建的Jack以及之前创建的mynet;
③ip netns del NAME:删除指定的netns
示例:
删除mynet:
[[email protected] ~]# ip netns del mynet [[email protected] ~]# ip netns list Jack
④ip netns exec [NAME] COMMAND:在指定的netns上运行COMMAND
示例:
将ens38接口移至Jack(netns)中,并为之配置IP地址:
[[email protected] ~]# ip link #首先查看当前主机上的所有接口属性; 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT qlen 1000 link/ether 00:0c:29:c8:ff:4e brd ff:ff:ff:ff:ff:ff 3: ens38: <BROADCAST> mtu 1400 qdisc noop state DOWN mode DEFAULT qlen 1000 link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff [[email protected] ~]# ip netns list #列出所有的netns; Jack [[email protected] ~]# ip link set ens38 netns Jack #将ens38接口放进Jack(netns)中; [[email protected] ~]# ip netns exec Jack ip link #在Jack(netns)中运行"ip link"; 1: lo: <LOOPBACK> mtu 65536 qdisc noop state DOWN mode DEFAULT qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 3: ens38: <BROADCAST> mtu 1400 qdisc noop state DOWN mode DEFAULT qlen 1000 link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff [[email protected] ~]# ip netns exec Jack ip addr add 172.16.10.1/24 dev ens38 #在Jack(netns)中为挪进去的ens38接口配置IP/NETMASK; [[email protected] ~]# ip netns exec Jack ip addr show ens38 #在Jack(netns)中查看ens38接口IP地址; 3: ens38: <BROADCAST> mtu 1400 qdisc noop state DOWN qlen 1000 link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff inet 172.16.10.1/24 scope global ens38 valid_lft forever preferred_lft forever
ip addr命令:用于管理IP地址
ip addr list|show:查看IP地址;
ip addr add:添加IP地址;
ip addr delete:删除IP地址;
ip addr flush:清空IP地址;
①ip addr list|show:查看IP地址
语法格式:
ip addr list [IFACE]
示例:
查看所有网络接口的IP地址:
[[email protected] ~]# ip addr list 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c8:ff:4e brd ff:ff:ff:ff:ff:ff inet 10.10.10.140/24 brd 10.10.10.255 scope global ens33 valid_lft forever preferred_lft forever inet 10.10.10.138/24 brd 10.10.10.255 scope global secondary ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fec8:ff4e/64 scope link valid_lft forever preferred_lft forever 3: ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff inet 192.168.10.1/24 brd 192.168.10.255 scope global ens38 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fec8:ff58/64 scope link valid_lft forever preferred_lft forever 或者: [[email protected] ~]# ip addr show
查看ens33接口上的所有IP地址:
[[email protected] ~]# ip addr show ens33 2: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c8:ff:4e brd ff:ff:ff:ff:ff:ff inet 10.10.10.140/24 brd 10.10.10.255 scope global ens33 valid_lft forever preferred_lft forever inet 10.10.10.138/24 brd 10.10.10.255 scope global secondary ens33 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fec8:ff4e/64 scope link valid_lft forever preferred_lft forever 或者: [[email protected] ~]# ip addr list ens33
②ip addr add:添加IP地址
语法格式:
ip addr add IFADDR dev IFACE
其他属性:
[label NAME]:为网卡接口另外添加的IP地址指定别名;
[broadcast ADDRESS]:给定广播地址;可不指定,系统会根据IP和NETMASK自动计算得出;
[scope SCOPE_VALUE]:指定作用域;SCOPE_VALUE主要有global, link, host;
SCOPE_VALUE:
global:全局有效;即指定添加的接口配置既可实现本地通信,又可实现跨网络通信;
link:仅接口有效;即只能与使用此接口配置的IP地址通信;
host:仅主机有效;即只能与本地主机上的IP地址进行通信;
示例:
添加IP地址:192.168.20.1/24,接口为ens38:
[[email protected] ~]# ip addr add 192.168.20.1/24 dev ens38 [[email protected] ~]# ip addr show ens38 3: ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff inet 192.168.10.1/24 brd 192.168.10.255 scope global ens38 valid_lft forever preferred_lft forever inet 192.168.20.1/24 scope global ens38 #显示已添加该地址,并作为ens38接口的secondary地址,如果没有指定别名,则该地址在ifconfig命令中不会显示; valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fec8:ff58/64 scope link valid_lft forever preferred_lft forever
添加IP地址:172.16.10.1/24,接口为ens38,接口别名为ens38:0:
[[email protected] ~]# ip addr add 172.168.10.1/24 dev ens38 label ens38:0 [[email protected] ~]# ip addr show ens38 3: ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff inet 192.168.10.1/24 brd 192.168.10.255 scope global ens38 valid_lft forever preferred_lft forever inet 192.168.20.1/24 scope global ens38 valid_lft forever preferred_lft forever inet 172.168.10.1/24 scope global ens38:0 #显示已添加,网卡别名为ens38:0,并作为ens38的secondary地址; valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fec8:ff58/64 scope link valid_lft forever preferred_lft forever 用ifconfig显示: [[email protected] ~]# ifconfig ens38:0 ens38:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.168.10.1 netmask 255.255.255.0 broadcast 0.0.0.0 ether 00:0c:29:c8:ff:58 txqueuelen 1000 (Ethernet)
③ip addr delete:删除IP地址
语法格式:
ip addr delete IFADDR dev IFACE
用法和ip addr add相同。
示例:
删除刚添加的IP地址:192.168.20.1/24
[[email protected] ~]# ip addr show ens38 3: ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff inet 192.168.10.1/24 brd 192.168.10.255 scope global ens38 valid_lft forever preferred_lft forever inet 192.168.20.1/24 scope global ens38 #要删除的IP地址; valid_lft forever preferred_lft forever inet 172.168.10.1/24 scope global ens38:0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fec8:ff58/64 scope link valid_lft forever preferred_lft forever [[email protected] ~]# [[email protected] ~]# ip addr delete 192.168.20.1/24 dev ens38 [[email protected] ~]# ip addr show ens38 3: ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff inet 192.168.10.1/24 brd 192.168.10.255 scope global ens38 valid_lft forever preferred_lft forever inet 172.168.10.1/24 scope global ens38:0 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fec8:ff58/64 scope link valid_lft forever preferred_lft forever 显示已经删除。
④ip addr flush:清空IP地址
语法格式:
ip addr flush dev IFACE
[[email protected] ~]# ip addr flush dev ens38 [[email protected] ~]# ip addr show ens38 3: ens38: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff
ip route:用于管理路由表
ip route show:显示路由表;
ip route add:添加路由;
ip route change:修改路由;
ip route replace:修改路由或添加路由;
ip route delete:删除路由;
ip route get:获得单条路由的详细信息;
ip route flush:清空路由表;
相关属性:
src SOURCE_IP:当一个网卡有多个地址时可以指定以哪个IP地址作为出口地址(源地址);
①ip route show:显示路由表
语法格式:
ip route show [TYPE PREFIX]
示例:
显示主机上路由表信息:
[[email protected] ~]# ip route show default via 10.10.10.2 dev ens33 10.10.10.0/24 dev ens33 proto kernel scope link src 10.10.10.140 169.254.0.0/16 dev ens33 scope link metric 1002 172.16.0.0/16 via 192.168.20.1 dev ens38 src 192.168.30.1 172.16.10.0/24 via 10.10.10.2 dev ens33 192.168.10.0/24 dev ens38 proto kernel scope link src 192.168.10.1 192.168.20.0/24 dev ens38 proto kernel scope link src 192.168.20.1 192.168.30.0/24 dev ens38 proto kernel scope link src 192.168.30.1
显示172.16.0.0/16这条路由的信息:
[[email protected] ~]# ip route show 172.16.0.0/16 172.16.0.0/16 via 192.168.20.1 dev ens38 src 192.168.30.1
②ip route add:添加路由
语法格式:
ip route add TYPE PREFIX via GW [dev IFACE] [src SOURCE_IP]
示例:
添加一条172.16.10.0/24的路由条目,下一跳为10.10.10.140:
[[email protected] ~]# ip route add 172.16.10.0/24 via 10.10.10.2 [[email protected] ~]# route -n Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.10.10.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 ens33 172.16.10.0 10.10.10.2 255.255.255.0 UG 0 0 0 ens33 0.0.0.0 10.10.10.2 0.0.0.0 UG 0 0 0 ens33
添加一条到172.16.0.0/24网段的路由, 下一跳是192.168.20.1,网卡接口是ens38, 源地址是192.168.30.1:
[[email protected] ~]# ip addr add 192.168.10.1/24 dev ens38 #为ens38接口添加地址; [[email protected] ~]# ip addr add 192.168.20.1/24 dev ens38 [[email protected] ~]# ip addr add 192.168.30.1/24 dev ens38 [[email protected] ~]# ip addr show ens38 3: ens38: <BROADCAST,UP,LOWER_UP> mtu 1400 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:c8:ff:58 brd ff:ff:ff:ff:ff:ff inet 192.168.10.1/24 scope global ens38 valid_lft forever preferred_lft forever inet 192.168.20.1/24 scope global ens38 valid_lft forever preferred_lft forever inet 192.168.30.1/24 scope global ens38 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fec8:ff58/64 scope link valid_lft forever preferred_lft forever [[email protected] ~]# ip route add 172.16.0.0/16 via 192.168.20.1 src 192.168.30.1 [[email protected] ~]# ip route list default via 10.10.10.2 dev ens33 10.10.10.0/24 dev ens33 proto kernel scope link src 10.10.10.140 169.254.0.0/16 dev ens33 scope link metric 1002 172.16.0.0/16 via 192.168.20.1 dev ens38 src 192.168.30.1 #src为192.168.30.1 172.16.10.0/24 via 10.10.10.2 dev ens33 192.168.10.0/24 dev ens38 proto kernel scope link src 192.168.10.1 192.168.20.0/24 dev ens38 proto kernel scope link src 192.168.20.1 192.168.30.0/24 dev ens38 proto kernel scope link src 192.168.30.1
③ip route change:修改路由
语法格式:
ip route change TYPE PREFIX via GW [dev IFACE] [src SOURCE_IP]
示例:
将172.16.0.0/16这条路由的源地址(src)改为192.168.10.1:
[[email protected] ~]# ip route show 172.16.0.0/16 #先查看172.16.0.0/16路由信息; 172.16.0.0/16 via 192.168.20.1 dev ens38 src 192.168.30.1 #源地址为192.168.30.1; [[email protected] ~]# ip route change 172.16.0.0/16 src 192.168.10.1 #将172.160.0.0/16这条路由的源地址改为192.168.10.1; [[email protected] ~]# ip route show 172.16.0.0/16 172.16.0.0/16 dev ens38 scope link src 192.168.10.1 #源地址已改动;
④ip route replace:修改路由或添加路由
语法格式:
ip route replace TYPE PREFIX via GW [dev IFACE] [src SOURCE_IP]
示例:
将30.0.0.0/8这条路由(该路由事先不存在)的下一跳改为192.168.20.1,源地址改为192.168.10.1:
[[email protected] ~]# ip route change 30.0.0.0/8 via 192.168.20.1 src 192.168.10.1 RTNETLINK answers: No such file or directory #如果使用change命令则无法修改,因为路由30.0.0.0/8事先不存在; [[email protected] ~]# ip route replace 30.0.0.0/8 via 192.168.20.1 src 192.168.10.1 #如果使用replace命令则在键入后无任何信息输出; [[email protected] ~]# ip route show 30.0.0.0/8 #查看30.0.0.0/8路由的信息,以检测该路由是否存在; 30.0.0.0/8 via 192.168.20.1 dev ens38 src 192.168.10.1 #说明如果指定路由不存在则会自动按指定的属性创建该路由; #ip route replace命令类似于rpm -Uvh(若程序包原先存在则升级,不存在则自动安装);
⑤ip route delete:删除路由
语法格式:
ip route del TYPE PREFIX
示例:
删除30.0.0.0/8这条路由:
[[email protected] ~]# ip route del 30.0.0.0/8 [[email protected] ~]# ip route show default via 10.10.10.2 dev ens33 10.10.10.0/24 dev ens33 proto kernel scope link src 10.10.10.140 169.254.0.0/16 dev ens33 scope link metric 1002 172.16.0.0/16 dev ens38 scope link src 192.168.10.1 172.16.10.0/24 via 10.10.10.2 dev ens33 192.168.10.0/24 dev ens38 proto kernel scope link src 192.168.10.1 192.168.20.0/24 dev ens38 proto kernel scope link src 192.168.20.1 192.168.30.0/24 dev ens38 proto kernel scope link src 192.168.30.1 #可看出30.0.0.0/8路由已被删除;
⑥ip route get:获得单条路由的详细信息
语法格式:
ip route get TYPE PREFIX
示例:
获取192.168.10.0/24路由信息:
[[email protected] ~]# ip route get 192.168.10.0/16 broadcast 192.168.10.0 dev ens38 src 192.168.10.1 cache <local,brd>
⑦ip route flush:清空路由表
语法格式:
ip route flush TYPE PREFIX
示例:
[[email protected] ~]# ip route flush 192.168.10.0/24 #删除指定路由; [[email protected] ~]# ip route show default via 10.10.10.2 dev ens33 10.10.10.0/24 dev ens33 proto kernel scope link src 10.10.10.140 169.254.0.0/16 dev ens33 scope link metric 1002 172.16.0.0/16 dev ens38 scope link src 192.168.10.1 172.16.10.0/24 via 10.10.10.2 dev ens33 192.168.20.0/24 dev ens38 proto kernel scope link src 192.168.20.1 192.168.30.0/24 dev ens38 proto kernel scope link src 192.168.30.1 #指定路由已被删除,功能类似del;
ss命令:
用于获取socket统计信息等,功能大致与netsata相同,ss工具是用于取代netstat的,因为很多用法相同;相比netstat而言,ss命令主要在选项上多支持了-o和-m选项,并且支持过滤功能;此外,在网络连接数量众多时,ss命令的执行性能要比netstat高得多。
语法格式:
ss [options] [ FILTER ]
其中,FILTER := [ state TCP-STATE ] [ EXPRESSION ],表示过滤条件;
EXPRESSION:
dport = :目的端口;
sport = :源端口;
常用选项:
-t:显示tcp相关的连接;
-u:显示udp相关的连接;
-w:显示raw socket相关的连接;
-a:显示所有状态的连接;
-l:显示处于监听状态的连接;
-n:以数字格式显示IP和Port;
-p:显示相应的进程及其PID;
-e:以长格式显示;
-m:显示内存用量;
-o:显示计数器信息;
示例:
显示源端口为22或目标端口为22的tcp连接:
[[email protected] ~]# ss -tan ‘( dport = :22 or sport = :22 )‘ State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 128 *:22 *:* ESTAB 0 52 10.10.10.140:22 10.10.10.1:56108 LISTEN 0 128 :::22 :::*
显示连接状态为ESTABLISHED的连接:
[[email protected] ~]# ss -tan state established Recv-Q Send-Q Local Address:Port Peer Address:Port 0 52 10.10.10.140:22 10.10.10.1:56108
以上为iproute家族命令的介绍。此外,还可指定本地主机名以及DNS服务器的地址:
(1)DNS服务器的指定:
配置文件:/etc/resolv.conf
用法示例:
添加DNS服务器地址:114.114.114.114:
[[email protected] ~]# vim /etc/resolv.conf nameserver 114.114.114.114
测试配置的DNS指向是否有效:
测试工具:dig/nslookup/host
①dig工具:
用法:
dig -t A FQDN:由FQDN解析出IP地址;(FQDN即为主机名)
dig -x IP:由IP地址反解出FQDN;
用法示例:
由FQDN解析出IP地址:
[[email protected] ~]# dig -t A public1.114dns.com. ; <<>> DiG 9.9.4-RedHat-9.9.4-37.el7 <<>> -t A public1.114dns.com. ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 64858 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;public1.114dns.com. IN A ;; ANSWER SECTION: public1.114dns.com. 441 IN A 114.114.114.114 #解出地址,说明该DNS服务器地址有效; ;; Query time: 38 msec ;; SERVER: 114.114.114.114#53(114.114.114.114) ;; WHEN: Sat Feb 11 19:38:48 CST 2017 ;; MSG SIZE rcvd: 63
有时还可反解,不过大多数IP地址不支持反解:
[[email protected] ~]# dig -x 114.114.114.114 #由IP地址解析出FQDN; ; <<>> DiG 9.9.4-RedHat-9.9.4-37.el7 <<>> -x 114.114.114.114 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 48274 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; udp: 4096 ;; QUESTION SECTION: ;114.114.114.114.in-addr.arpa. IN PTR ;; ANSWER SECTION: 114.114.114.114.in-addr.arpa. 330 IN PTR public1.114dns.com. #获取FQDN,反解成功; ;; Query time: 39 msec ;; SERVER: 114.114.114.114#53(114.114.114.114) ;; WHEN: Sat Feb 11 19:42:21 CST 2017 ;; MSG SIZE rcvd: 89
注意:dig工具在解析过程中不会检查本机HOST表(即/etc/hosts文件);
②nslookp工具:
用法示例:
由FQDN解析出IP地址:
[[email protected] ~]# nslookup 114.114.114.114 Server: 114.114.114.114 Address: 114.114.114.114#53 Non-authoritative answer: 114.114.114.114.in-addr.arpa name = public1.114dns.com. Authoritative answers can be found from:
由IP地址反解出FQDN:
[[email protected] ~]# nslookup 114.114.114.114 Server: 114.114.114.114 Address: 114.114.114.114#53 Non-authoritative answer: 114.114.114.114.in-addr.arpa name = public1.114dns.com. Authoritative answers can be found from:
③host工具:
用法示例:
由FQDN解析出IP地址:
[[email protected] ~]# host public1.114dns.com. public1.114dns.com has address 114.114.114.114
由IP地址反解出FQDN:
[[email protected] ~]# host 114.114.114.114 114.114.114.114.in-addr.arpa domain name pointer public1.114dns.com.
(2)本地主机名的配置:
①hostname命令;
②hostnamectl命令(CentOS 7专用);
③配置文件:/etc/sysconfig/network;
①hostname命令
用法示例:
查看当前主机名:
[[email protected] ~]# hostname localhost.localdomain
将主机名改为www.iTab.com:
[[email protected] ~]# hostname www.iTab.com
②hostnamectl命令(CentOS 7专用)
用法示例:
显示当前主机名信息:
[[email protected] ~]# hostnamectl Static hostname: localhost Transient hostname: localhost.localdomain Icon name: computer-vm Chassis: vm Machine ID: dca382f93a984870b93aab37ceaf66dc Boot ID: 033b1679890b4e6e9c1bafe6528e9c8e Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-514.el7.x86_64 Architecture: x86-64
将主机名改为www.iTab.com:
[[email protected] ~]# hostnamectl set-hostname www.iTab.com [[email protected] ~]# hostnamectl #注意:该命令会修改配置文件,不会立即生效,但重启后会永久生效; Static hostname: www.itab.com Pretty hostname: www.iTab.com Icon name: computer-vm Chassis: vm Machine ID: dca382f93a984870b93aab37ceaf66dc Boot ID: 033b1679890b4e6e9c1bafe6528e9c8e Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-514.el7.x86_64 Architecture: x86-64
③配置文件:/etc/sysconfig/network
用法示例:
[[email protected] ~]# vim /etc/sysconfig/network HOSTNAME=www.iTab.com #注意:此方法不会立即有效,但重启会一直有效;
网络属性配置文件:
①修改接口属性(IP/NETMASK/GW/DNS)的配置文件:/etc/sysconfig/network-scripts/ifcfg-IFACE;
②路由的配置文件:/etc/sysconfig/network-scripts/route-IFACE;
①修改接口属性(IP/NETMASK/GW/DNS)的配置文件:/etc/sysconfig/network-scripts/ifcfg-IFACE:
该配置文件通过大量参数来定义接口的属性,可通过文本编辑器vim来编辑,也可通过命令行修改此配置文件(在CentOS 6上为system-config-network(setup),在CentOS 7上还支持nmtui)。
配置文件参数: DEVICE:此配置文件对应的设备的名称; ONBOOT:在系统引导过程中,是否激活此接口; BOOTPROTO:激活此接口时通过什么协议来配置此接口的属性;常用的有:dhcp, bootp, static, none; PEERDNS:如果BOOTPROTO的值为"dhcp",则是否允许dhcp server分配的DNS服务器指向覆盖本地手动指定的DNS服务器指向;默认为允许(yes); UUID:此设备的唯一标识; IPV6INIT:是否初始化IPv6; TYPE:接口类型,常见的有:Ethernet, Bridge; DNS1:第一DNS服务器指向; DNS2:备用DNS服务器指向; DOMAIN:指定搜索域; IPADDR:IP地址; NETMASK:子网掩码; GATEWAY:默认网关;在CentOS 7上还支持使用PREFIX以长度方式指明子网掩码; USERCTL:是否允许普通用户控制此设备; HWADDR:此设备的MAC地址; NM_CONTROLLED:是否使用NetworkManager服务来控制此接口;
修改好配置文件后,需要重启网络服务通过一些脚本读取该配置文件才能生效:
CentOS 6:
方式一:# /etc/init.d/network restart 方式二:# service network restart
CentOS 7:
方式一(推荐):# systemctl restart network.service 方式二:# service network restart
相关的网络服务有:
network NetworkManager
②路由的配置文件:/etc/sysconfig/network-scripts/route-IFACE:
当需要用到非默认网关路由时,可通过此配置文件来配置路由条目;
支持两种配置方式:
(1)每行一个路由条目:
TARGET via GW
(2)每三行一个路由条目:
ADDRESS#=TARGET NETMASK#=MASK GATEWAY#=NEXTHOP #其中‘#‘为数字,表示第几组路由条目;
注意:两种配置方式不能混用;
用法示例:
[[email protected] ~]# route -n #配置前先查看路由表; Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.10.10.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 0.0.0.0 10.10.10.2 0.0.0.0 UG 100 0 0 ens33 [[email protected] ~]# vim /etc/sysconfig/network-scripts/route-ens33 #手动编辑路由配置文件; 100.0.0.0/8 via 10.10.10.2 #采用每一行一个路由条目方式; [[email protected] ~]# systemctl restart network #重启网络服务network; [[email protected] ~]# route -n #查看路由表; Kernel IP routing table Destination Gateway Genmask Flags Metric Ref Use Iface 10.10.10.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 100.0.0.0 10.10.10.2 255.0.0.0 UG 100 0 0 ens33 #配置成功; 0.0.0.0 10.10.10.2 0.0.0.0 UG 100 0 0 ens33
文本图形化编辑:
[[email protected] ~]# nmtui
如果配置完成可点击"OK",如果放弃修改可直接退出"quit"。
除了以上介绍的静态指定方式之外,还可通过动态分配方式配置网络属性,一般使用dhcp(Dynamic Host Configure Protocol)协议。
采用动态分配方式只需在接口属性配置文件中指明即可,如下:
[[email protected] ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 BOOTPROTO=dhcp