linux网络系统管理_linux基础知识点

(31) 2024-09-02 07:01:01

文章目录

    • 文档配套视频讲解链接地址
    • 第11章 网络管理
      • **11.1 查看网络设置**
        • 1. **ifconfig 显示与设置网络命令**
        • 2. **route 显示与设置路由命令**
        • 3. **hostname命令**
        • 3. **netstat命令**
      • **11.2 测试网络连接**
        • 1. **ping 命令**
        • 2. **traceroute命令**
      • **11.3 修改网络配置**
        • 1. **图形化配置**(network-manager)
        • 2. **命令行配置**(不建议使用,建议使用图形化)
        • 3. 网络图标消失的解决办法

文档配套视频讲解链接地址

  1. 腾讯课堂视频链接地址 : 44_网络管理_查看网络设置1
  2. 腾讯课堂视频链接地址 : 45_网络管理_测试网络连接2
  3. 腾讯课堂视频链接地址 : 46_网络管理_修改网络配置3
  4. 腾讯课堂视频链接地址 : 47_网络管理_修改网络配置4

第11章 网络管理

11.1 查看网络设置

1. ifconfig 显示与设置网络命令

​ ifconfig命令来自于英文词组”network interfaces configuring“的缩写,其功能是用于显示或设置网络设备参数信息。在Windows系统中与之类似的命令叫做ipconfig,同样的功能可以使用ifconfig去完成。

通常不建议使用ifconfig命令配置网络设备的参数信息,因为一旦服务器重启,配置过的参数会自动失效,还是编写到配置文件中更稳妥。

**语法格式:**ifconfig [参数] [网卡设备]

常用参数:

add<地址> 设置网络设备IPv6的IP地址
del<地址> 删除网络设备IPv6的IP地址
down 关闭指定的网络设备
up 启动指定的网络设备
首先说明下eth0与ens33的关系: 目前的主流网卡为使用以太网络协定所开发出来的以太网卡 (Ethernet),因此我们 Linux 就称呼这种网络接口为 ethN (N 为数字)。 举例来说,主机上面有一张以太网卡,因此主机的网络接口就是 eth0 (第一张为 0 号开始)。新的 CentOS 7 开始对于网卡的编号有另一套规则,网卡的界面代号与网卡的来源有关,网卡名称会是这样分类的: eno1 :代表由主板 BIOS 内置的网卡 ens1 :代表由主板 BIOS 内置的 PCI-E 界面的网卡 enp2s0 :代表 PCI-E 界面的独立网卡,可能有多个插孔,因此会有 s0, s1... 的编号~ eth0 :如果上述的名称都不适用,就回到原本的默认网卡编号 所以会有ens33这种网卡表现形式。 

常用命令

ifconfig # 查看系统的网卡信息  ifconfig ens33 #查看具体某一个网卡的信息  sudo ifconfig ens33 192.168.1.200 netmask 255.255.255.0 # 设置ip地址和子网掩码 , 只设置ip地址 , 重启后消失  sudo ifconfig ens33 192.168.1.199 # 只设置ip地址 , 重启后消失  sudo ifconfig ens33 down # 关闭网卡  sudo ifconfig ens33 up # 启动网卡 , 启动网卡后, 原来ifconfig设置的ip会丢失 

实例

