查看所有活动的网络接口信息
执行ifconfig命令
查看指定网络接口信息
ifconfig网络接口
[root@localhost ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.100.120 netmask 255.255.255.0 broadcast 192.168.100.255
inet6 fe80::649:df95:3c83:ded1 prefixlen 64 scopeid 0x20
ether 00:0c:29:33:30:14 txqueuelen 1000 (Ethernet)
RX packets 601231 bytes 882275039 (841.4 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 136498 bytes 11581281 (11.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73 mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10
loop txqueuelen 1000 (Local Loopback)
RX packets 90 bytes 7770 (7.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 90 bytes 7770 (7.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
virbr0: flags=4099 mtu 1500
inet 192.168.122.1 netmask 255.255.255.0 broadcast 192.168.122.255
ether 52:54:00:20:12:a8 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
hostname命令
查看或设置当前主机名
hostname [主机名]
大部分临时修改都是用命令修改
永久修改直接改配置文件
[root@localhost ~]# hostname #查看主机名
[root@localhost ~]# hostname pan #临时修改成pan
[root@localhost ~]# hostnamectl set-hostname pan #永久修改pan
route命令
查看或设置主机中路由表信息
route [-n]
[root@localhost ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway 0.0.0.0 UG 100 0 0 ens33
192.168.100.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.100.2 0.0.0.0 UG 100 0 0 ens33 #默认网段
192.168.100.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
netstat命令
查看系统的网络连接状态、路由表、接口统计等信息
常用选项
-a 所有 包含已连接和监听未连接
-n 已数字形式进行显示
-p 显示PID进程号
-t 查看TCP协议连接
-u 查看UDP协议连接
-r 查看路由表
[root@localhost ~]# netstat -ntap
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd
tcp 0 0 0.0.0.0:6000 0.0.0.0:* LISTEN 9921/X
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 9683/dnsmasq
tcp 0 52 192.168.100.120:22 192.168.100.1:56234 ESTABLISHED 12589/sshd: root@pt
LISTEN 未连接 监听
ESTABLISHED 已连接
ping命令
测试网络连通性
ping 目标主机IP
无法访问目标=到达不了对方找不到路
请求超时=数据包能到 某种原因回不来
Ctrl+c终止
traceroute
测试从当前主机到目标主机之间经过的网络节点
traceroute 目标地址IP
[root@localhost ~ ]# traceroute 192.168.7.7
traceroute to 192.1 68.7.7 (192.168.7.7), 30 hops max, 40 byte packets
1 (192.168.4.1) 7.740 ms 15.581 ms 15.881 ms
2 (192.168.7.7) 19.652 ms 19.995 ms 19.942 ms
后面可以跟ip地址或者域名
nslookup命令
测试DNS域名解析
nslookup 目标主机地址 [DNS服务器地址]
[root@localhost ~]# nslookup www.baidu.com
Server: 8.8.8.8
Address: 8.8.8.8#53
Non-authoritative answer:
www.baidu.com(别名) canonical name = www.a.shifen.com.(真正的名字)
www.a.shifen.com canonical name = www.wshifen.com.
Name: www.wshifen.com
Address: 104.193.88.123 #百度的服务器地址浏览器直接能ping通
Name: www.wshifen.com
Address: 104.193.88.77
可以根据域名查出对方IP地址
TCP 端口53用于连接
UDP 端口53用于解析
dig查询域名DNS信息
[root@localhost ~]# dig www.baidu.com
; <<>> DiG 9.9.4-RedHat-9.9.4-72.el7 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 54824
;; flags: qr rd ra; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;www.baidu.com. IN A
;; ANSWER SECTION:
www.baidu.com. 1174 IN CNAME www.a.shifen.com.
www.a.shifen.com. 17 IN CNAME www.wshifen.com.
www.wshifen.com. 124 IN A 104.193.88.123
www.wshifen.com. 124 IN A 104.193.88.77
;; Query time: 53 msec
;; SERVER: 8.8.8.8#53(8.8.8.8)
;; WHEN: 四 7月 02 18:31:39 CST 2020
;; MSG SIZE rcvd: 127
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static 模式(静态)
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33 名称
UUID=96401a89-2a9e-4214-b827-df367567dc24 网卡信息
DEVICE=ens33
ONBOOT=yes
IPADDR=192.168.100.120 手动输入IP地址
NETMAKS=255.255.255.0 子网掩码
GATEWAY=192.168.100.2 网段
域名解析服务
方法一
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
最后加上
DNS1=8.8.8.8
方法二域名解析服务文件写入DNS 或者网段ip
修改/etc/resolv.conf
[root@localhost ~]# echo "nameserver 8.8.8.8" > /etc/resolv.conf
nameserver 域名服务器IP
改完后systemctl restart network 重启网络服务
设置网络接口的IP地址、子网掩码
ifconfig 网络接口 ip地址 [netmask子网掩码]
ifconfig 网络接口 ip地址 [/子网掩码长度]
[root@localhost ~]# ifconfig ens33:192. 168. 100. 120 netmask 255. 255. 255. 0
禁用或者重新激活网卡
ifconfig 网络接口 up
ifconfig 网络接口 down
[root@localhost ~]# ifconfig ens33 down
[root@localhost ~]# ifconfig ens33 up
设置虚拟网络接口
ifconfig 网络接口:序号 IP地址
逻辑子接口 (跟单臂路由相似)一般做调试的时候用
[root@pan ~]# ifconfig ens33:0 192.168.100.33 netmask 255.255.255.0
[root@pan ~]# ifconfig
ens33: flags=4163 mtu 1500
inet 192.168.100.120 netmask 255.255.255.0 broadcast 192.168.100.255
inet6 fe80::649:df95:3c83:ded1 prefixlen 64 scopeid 0x20
ether 00:0c:29:33:30:14 txqueuelen 1000 (Ethernet)
RX packets 603214 bytes 882448177 (841.5 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 137809 bytes 11731729 (11.1 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:0: flags=4163 mtu 1500 多了ens33:0
inet 192.168.100.33 netmask 255.255.255.0 broadcast 192.168.100.255
ether 00:0c:29:33:30:14 txqueuelen 1000 (Ethernet)
添加到指定网段的路由记录
route add -net 192.168.111.0/24 gw 192.168.100.2
类似配置路由器的静态IP地址
删除到指定网段的路由记录
route del - net 192.168.111.0/24
[root@pan ~]# route add -net 192.168.111.0/24 gw 192.168.100.2
[root@pan ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.100.2 0.0.0.0 UG 100 0 0 ens33
192.168.100.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.111.0 192.168.100.2 255.255.255.0 UG 0 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@pan ~]# route del -net 192.168.111.0/24
[root@pan ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.100.2 0.0.0.0 UG 100 0 0 ens33
192.168.100.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
向路由表中添加默认网关记录
route add default -net gw IP地址
[root@pan ~]# route del -net 0.0.0.0
[root@pan ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.100.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
[root@pan ~]# route add default gw 192.168.100.2 添加后要重启网络服务
[root@pan ~]# systemctl restart network
[root@pan ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.100.2 0.0.0.0 UG 100 0 0 ens33
192.168.100.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33
192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr0
禁用、启用网络接口
[root@localhost ~ ]# ifdown ens33
[root@localhost ~]# ifup ens33
禁用后逻辑子接口会没有
设置dhcp获取重启后会ip地址会加1 以下方式能解决 停止 NetworkManager 禁用NetworkManager最好方法还是改成静态
systemctl stop NetworkManager
systemctl disable NetworkManager