文章目录
一 、查看网络接口信息 ifconfig
1、查看所有活动网接口的信息
执行ifconfig 命令
[root@zhen ~]# ifconfig
'虚拟机网卡'
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.110.132 netmask 255.255.255.0 broadcast 192.168.110.255
inet6 fe80::6234:4acd:67c9:841a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:b0:99:30 txqueuelen 1000 (Ethernet)
RX packets 17489 bytes 1681612 (1.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10990 bytes 6769011 (6.4 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
'环回网卡 一般用于测试tcp/ip 协议是否正常服务'
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 1000 (Local Loopback)
RX packets 32 bytes 2592 (2.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 32 bytes 2592 (2.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
' linux系统中虚拟的桥接网卡 /'
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:5f:4b:b2 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
2 、查看指定网络接口信息
[root@localhost ~]# ifconfig ens33
'///ens33第一块以太网卡的名称': flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 150
'inet IP地址 192.168.139.153' 'netmask 子网掩码 255.255.255.0' 'broadcast 广播地址 192.168.139.255'
inet6 fe80::413b:c9ad:e0e:1afc prefixlen 64 scopeid 0x20<link>
ether '00:0c:29:d6:c0:8a MAC地址' txqueuelen 1000 (Ethernet)
RX packets 28803 bytes 2100162 (2.0 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 21495 bytes 3147424 (3.0 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
二 、 查看主机名称 hostname
1 、hostname命令
'//查看或设置当前主机名'
[root@localhost ~]# hostname
2、更改主机名
永久更改主机名字
[root@localhost ~]# hostnamectl set-hostname zhen
[root@localhost ~]# su '刷新一下'
[root@zhen ~]#
三 、查看路由条目 route
1 、route 命令
查看或设置主机中的路由表信息
-n 以数字形式去显示
[root@zhen ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
0.0.0.0 192.168.110.2 0.0.0.0 UG 100 0 0 ens33
192.168.110.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 与route -n 的区别在于默认网络改为数字0.0.0.0
[root@zhen ~]# 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.110.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
Destination 网段
Gateway 网关
Genmask 子网掩码
metric 度量值
四 、 查看网络连接情况 netstat
1 、netstat 命令
查看系统的网络连接状态、路由表、接口统计等信息
netstat 【选项】
选项 | 解释 |
---|---|
-a | 显示所有 |
-n | 显示序列号 |
-p | 显示端口号 |
-t | 显示TCP协议 |
-u | 显示UDP协议 |
-r | 显示route |
[root@zhen ~]# netstat -natp
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 7183/X
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 7411/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 6904/sshd
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 6902/cupsd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 7278/master
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 11248/sshd: root@pt
tcp 0 36 192.168.110.132:22 192.168.110.1:49346 ESTABLISHED 11248/sshd: root@pt
tcp6 0 0 :::111 :::* LISTEN 1/systemd
tcp6 0 0 :::6000 :::* LISTEN 7183/X
tcp6 0 0 :::22 :::* LISTEN 6904/sshd
tcp6 0 0 ::1:631 :::* LISTEN 6902/cupsd
tcp6 0 0 ::1:25 :::* LISTEN 7278/master
tcp6 0 0 ::1:6010 :::* LISTEN 11248/sshd: root@pt
sshd 指远程访问
可以结合检索命令去针对性查看
[root@965 ~]# netstat -natp | grep "22"
tcp 0 0 192.168.122.1:53 0.0.0.0:* LISTEN 1296/dnsmasq
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1116/sshd
tcp 0 36 192.168.139.153:22 192.168.139.1:64738 ESTABLISHED 13953/sshd: root@pt
tcp6 0 0 :::22 :::* LISTEN 1116/sshd
[root@965 ~]# netstat -natp | grep ssh
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1116/sshd
tcp 0 0 127.0.0.1:6010 0.0.0.0:* LISTEN 13953/sshd: root@pt
tcp 0 36 192.168.139.153:22 192.168.139.1:64738 ESTABLISHED 13953/sshd: root@pt
tcp6 0 0 :::22 :::* LISTEN 1116/sshd
tcp6 0 0 ::1:6010 :::* LISTEN 13953/sshd: root@pt
五 、 跟踪数据包 traceroute(中文译路由跟踪)
1 traceroute 命令
测试从当前主机到目的主机之间经过的网络节点
traceroute +目标主机地址
[root@zhen ~]# traceroute 192.168.1.223
traceroute to 192.168.1.223 (192.168.1.223), 30 hops max, 60 byte packets
1 gateway (192.168.110.2) 0.595 ms 0.406 ms 0.355 ms
2 * * *
3 * * *
数据先到网关,***号说明被nat屏蔽了
六 : 域名解析 nslookup
1 、nslookup 命令
测试DNS命令
nslookup 目标主机地址 [DNS服务器地址]
也可以使用dig www.baidu.com 来进行域名解析
[root@zhen ~]# nslookup www.baidu.com
Server: 8.8.8.8 'dns服务器地址'
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: 103.235.46.39 ' 百度的IP地址'
七 设置网络参数的方式
1、 临时配置—— 使用命令调整网络参数
简单、快速,可直接修改运行中的网络参数
一般只适合在调试网络的过程中使用
系统重启以后,所做的修改将会失效
2 、固定设置——— 通过配置文件修改网络参数
修改各项参数的配置文件
适合对服务器设置固定参数时使用
需要重载网络服务或者重启以后才会生效
3、 设置网络接口参数命令 ifconfig
设置网络接口的IP地址、子网掩码
ifconfig 接口名(网卡名) ip地址 [netmask 子网掩码]
ifconfig 网络接口 ip地址[/掩码长度]
禁用或重新激活网卡
ifconfig 网络接口 up
ifconfig 网络接口 down
设置虚拟网络接口
ifconfig 接口名:序号 IP地址
接口名 ens33
[root@zhen ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.110.132 netmask 255.255.255.0 broadcast 192.168.110.255
inet6 fe80::6234:4acd:67c9:841a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:b0:99:30 txqueuelen 1000 (Ethernet)
RX packets 17831 bytes 1709938 (1.6 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11338 bytes 6803765 (6.4 MiB)
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 1000 (Local Loopback)
RX packets 32 bytes 2592 (2.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 32 bytes 2592 (2.5 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
临时修改,只用与维护时使用,
序号,0-123456,相当于逻辑接口,
当一台主机需要用多台IP地址时,可以使用逻辑接口配置
配置如下:
[root@zhen ~]# ifconfig ens33:1 192.168.110.132 '///配置虚拟接口1的IP地址'
[root@zhen ~]# ifconfig ' 查看后发现'
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 1.1.1.1 netmask 255.255.255.0 broadcast 1.1.1.255
inet6 fe80::6234:4acd:67c9:841a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:b0:99:30 txqueuelen 1000 (Ethernet)
RX packets 18070 bytes 1981290 (1.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11535 bytes 6823432 (6.5 MiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.110.132 netmask 255.255.255.0 broadcast 192.168.110.255
ether 00:0c:29:b0:99:30 txqueuelen 1000 (Ethernet)
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 1000 (Local Loopback)
RX packets 32 bytes 2592 (2.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 32 bytes 2592 (2.5 KiB)
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:5f:4b:b2 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
逻辑子接口(例:ens33:1)的down相当于把IP地址释放掉了
这里开启可以用ifdown ens33
关闭可以用 ifup ens33
[root@zhen ~]# ifconfig ens33 down '将网卡接口关掉'
[root@zhen ~]# ifconfig
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 1000 (Local Loopback)
RX packets 32 bytes 2592 (2.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 32 bytes 2592 (2.5 KiB)
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:5f:4b:b2 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
[root@zhen ~]# ifconfig ens33 up '将接口打开'
[root@zhen ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.110.132 netmask 255.255.255.0 broadcast 192.168.110.255
inet6 fe80::6234:4acd:67c9:841a prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:b0:99:30 txqueuelen 1000 (Ethernet)
RX packets 18086 bytes 1986559 (1.8 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 11576 bytes 6829573 (6.5 MiB)
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 1000 (Local Loopback)
RX packets 32 bytes 2592 (2.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 32 bytes 2592 (2.5 KiB)
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:5f:4b:b2 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
八 、设置路由记录 route
1、 添加到指定网段的路由记录
route add -net 网段地址 gw IP地址
[root@zhen ~]# route add -net 192.168.10.0/24 gw 192.168.110.2
[root@zhen ~]# 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.10.0 gateway 255.255.255.0 UG 0 0 0 ens33
192.168.110.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
2、 删除到网段的路由记录
route del -net 网段地址 gw IP地址
[root@zhen ~]# route del -net 192.168.10.0/24 gw 192.168.110.2
[root@zhen ~]# rout
bash: rout: 未找到命令...
[root@zhen ~]# 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.110.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
3、 向路由表中添加默认网关记录
route del defalut gw IP地址
[root@zhen ~]# route add default gw 192.168.110.2
[root@zhen ~]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
default gateway 0.0.0.0 UG 0 0 0 ens33
default gateway 0.0.0.0 UG 100 0 0 ens33
192.168.110.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
4、 删除路由表中的默认网关记录
route add default gw IP地址
root@zhen ~]# route del default gw 192.168.110.2
[root@zhen ~]# 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.110.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
gw 下一跳地址
-net 目标网段
九、 配置文件
1、 /etc/sysconfig/network 主机名称配置文件
保存全局网络配置,主要包括主机信息
vim /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=zhen
:wq
reboot '重启生效'
2 、/etc/resolv.conf 域名解析配置文件
保存本机需要使用DNS服务器的IP地址
[root@dabendan ~]# vim /etc/resolv.conf
#Generated by NetworkManager
search localdomain'主机域'
nameserver 192.168.139.2 '默认网关,也可以手动配置'
3 、/etc/hosts 本地主机映射文件
保存主机名与IP地址的映射文件
[root@dabendan ~]# vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
119.75.218.70 www.baidu.com
hosts文件和DNS服务器的比较
默认情况下,系统首先从hosts文件查找解析记录
hosts文件只对当前的主机有效
hosts文件可减少DNS查询过程,从而加快访问速度
域名解析会优先去查看/etc/hosts 映射文件,若是没有,就会再去找dns服务器