linux@ubuntu:~/work$ ifconfig # ubuntu 默认没有集成ifconfig ,需要额外的安装命令 Command 'ifconfig' not found, but can be installed with: sudo apt install net-tools linux@ubuntu:~/work$ linux@ubuntu:~/work$ sudo apt install net-tools # 安装ifconfig 命令  [sudo] linux 的密码: 正在读取软件包列表... 完成 正在分析软件包的依赖关系树 正在读取状态信息... 完成 下列【新】软件包将被安装: net-tools 升级了 0 个软件包,新安装了 1 个软件包,要卸载 0 个软件包,有 0 个软件包未被升级。 需要下载 194 kB 的归档。 解压缩后会消耗 803 kB 的额外空间。 获取:1 http://mirrors.yun-idc.com/ubuntu bionic/main amd64 net-tools amd64 1.60+git.90da8a0-1ubuntu1 [194 kB] 已下载 194 kB,耗时 0(1,058 kB/s) 正在选中未选择的软件包 net-tools。 (正在读取数据库 ... 系统当前共安装有  个文件和目录。) 正准备解包 .../net-tools_1.60+git.90da8a0-1ubuntu1_amd64.deb ... 正在解包 net-tools (1.60+git.90da8a0-1ubuntu1) ... 正在设置 net-tools (1.60+git.90da8a0-1ubuntu1) ... 正在处理用于 man-db (2.8.3-2ubuntu0.1) 的触发器 ... linux@ubuntu:~/work$ linux@ubuntu:~$ ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.103 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::fb81:9220:45a:8e prefixlen 64 scopeid 0x20<link> inet6 2409:8a02:7826:b7f0:c027:1733:cfe1:1684 prefixlen 64 scopeid 0x0<global> inet6 2409:8a02:7826:b7f0:c575:a926:1076:de45 prefixlen 64 scopeid 0x0<global> ether 00:0c:29:d1:30:6c txqueuelen 1000 (以太网) RX packets 4599 bytes  (870.9 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3423 bytes  (406.7 KB) 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 (本地环回) RX packets 24241 bytes  (1.7 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 24241 bytes  (1.7 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 linux@ubuntu:~$ linux@ubuntu:~/work$ ifconfig ens33 ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.103 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::3901:d959:9bc4:b96 prefixlen 64 scopeid 0x20<link> inet6 2409:8a02:782e:72d0:e147:85ad:1f7a:666d prefixlen 64 scopeid 0x0<global> inet6 2409:8a02:782e:72d0:d89c:64ba:7cd2:a002 prefixlen 64 scopeid 0x0<global> ether 00:0c:29:d1:30:6c txqueuelen 1000 (以太网) RX packets 2639 bytes  (980.8 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1368 bytes  (154.9 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 linux@ubuntu:~/work$ ifconfig ens33 192.168.1.200 netmask 255.255.255.0 # 设置ip地址和子网掩码 SIOCSIFADDR: 不允许的操作 SIOCSIFFLAGS: 不允许的操作 SIOCSIFNETMASK: 不允许的操作 linux@ubuntu:~/work$ sudo ifconfig ens33 192.168.1.200 netmask 255.255.255.0 linux@ubuntu:~/work$ ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.200 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::3901:d959:9bc4:b96 prefixlen 64 scopeid 0x20<link> inet6 2409:8a02:782e:72d0:e147:85ad:1f7a:666d prefixlen 64 scopeid 0x0<global> inet6 2409:8a02:782e:72d0:d89c:64ba:7cd2:a002 prefixlen 64 scopeid 0x0<global> ether 00:0c:29:d1:30:6c txqueuelen 1000 (以太网) RX packets 2653 bytes  (982.1 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1400 bytes  (159.0 KB) 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 (本地环回) RX packets 465 bytes 41122 (41.1 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 465 bytes 41122 (41.1 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 linux@ubuntu:~/work$ sudo ifconfig ens33 192.168.1.199 # 只设置ip地址 , 重启后消失  linux@ubuntu:~/work$ ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.199 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::3901:d959:9bc4:b96 prefixlen 64 scopeid 0x20<link> inet6 2409:8a02:782e:72d0:e147:85ad:1f7a:666d prefixlen 64 scopeid 0x0<global> inet6 2409:8a02:782e:72d0:d89c:64ba:7cd2:a002 prefixlen 64 scopeid 0x0<global> ether 00:0c:29:d1:30:6c txqueuelen 1000 (以太网) RX packets 2664 bytes  (983.1 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1432 bytes  (163.6 KB) 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 (本地环回) RX packets 473 bytes 41770 (41.7 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 473 bytes 41770 (41.7 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 linux@ubuntu:~/work$ sudo ifconfig ens33 down # 关闭网卡  linux@ubuntu:~/work$ 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 (本地环回) RX packets 483 bytes 42690 (42.6 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 483 bytes 42690 (42.6 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 linux@ubuntu:~/work$ linux@ubuntu:~/work$ sudo ifconfig ens33 up # 启动网卡 , 启动网卡后, 原来ifconfig设置的ip会丢失 linux@ubuntu:~/work$ ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.103 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::3901:d959:9bc4:b96 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:d1:30:6c txqueuelen 1000 (以太网) RX packets 2698 bytes  (986.8 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1509 bytes  (171.4 KB) 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 (本地环回) RX packets 511 bytes 44732 (44.7 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 511 bytes 44732 (44.7 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 linux@ubuntu:~/work$ 
2. route 显示与设置路由命令

​ route命令的功能是用于显示与设置路由信息,是Linux系统中常用的静态路由配置工具。要想让两台处在不同子网之间的服务器能够通信,需要有一个跨网段的路由器来连接它们,并用route命令为其设置路由信息,提供软硬件的支撑。

**语法格式:**route [参数]

常用参数:

