ifconfig
ifconfig是传统的网络管理命令,用于查看和配置网络接口,常规使用方法如下:
ifconfig [-a] :用于查看当前所有网络接口的状态,-a可省略
[[email protected] ~]# ifconfigeno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 #网络接口名、状态、最大传输单元 inet 192.168.254.148 netmask 255.255.255.0 broadcast 192.168.254.255 #IP地址、子网掩码、组播地址 inet6 fe80::20c:29ff:fe1f:3407 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:1f:34:07 txqueuelen 1000 (Ethernet) #MAC地址、网络类型 RX packets 9784 bytes 6661801 (6.3 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3204 bytes 334670 (326.8 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 8 bytes 704 (704.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 8 bytes 704 (704.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 virbr0: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255 ether 52:54:00:b9:d3:ee 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
ipconfig INTERFACE:用于查看具体某个网路接口的信息
[[email protected] ~]# ifconfig eno16777736eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.254.148 netmask 255.255.255.0 broadcast 192.168.254.255 inet6 fe80::20c:29ff:fe1f:3407 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:1f:34:07 txqueuelen 1000 (Ethernet) RX packets 10412 bytes 6710915 (6.4 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3386 bytes 363486 (354.9 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ifconfig INTERFACE [up|down]:启用和关闭某网络接口
使用ifconfig配置IP地址的两种方法
ifconfig INTERFACE ip/mask [up],例如:ifconfig eno16777736 192.168.100.100/24
ifconfig INTERFACE ip mask 例如ifconfig eno16777736 192.168.100.100 255.255.255.0
以上两种方法配置IP地址可立即生效,但是重启network服务或重启电脑后就会失效。
可以通过 ifconfig [-]promisc 来开启或关闭混杂模式,混杂模式主要用于网络抓包
ifconfig promisc :开启混杂模式
ifconfig -promisc: 关闭混杂模式
route
route命令主要用于路由查看、创建、修改、删除等管理操作,具体用法如下:
route -n :查看路由状态
[[email protected] ~]# route -nKernel IP routing tableDestination(目标) Gateway(网关) Genmask(掩码) Flags(状态,U代表启用,G代表从网关连接,H代表仅连接到某主机) Metric(路由开销) Ref Use Iface(网络接口名称)0.0.0.0 192.168.254.2 0.0.0.0 UG 100 0 0 eno16777736192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0192.168.254.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736
route add :增加路由条目。
格式:route add [-net|-host] target [netmask Nm] [gw Gw] [[dev] If] -net为网络路由,-host为主机路由
示例1:增加一条主机路由,目标:10.10.10.1 子网掩码:255.255.255.0 网关:192.168.254.2
[[email protected] ~]# route add -host 10.10.10.1 gw 192.168.254.2 dev eno16777736[[email protected] ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.254.2 0.0.0.0 UG 100 0 0 eno1677773610.10.10.21 192.168.254.2 255.255.255.255 UGH 0 0 0 eno16777736192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0192.168.254.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736
示例2:增加一条网络路由,目标:10.10.10.0 子网掩码:255.255.255.0 网关:192.168.254.2
[[email protected] ~]# route add -net 10.10.10.0/24 gw 192.168.254.2 dev eno16777736[[email protected] ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.254.2 0.0.0.0 UG 100 0 0 eno1677773610.10.10.0 192.168.254.2 255.255.255.0 UG 0 0 0 eno1677773610.10.10.21 192.168.254.2 255.255.255.255 UGH 0 0 0 eno16777736192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0192.168.254.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736
route del:删除路由条目
格式:route del [-net|-host] target [gw Gw] [netmask Nm] [[dev] If]
示例1:删除一条主机路由,目标:10.10.10.1 子网掩码:255.255.255.0 网关:192.168.254.2
[[email protected] ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.254.2 0.0.0.0 UG 100 0 0 eno1677773610.10.10.0 192.168.254.2 255.255.255.0 UG 0 0 0 eno1677773610.10.10.21 192.168.254.2 255.255.255.255 UGH 0 0 0 eno16777736192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0192.168.254.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736[[email protected] ~]# route del 10.10.10.21[[email protected] ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.254.2 0.0.0.0 UG 100 0 0 eno1677773610.10.10.0 192.168.254.2 255.255.255.0 UG 0 0 0 eno16777736192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0192.168.254.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736
示例2:删除一条网络路由,目标:10.10.10.0 子网掩码:255.255.255.0 网关:192.168.254.2
[[email protected] ~]# route del -net 10.10.10.0/24[[email protected] ~]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface0.0.0.0 192.168.254.2 0.0.0.0 UG 100 0 0 eno16777736192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0192.168.254.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736
Tip:删除主机路由条目时可 [-host] 和[netmask]可省略,进指定删除的目标IP即可,删除网络路由时[-net]和[netmask]均不能省略。
DNS设置与解析
一般本地会从/etc/resolv.conf文件中读取DNS信息,文件内容如下:
# Generated by NetworkManager search localdomain nameserver 192.168.21.2 #格式为nameserver DNS地址,共计可以设置三个DNS地址。
DNS正解,FQDN-->IP
dig -t A FQDN :将FQDN解析为IP地址
[[email protected] ~]# dig -t A n-test.testAD.com ; <<>> DiG 9.9.4-RedHat-9.9.4-38.el7_3.2 <<>> -t A n-wangyanglin.zjipst.com ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 43502 ;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; MBZ: 0005 , udp: 4000 ;; QUESTION SECTION: ;n-test.testAD.com. IN A ;; ANSWER SECTION: n-test.testAD.com. 5 IN A 192.168.252.235 ;; Query time: 26 msec ;; SERVER: 192.168.21.2#53(192.168.21.2) ;; WHEN: Sat May 06 16:04:21 CST 2017 ;; MSG SIZE rcvd: 69
host -t A FQDN(计算机名):将FQDN(计算机名)解析为IP地址
[[email protected] ~]# host -t A n-test.testAD.com n-test.testAD.com has address 192.168.252.235
DNS反解,IP-FQDN
dig -x ip-address:将ip地址解析为FQDN名
[[email protected] ~]# dig -x 115.239.210.27 ; <<>> DiG 9.9.4-RedHat-9.9.4-38.el7_3.2 <<>> -x 115.239.210.27 ;; global options: +cmd ;; Got answer: ;; ->>HEADER<<- opcode: QUERY, status: NXDOMAIN, id: 59372 ;; flags: qr rd ra; QUERY: 1, ANSWER: 0, AUTHORITY: 1, ADDITIONAL: 1 ;; OPT PSEUDOSECTION: ; EDNS: version: 0, flags:; MBZ: 0005 , udp: 4000 ;; QUESTION SECTION: ;27.210.239.115.in-addr.arpa. IN PTR ;; AUTHORITY SECTION: 239.115.in-addr.arpa. 5 IN SOA ns.zjnbptt.net.cn. root. 2017021401 3600 1800 604800 3600 ;; Query time: 29 msec ;; SERVER: 192.168.21.2#53(192.168.21.2) ;; WHEN: Sat May 06 16:10:21 CST 2017 ;; MSG SIZE rcvd: 113
host -t PTR IP地址:将IP地址解析为FQDN名
[[email protected] ~]# host -t PTR 192.168.254.2 2.192.168.254.in-addr.arpa domain name pointer AD03.test.com.
netstat
netstat命令主要用于显示网络连接、路由表、网络接口状态等相关参数
显示网络连接
格式: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:显示TCP协议相关连接的所有状态信息,IP地址和端口号以数字显示
-tnl:显示TCP协议相关处于坚挺状态的所有连接信息,IP地址和端口已数字显示
-uan:显示UDP协议相关连接的所有状态信息,IP地址和端口号以数字显示
-unl:显示UDP协议相关处于坚挺状态的所有连接信息,IP地址和端口已数字显示
显示路由表
格式:netstat {--route|-r} [--numeric|-n]
参数:
-r: 显示内核路由表
-n: 数字格式
显示接口统计数据
格式:netstat {--interfaces|-I|-i} [iface] [--all|-a] [--extend|-e] [--program|-p] [--numeric|-n]
IP
用户查看和配置网络接口、路由、设备、策略路由及隧道
格式:ip [ OPTIONS ] OBJECT { COMMAND | help }
OBJECT := { link | addr | route }
IP link:用于网络设备链路配置
子命令
set DEV {up|down}:将某个网络接口关闭或开启
[[email protected] ~]# ip link set eno16777736 down;ip link set eno16777736 up
show DEV :查看指定设备,若不加DEV,默认显示所有网络接口
show up :查看出于开启状态的网络设备
IP addr:用户管理网络地址和相关协议
格式:ip addr { add | del } IFADDR dev STRING