-A 设置地址类型( 默认IPv4)
-C 打印核心的路由缓存
-v 详细信息模式
-n 直接显示数字形式的ip地址
-e netstat格式显示路由表
-net 到一个网络的路由表
-host 到一个主机的路由表
Add 增加指定的路由记录
Del 删除指定的路由记录
Target 目的网络或目的主机
gw 设置默认网关
mss 设置TCP的最大区块长度(单位MB)
window 指定通过路由表的TCP连接的窗口大小
dev 路由记录所表示的网络接口

常用命令

route # 查看默认的路由信息  route add -net 192.168.10.0 netmask 255.255.255.0 dev ens33 # 添加一个路由连接 route del -net 192.168.10.0 netmask 255.255.255.0 dev ens33 # 删除一个路由连接 

实例

linux@ubuntu:~/work$ route 内核 IP 路由表 目标 网关 子网掩码 标志 跃点 引用 使用 接口 default 192.168.1.1 0.0.0.0 UG 20100 0 0 ens33 link-local 0.0.0.0 255.255.0.0 U 1000 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 linux@ubuntu:~/work$ route add -net 192.168.10.0 netmask 255.255.255.0 dev ens33 # 添加路由信息 SIOCADDRT: 不允许的操作 linux@ubuntu:~/work$ sudo route add -net 192.168.10.0 netmask 255.255.255.0 dev ens33 # [sudo] linux 的密码: linux@ubuntu:~/work$ route 内核 IP 路由表 目标 网关 子网掩码 标志 跃点 引用 使用 接口 default 192.168.1.1 0.0.0.0 UG 100 0 0 ens33 link-local 0.0.0.0 255.255.0.0 U 1000 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.10.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 linux@ubuntu:~/work$ route del -net 192.168.10.0 netmask 255.255.255.0 dev ens33 # 删除路由信息 SIOCDELRT: 不允许的操作 linux@ubuntu:~/work$ sudo route del -net 192.168.10.0 netmask 255.255.255.0 dev ens33 linux@ubuntu:~/work$ route 内核 IP 路由表 目标 网关 子网掩码 标志 跃点 引用 使用 接口 default 192.168.1.1 0.0.0.0 UG 100 0 0 ens33 link-local 0.0.0.0 255.255.0.0 U 1000 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 linux@ubuntu:~/work$ 
3. hostname命令

​ hostname命令的功能是用于显示和设置系统的主机名,Linux系统中的HOSTNAME环境变量对应保存了当前的主机名称,使用hostname命令能够查看和设置此环境变量的值,而要想永久修改主机名称则需要使用hostnamectl命令或直接编辑配置文件/etc/hostname才行。

**语法格式:**hostname [参数]

常用参数:

-a 显示主机别名
-d 显示DNS域名
-f 显示FQDN名称
-i 显示主机的ip地址
-s 显示短主机名称
-y 显示NIS域名

参考实例

linux@ubuntu:~/work$ cat /etc/hostname ubuntu linux@ubuntu:~/work$ hostname ubuntu linux@ubuntu:~/work$ hostname -i 127.0.1.1 linux@ubuntu:~/work$ ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.103 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::3901:d959:9bc4:b96 prefixlen 64 scopeid 0x20<link> inet6 2409:8a02:782e:72d0:e147:85ad:1f7a:666d prefixlen 64 scopeid 0x0<global> inet6 2409:8a02:782e:72d0:d89c:64ba:7cd2:a002 prefixlen 64 scopeid 0x0<global> ether 00:0c:29:d1:30:6c txqueuelen 1000 (以太网) RX packets 2953 bytes  (1.0 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1690 bytes  (191.2 KB) 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 (本地环回) RX packets 593 bytes 51789 (51.7 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 593 bytes 51789 (51.7 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 linux@ubuntu:~/work$ hostname -y hostname: Local domain name not set linux@ubuntu:~/work$ man hostname linux@ubuntu:~/work$ hostname ubuntu18.04 # 临时修改主机名, 重启后消失  hostname: you must be root to change the host name linux@ubuntu:~/work$ sudo hostname ubuntu18.04 linux@ubuntu:~/work$ hostname ubuntu18.04 linux@ubuntu:~/work$ cat /etc/hostname ubuntu linux@ubuntu:~/work$ linux@ubuntu1804:~$ sudo vi /etc/hostname #修改为ubuntu , [sudo] linux 的密码: linux@ubuntu1804:~$ reboot 
3. netstat命令

​ netstat命令来自于英文词组”network statistics“的缩写,其功能是用于显示各种网络相关信息,例如网络连接状态、路由表信息、接口状态、NAT、多播成员等等。

netstat命令不仅应用于Linux系统,而且在Windows XP、Windows 7、Windows 10及Windows 11中均已默认支持,并且可用参数也相同,有经验的运维人员可以直接上手。

**语法格式:**netstat [参数]

常用参数:

-a 显示所有连线中的Socket
-p 显示正在使用Socket的程序识别码和程序名称
-l 仅列出在监听的服务状态
-t 显示TCP传输协议的连线状况
-u 显示UDP传输协议的连线状况
-i 显示网络界面信息表单
-r 显示路由表信息
-n 直接使用IP地址,不通过域名服务器

常用命令

netstat -a # 显示系统网络中的所有连接信息  netstat -nu #显示系统网络状态中的UDP连接信息 netstat -apu #显示系统网络状态中的UDP连接端口号使用信息 netstat -i #显示网卡当前状态信息 netstat -r #显示网络路由表状态信息 netstat -ap | grep sogou #找到某个服务所对应的连接信息 

参考实例

linux@ubuntu:~$ netstat -a 激活Internet连接 (服务器和已建立连接的) Proto Recv-Q Send-Q Local Address Foreign Address State tcp 0 0 localhost:ipp 0.0.0.0:* LISTEN tcp 0 0 localhost:domain 0.0.0.0:* LISTEN tcp6 0 0 ip6-localhost:ipp [::]:* LISTEN udp 0 0 0.0.0.0:ipp 0.0.0.0:* udp 0 0 0.0.0.0:43772 0.0.0.0:* udp 0 0 localhost:domain 0.0.0.0:* udp 0 0 0.0.0.0:bootpc 0.0.0.0:* udp 0 0 0.0.0.0:mdns 0.0.0.0:* udp6 0 0 [::]:56761 [::]:* udp6 0 0 [::]:mdns [::]:* raw6 0 0 [::]:ipv6-icmp [::]:* 7 活跃的UNIX域套接字 (服务器和已建立连接的) Proto RefCnt Flags Type State I-Node 路径 unix 2 [ ACC ] 流 LISTENING 43699 /tmp/fcitx-socket-:0 unix 2 [ ACC ] 流 LISTENING 49634 @/tmp/.ICE-unix/1197 linux@ubuntu:~$ netstat -nu 激活Internet连接 (w/o 服务器) Proto Recv-Q Send-Q Local Address Foreign Address State linux@ubuntu:~$ linux@ubuntu:~$ sudo netstat -apu [sudo] linux 的密码: 激活Internet连接 (服务器和已建立连接的) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name udp 0 0 0.0.0.0:ipp 0.0.0.0:* 759/cups-browsed udp 0 0 0.0.0.0:43772 0.0.0.0:* 665/avahi-daemon: r udp 0 0 localhost:domain 0.0.0.0:* 560/systemd-resolve udp 0 0 0.0.0.0:bootpc 0.0.0.0:* 818/dhclient udp 0 0 0.0.0.0:mdns 0.0.0.0:* 665/avahi-daemon: r udp6 0 0 [::]:56761 [::]:* 665/avahi-daemon: r udp6 0 0 [::]:mdns [::]:* 665/avahi-daemon: r linux@ubuntu:~$ linux@ubuntu:~$ sudo 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 547 0 0 0 411 0 0 0 BMRU lo 65536 201 0 0 0 201 0 0 0 LRU linux@ubuntu:~$ linux@ubuntu:~$ sudo netstat -r 内核 IP 路由表 Destination Gateway Genmask Flags MSS Window irtt Iface default 192.168.1.1 0.0.0.0 UG 0 0 0 ens33 link-local 0.0.0.0 255.255.0.0 U 0 0 0 ens33 192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0 ens33 linux@ubuntu:~$ linux@ubuntu:~$ sudo netstat -ap |grep sogou unix 3 [ ] 流 已连接 55226 1800/sogoupinyin-wa unix 3 [ ] 流 已连接 56677 1800/sogoupinyin-wa unix 3 [ ] 流 已连接 53587 1806/sogoupinyin-se unix 3 [ ] 流 已连接 53573 1806/sogoupinyin-se unix 3 [ ] 流 已连接 53574 1806/sogoupinyin-se unix 3 [ ] 流 已连接 53579 1806/sogoupinyin-se unix 3 [ ] 流 已连接 55227 1800/sogoupinyin-wa unix 3 [ ] 流 已连接 56676 1800/sogoupinyin-wa unix 3 [ ] 流 已连接 55220 1806/sogoupinyin-se unix 3 [ ] 流 已连接 53582 1806/sogoupinyin-se unix 3 [ ] 流 已连接 56690 1806/sogoupinyin-se unix 3 [ ] 流 已连接 53577 1806/sogoupinyin-se unix 3 [ ] 流 已连接 55447 1800/sogoupinyin-wa unix 3 [ ] 流 已连接 55441 1800/sogoupinyin-wa unix 3 [ ] 流 已连接 55453 1806/sogoupinyin-se unix 3 [ ] 流 已连接 55451 1800/sogoupinyin-wa unix 3 [ ] 流 已连接 55444 1800/sogoupinyin-wa unix 3 [ ] 流 已连接 55454 1806/sogoupinyin-se unix 3 [ ] 流 已连接 55449 1800/sogoupinyin-wa unix 3 [ ] 流 已连接 55456 1806/sogoupinyin-se linux@ubuntu:~$ 

11.2 测试网络连接

1. ping 命令

​ ping命令的功能是用于测试主机间网络连通性,发送出基于ICMP传输协议的数据包,要求对方主机予以回复,若对方主机的网络功能没有问题且防火墙放行流量,则就会回复该信息,我们也就可得知对方主机系统在线并运行正常了。

不过值得我们注意的是Linux与Windows相比有一定差异,Windows系统下的ping命令会发送出去4个请求后自动结束该命令;而Linux系统则不会自动终止,需要用户手动按下组合键“Ctrl+c”才能结束,或是发起命令时加入-c参数限定发送个数。

**语法格式:**ping [参数] 目标主机(ip地址或域名)

常用参数:

-d 使用Socket的SO_DEBUG功能
-c 指定发送报文的次数
-i 指定收发信息的间隔时间
-I 使用指定的网络接口送出数据包
-l 设置在送出要求信息之前,先行发出的数据包
-n 只输出数值
-p 设置填满数据包的范本样式
-q 不显示指令执行过程
-R 记录路由过程
-s 设置数据包的大小
-t 设置存活数值TTL的大小
-v 详细显示指令的执行过程
ping 192.168.1.1 # 可以测试 电脑和路由器之间是否连接可以通信 Ctrl+c结束测试 ping www.baidu.com # 测试外网的联通 ping -c 4 www.baidu.com # 发送4个数据包 

参考实例

测试与指定网站服务器之间的网络连通性(需手动按下“Ctrl+c”组合键结束命令):

linux@ubuntu:~$ ping 192.168.1.1 # 可以测试 电脑和路由器之间是否连接可以通信 Ctrl+c结束测试 PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data. 64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=2.18 ms 64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=4.27 ms 64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=4.19 ms 64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=2.82 ms 64 bytes from 192.168.1.1: icmp_seq=5 ttl=64 time=2.67 ms 64 bytes from 192.168.1.1: icmp_seq=6 ttl=64 time=4.11 ms 64 bytes from 192.168.1.1: icmp_seq=7 ttl=64 time=2.48 ms 64 bytes from 192.168.1.1: icmp_seq=8 ttl=64 time=2.84 ms 64 bytes from 192.168.1.1: icmp_seq=9 ttl=64 time=2.38 ms ^C --- 192.168.1.1 ping statistics --- 9 packets transmitted, 9 received, 0% packet loss, time 8016ms rtt min/avg/max/mdev = 2.181/3.109/4.278/0.795 ms linux@ubuntu:~$ ping www.baidu.com # 测试外网的联通 PING www.a.shifen.com (39.156.66.18) 56(84) bytes of data. 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=1 ttl=53 time=11.1 ms 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=2 ttl=53 time=11.9 ms 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=3 ttl=53 time=11.7 ms 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=4 ttl=53 time=11.7 ms 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=5 ttl=53 time=12.8 ms ^C --- www.a.shifen.com ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 4007ms rtt min/avg/max/mdev = 11.142/11.885/12.808/0.550 ms linux@ubuntu:~$ ping www.github.com PING github.com (20.205.243.166) 56(84) bytes of data. ^C --- github.com ping statistics --- 11 packets transmitted, 0 received, 100% packet loss, time 10239ms linux@ubuntu:~$ ^C linux@ubuntu:~$ ping www.ubuntu.com.cn # 测试ubuntu的网速  PING www.ubuntu.com.cn (104.160.18.24) 56(84) bytes of data. 64 bytes from 104.160.18.24 (104.160.18.24): icmp_seq=1 ttl=51 time=276 ms 64 bytes from 104.160.18.24 (104.160.18.24): icmp_seq=2 ttl=51 time=203 ms 64 bytes from 104.160.18.24 (104.160.18.24): icmp_seq=3 ttl=51 time=203 ms 64 bytes from 104.160.18.24 (104.160.18.24): icmp_seq=4 ttl=51 time=204 ms 64 bytes from 104.160.18.24 (104.160.18.24): icmp_seq=5 ttl=51 time=203 ms ^C --- www.ubuntu.com.cn ping statistics --- 5 packets transmitted, 5 received, 0% packet loss, time 7738ms rtt min/avg/max/mdev = 203.182/218.121/276.323/29.102 ms linux@ubuntu:~$ ping mirrors.yun-idc.com PING mirrors.yun-idc.com (114.112.43.174) 56(84) bytes of data. 64 bytes from 114.112.43.174 (114.112.43.174): icmp_seq=1 ttl=51 time=16.6 ms 64 bytes from 114.112.43.174 (114.112.43.174): icmp_seq=2 ttl=51 time=14.8 ms 64 bytes from 114.112.43.174 (114.112.43.174): icmp_seq=3 ttl=51 time=14.7 ms 64 bytes from 114.112.43.174 (114.112.43.174): icmp_seq=4 ttl=51 time=15.2 ms ^C --- mirrors.yun-idc.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 12028ms rtt min/avg/max/mdev = 14.789/15.403/16.682/0.768 ms linux@ubuntu:~$ ping mirrors.cn99.com PING mirrors.s.3322.net (118.184.169.32) 56(84) bytes of data. 64 bytes from h118-184-169-32.pubyun.com (118.184.169.32): icmp_seq=1 ttl=53 time=27.1 ms 64 bytes from h118-184-169-32.pubyun.com (118.184.169.32): icmp_seq=2 ttl=53 time=29.8 ms 64 bytes from h118-184-169-32.pubyun.com (118.184.169.32): icmp_seq=3 ttl=53 time=29.4 ms 64 bytes from h118-184-169-32.pubyun.com (118.184.169.32): icmp_seq=4 ttl=53 time=27.6 ms ^C --- mirrors.s.3322.net ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3005ms rtt min/avg/max/mdev = 27.176/28.512/29.836/1.146 ms linux@ubuntu:~$ linux@ubuntu:~$ ping -c 4 www.baidu.com PING www.baidu.com (39.156.66.18) 56(84) bytes of data. 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=1 ttl=53 time=94.0 ms 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=2 ttl=53 time=11.5 ms 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=3 ttl=53 time=14.9 ms 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=4 ttl=53 time=13.5 ms --- www.baidu.com ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3005ms rtt min/avg/max/mdev = 11.570/33.537/94.097/34.984 ms linux@ubuntu:~$ 
2. traceroute命令

​ traceroute命令用于追踪数据包在网络上的传输时的全部路径,它默认发送的数据包大小是40字节。通过traceroute我们可以知道信息从你的计算机到互联网另一端的主机是走的什么路径。当然每次数据包由某一同样的出发点(source)到达某一同样的目的地(destination)走的路径可能会不一样,但基本上来说大部分时候所走的路由是相同的。

traceroute通过发送小的数据包到目的设备直到其返回,来测量其需要多长时间。一条路径上的每个设备traceroute要测3次。输出结果中包括每次测试的时间(ms)和设备的名称(如有的话)及其ip地址。

**语法格式:**traceroute [参数] [域名或者IP]

参考实例

linux@ubuntu:~$ traceroute Command 'traceroute' not found, but can be installed with: sudo apt install inetutils-traceroute sudo apt install traceroute linux@ubuntu:~$ traceroute linux@ubuntu:~$ traceroute www.baidu.com traceroute to www.a.shifen.com (39.156.66.18), 64 hops max 1 192.168.1.1 2.693ms 1.952ms 3.894ms 2 10.218.0.1 7.793ms 6.179ms 5.989ms 3 117.131.130.109 6.204ms 6.087ms 4.574ms 4 221.183.63.133 4.546ms 4.183ms * 5 221.183.37.185 13.826ms 6.143ms 7.969ms 6 221.183.49.126 14.798ms 16.952ms 21.972ms 7 * 39.156.27.1 10.678ms 9.957ms 8 39.156.67.33 27.424ms 23.590ms 13.295ms 9 39.156.67.41 17.232ms 16.121ms 15.431ms 10 * * * 11 * * * 12 * * * 13 * * * 14 * * * 15 * * * 16 * ^C linux@ubuntu:~$ ^C linux@ubuntu:~$ ping www.baidu.com PING www.a.shifen.com (39.156.66.18) 56(84) bytes of data. 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=1 ttl=53 time=11.6 ms 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=2 ttl=53 time=17.7 ms ^C --- www.a.shifen.com ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1002ms rtt min/avg/max/mdev = 11.630/14.676/17.722/3.046 ms linux@ubuntu:~$ 

11.3 修改网络配置

1. 图形化配置(network-manager)

​ Ubuntu有Server版和Desktop版,又有直接安装在处理器上和装在虚拟机上。
​ interfaces和 nm(network-manager)之间的关系,当系统内没有第三方网络管理工具(比如nm)时,系统默认使用interfaces文件内的参数进行网络配置。接着,当系统内安装了 nm之后,nm默认接管了系统的网络配置,使用nm 自己的网络配置参数来进行配置。
但是,如果用户在安装nm之后(Desktop版本默认安装了nm),自己手动修改了interfaces 文件,那nm 就自动停止对系统网络的管理,系统改使用interfaces 文件内的参数进行网络配置。此时,再去修改nm 内的参数,不影响系统实际的网络配置。若要让nm 内的配置生效,必须重新启用nm 接管系统的网络配置。

设置静态ip上网

-> 开始图标(显示应用程序) -> 设置 -> 网络 -> 有线连接 -> 齿轮工具 -> 点击IPV4 选项卡 -> 选择"手动" -> 通过"详细信息"查看, 可以知道主机的ip 是 192.168.1.103, 路由器的信息就是192.168.1.1 -> 地址栏 手动输入ip信息: 地址 : 192.168.1.10 子网掩码: 255.255.255.0 网关 : 192.168.1.1 DNS :自动关闭 , 输入 114.114.114.114 路由 :自动 -> 应用 -> 在网络配置界面中, 有线连接选项中 关闭网络 -> 在网络配置界面中, 有线连接选项中 开启网络 (修改完ip后要重启网络) 
linux@ubuntu:~$ ping www.baidu.com PING www.a.shifen.com (39.156.66.18) 56(84) bytes of data. 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=1 ttl=53 time=11.6 ms 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=2 ttl=53 time=17.7 ms ^C --- www.a.shifen.com ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1002ms rtt min/avg/max/mdev = 11.630/14.676/17.722/3.046 ms linux@ubuntu:~$ linux@ubuntu:~$ ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::3901:d959:9bc4:b96 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:d1:30:6c txqueuelen 1000 (以太网) RX packets 8999 bytes  (4.1 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 3645 bytes  (368.5 KB) 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 (本地环回) RX packets 1206 bytes  (104.2 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1206 bytes  (104.2 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 linux@ubuntu:~$ ping www.baidu.com PING www.baidu.com (39.156.66.18) 56(84) bytes of data. 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=1 ttl=53 time=11.5 ms 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=2 ttl=53 time=13.0 ms 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=3 ttl=53 time=11.9 ms ^C --- www.baidu.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 11025ms rtt min/avg/max/mdev = 11.596/12.216/13.072/0.637 ms linux@ubuntu:~$ 

设置动态ip上网

-> 开始图标(显示应用程序) -> 设置 -> 网络 -> 有线连接 -> 齿轮工具 -> 点击IPV4 选项卡 -> 选择"自动", 让路由器自动分配一个ip地址 DNS :自动 路由 :自动 -> 应用 -> 在网络配置界面中, 有线连接选项中 关闭网络 -> 在网络配置界面中, 有线连接选项中 开启网络 (修改完ip后要重启网络) 
linux@ubuntu:~$ ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.103 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::3901:d959:9bc4:b96 prefixlen 64 scopeid 0x20<link> ether 00:0c:29:d1:30:6c txqueuelen 1000 (以太网) RX packets 9588 bytes  (4.2 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 4111 bytes  (427.0 KB) 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 (本地环回) RX packets 1386 bytes  (119.3 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1386 bytes  (119.3 KB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 linux@ubuntu:~$ ping www.baidu.com PING www.baidu.com (39.156.66.18) 56(84) bytes of data. 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=1 ttl=53 time=11.6 ms 64 bytes from 39.156.66.18 (39.156.66.18): icmp_seq=2 ttl=53 time=11.3 ms ^C --- www.baidu.com ping statistics --- 2 packets transmitted, 2 received, 0% packet loss, time 1002ms rtt min/avg/max/mdev = 11.377/11.505/11.633/0.128 ms linux@ubuntu:~$ 
2. 命令行配置(不建议使用,建议使用图形化)

​ 关于Ubuntu网络配置文件说明:ubuntu从17.10开始,已放弃在/etc/network/interfaces里固定IP的配置,即使配置也不会生效,而是改成netplan方式 ,网卡配置文件路径在:/etc/netplan/文件下,一般后缀名为.yaml文件;可自行切换到/etc/netplan文件下自行查看,我的是:01-network-manager-all.yaml(如果没有.yaml后缀文件可以使用gedit 01-network-manager-all.yaml自己创建(非root账号,命令前加sudo))。
在Ubuntu系的Linux系统里,与网络相关的主要设置文件

#  # Let NetworkManager manage all devices on this system network: version: 2 renderer: NetworkManager ethernets: ens33: #配置的网卡名称 addresses: [192.168.1.20/24] #设置IP掩码 , 24 是掩码的位数 255.255.255.0 , 3个255 正好是三个字节,就是24位 gateway4: 192.168.1.1 #设置网关 nameservers: addresses: [114.114.114.114] #设置dns #注意点: 1.以上配置文件共11行,其中第2,3,6,7四行可以不写,测试过没有这四行,网络也能工作正常,第5行的ens33为虚拟网卡,可以使用ifconfig -a查看本机的网卡。 2.配置文件里在冒号:号出现的后面一定要空一格,不空格则在运行netplan apply时提示出错。 3.关键之关键是看清配置总共分为五个层次,逐层向后至少空一格, 第一层-network: 第二层-- ethernets: 第三层--- ens33: 第四层----addresses: [192.168.1.55/24] 第四层----gateway4: 192.168.1.1 第四层----nameservers: 第五层-----addresses: [114.114.114.114, 8.8.8.8] 

修改ip和网关信息

配置文件内容如下: /etc/netplan/01-network-manager-all.yaml

# Let NetworkManager manage all devices on this system network: version: 2 renderer: NetworkManager ethernets: ens33: addresses: [192.168.1.20/24] gateway4: 192.168.1.1 nameservers: addresses: [114.114.114.114] 
linux@ubuntu:~$ sudo vi /etc/netplan/01-network-manager-all.yaml linux@ubuntu:~$ sudo netplan apply linux@ubuntu:~$ ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.20 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::20c:29ff:fed1:306c prefixlen 64 scopeid 0x20<link> ether 00:0c:29:d1:30:6c txqueuelen 1000 (以太网) RX packets 1110 bytes  (145.3 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1507 bytes  (198.4 KB) 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 (本地环回) RX packets 23621 bytes  (1.6 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 23621 bytes  (1.6 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 linux@ubuntu:~$ # 在图形界面下, 把netplay-ens33 这个名称改为 static , 这个配置表示是静态ip  # 在图形界面下, 把ens33 这个名称改为 auto ,这个配置表示是静态ip  # 切换到auto  linux@ubuntu:~$ ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.103 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::fb81:9220:45a:8e prefixlen 64 scopeid 0x20<link> ether 00:0c:29:d1:30:6c txqueuelen 1000 (以太网) RX packets 1134 bytes  (147.9 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 1575 bytes  (207.8 KB) 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 (本地环回) RX packets 23655 bytes  (1.6 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 23655 bytes  (1.6 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 linux@ubuntu:~$ 
3. 网络图标消失的解决办法
# 手动的让网络图标消失  linux@ubuntu:~$ ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.103 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::fb81:9220:45a:8e prefixlen 64 scopeid 0x20<link> inet6 2409:8a02:7826:b7f0:c027:1733:cfe1:1684 prefixlen 64 scopeid 0x0<global> inet6 2409:8a02:7826:b7f0:c575:a926:1076:de45 prefixlen 64 scopeid 0x0<global> ether 00:0c:29:d1:30:6c txqueuelen 1000 (以太网) RX packets 745 bytes  (104.0 KB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 558 bytes 75292 (75.2 KB) 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 (本地环回) RX packets 22970 bytes  (1.6 MB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 22970 bytes  (1.6 MB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 linux@ubuntu:~$ sudo ifconfig ens33 down # 关闭网卡 ens33  [sudo] linux 的密码: # 启动网卡ens33 , 启动后, 网络图标并不会出现, 这个时候可以, 可以在设置中找到网络进行配置 linux@ubuntu:~$ sudo ifconfig ens33 up # 如果以上方法解决不了, 可以使用下面的解决办法  sudo service network-manager stop # 把网络管理服务关闭  sudo rm /var/lib/NetworkManager/NetworkManager.state # 删除这个状态文件, 重启服务器后, 会自动生成这个文件 sudo service network-manager start # 再次启动服务  sudo vi /etc/NetworkManager/NetworkManager.conf #把false改成true sudo service network-manager restart # 去设置-> 网络 再进行操作 
THE END

发表回复