第7章 网络管理

第7章 网络管理

7.1 ip 命令

使用 ip 命令可以配置临时网络的连接信息,相关命令如下:

 ip link:显示网络设备运行状态
 ip -s link:显示更详细的设备信息
 ip link show [网络设备名]:仅显示指定的网络设备名的信息
 ip link show up :仅显示当前处于激活状态的设备信息
 ip link set [网络设备名] down:将指定的网络设备下线
 ​
 ​
 ip [-s] addr|a show [网络设备名]:查看指定网络设备(网卡名称)的配置信息,如果加了 -s 选项,则会添加相关的统计信息,如果接收到的或传送的数据包等
 ip [-4] addr|a add IP地址[/掩码] dev [网络设备名]:添加一个临时的 IPv4 的地址(重启后失效)
 ip -6 addr|a add IP地址[/掩码] dev [网络设备名]:添加一个临时的 IPv6 的地址
 ip [-4] addr|a del IP地址[/掩码] dev [网络设备名]:删除网卡的临时IPv4地址

使用示例:

 [root@bogon system-connections]# ip link
 1: lo:  mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
 2: ens160:  mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
     link/ether 00:0c:29:08:da:ba brd ff:ff:ff:ff:ff:ff
     altname enp3s0
 ​
 [root@bogon system-connections]# ip -s link
 1: lo:  mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
     RX:  bytes packets errors dropped  missed   mcast           
           2149      19      0       0       0       0 
     TX:  bytes packets errors dropped carrier collsns           
           2149      19      0       0       0       0 
 2: ens160:  mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
     link/ether 00:0c:29:08:da:ba brd ff:ff:ff:ff:ff:ff
     RX:  bytes packets errors dropped  missed   mcast           
          62860     738      0       0       0      12 
     TX:  bytes packets errors dropped carrier collsns           
          69299     698      0       0       0       0 
     altname enp3s0
     
     
 [root@bogon system-connections]# ip link show ens160
 2: ens160:  mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
     link/ether 00:0c:29:08:da:ba brd ff:ff:ff:ff:ff:ff
     altname enp3s0
 ​
 [root@bogon system-connections]# ip -s link show ens160
 2: ens160:  mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
     link/ether 00:0c:29:08:da:ba brd ff:ff:ff:ff:ff:ff
     RX:  bytes packets errors dropped  missed   mcast           
          70456     831      0       0       0      12 
     TX:  bytes packets errors dropped carrier collsns           
          75925     759      0       0       0       0 
     altname enp3s0
     
 ​
 [root@bogon system-connections]# ip a
 1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
     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: ens160:  mtu 1500 qdisc mq state UP group default qlen 1000
     link/ether 00:0c:29:08:da:ba brd ff:ff:ff:ff:ff:ff
     altname enp3s0
     inet 192.168.72.128/24 brd 192.168.72.255 scope global dynamic noprefixroute ens160
        valid_lft 1562sec preferred_lft 1562sec
     inet6 fe80::20c:29ff:fe08:daba/64 scope link noprefixroute 
        valid_lft forever preferred_lft forever
        
        
 [root@bogon system-connections]# ip -s a
 1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
     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
     RX:  bytes packets errors dropped  missed   mcast           
           2149      19      0       0       0       0 
     TX:  bytes packets errors dropped carrier collsns           
           2149      19      0       0       0       0 
 2: ens160:  mtu 1500 qdisc mq state UP group default qlen 1000
     link/ether 00:0c:29:08:da:ba brd ff:ff:ff:ff:ff:ff
     altname enp3s0
     inet 192.168.72.128/24 brd 192.168.72.255 scope global dynamic noprefixroute ens160
        valid_lft 1544sec preferred_lft 1544sec
     inet6 fe80::20c:29ff:fe08:daba/64 scope link noprefixroute 
        valid_lft forever preferred_lft forever
     RX:  bytes packets errors dropped  missed   mcast           
          73136     865      0       0       0      12 
     TX:  bytes packets errors dropped carrier collsns           
          79535     784      0       0       0       0
          
          
 [root@bogon system-connections]# ip -s a show ens160
 2: ens160:  mtu 1500 qdisc mq state UP group default qlen 1000
     link/ether 00:0c:29:08:da:ba brd ff:ff:ff:ff:ff:ff
     altname enp3s0
     inet 192.168.72.128/24 brd 192.168.72.255 scope global dynamic noprefixroute ens160
        valid_lft 1484sec preferred_lft 1484sec
     inet6 fe80::20c:29ff:fe08:daba/64 scope link noprefixroute 
        valid_lft forever preferred_lft forever
     RX:  bytes packets errors dropped  missed   mcast           
          77056     915      0       0       0      12 
     TX:  bytes packets errors dropped carrier collsns           
          83451     814      0       0       0       0
          
 # 添加一个临时的 ip 地址        
 [root@bogon system-connections]# ip -4 a add 192.168.72.120/24 dev ens160
 [root@bogon system-connections]# ip a show ens160
 2: ens160:  mtu 1500 qdisc mq state UP group default qlen 1000
     link/ether 00:0c:29:08:da:ba brd ff:ff:ff:ff:ff:ff
     altname enp3s0
     inet 192.168.72.128/24 brd 192.168.72.255 scope global dynamic noprefixroute ens160
        valid_lft 1339sec preferred_lft 1339sec
     inet 192.168.72.120/24 scope global secondary ens160
        valid_lft forever preferred_lft forever
     inet6 fe80::20c:29ff:fe08:daba/64 scope link noprefixroute 
        valid_lft forever preferred_lft forever
 ​
 # 删除一个临时的 IP 地址
 [root@bogon system-connections]# ip -4 a del 192.168.72.120/24 dev ens160
 [root@bogon system-connections]# ip a show ens160
 2: ens160:  mtu 1500 qdisc mq state UP group default qlen 1000
     link/ether 00:0c:29:08:da:ba brd ff:ff:ff:ff:ff:ff
     altname enp3s0
     inet 192.168.72.128/24 brd 192.168.72.255 scope global dynamic noprefixroute ens160
        valid_lft 1212sec preferred_lft 1212sec
     inet6 fe80::20c:29ff:fe08:daba/64 scope link noprefixroute 
        valid_lft forever preferred_lft forever

7.2 修改配置文件

在 redhat 9 中,已经不推荐修改配置文件了,而推荐使用命令的方式来进行配置。

要修改配置文件,我们需要编辑 /etc/NetworkManager/system-connections 目录下的 ens160.nmconnection 文件中。

注意:不同的电脑,这个文件的名称可能不同。

[root@bogon system-connections]# cat ens160.nmconnection
[connection]	# 网络连接配置
id=ens160	# 网卡标识,这个名称可以和网卡名称不一致,只要唯一即可
uuid=1f212fa6-bb52-3c62-ab97-66818fb871a1	# 网络的标识
type=ethernet		# 网络类型,默认情况下是以太网
autoconnect-priority=-999	# 自动连接策略
interface-name=ens160		# 指定网卡名称
timestamp=1697397259

[ethernet]

[ipv4]		# ipv4的配置
method=auto

[ipv6]		# ipv6的配置
addr-gen-mode=eui64
method=auto

[proxy]

下面我们把这个文件编辑一下,把我们固定 IP 写到这个文件中,并指定 dns 和 网关。

[root@bogon system-connections]# cd /etc/NetworkManager/system-connections/
[root@bogon system-connections]# ll
total 4
-rw-------. 1 root root 229 Oct 15 19:43 ens160.nmconnection
[root@bogon system-connections]# vim ens160.nmconnection

编辑这个文件的内容如下:

#省略
[ipv4]
address=192.168.72.121/24,192.168.72.2
dns=8.8.8.8;192.168.72.2
method=manual
# 省略

7.3 nmcli 命令*

7.3.1 查看网卡设备

语法:nmcli device 或者简写为 nmcli d

[root@localhost system-connections]# nmcli device 
DEVICE  TYPE      STATE                   CONNECTION 
ens160  ethernet  connected               ens160     
lo      loopback  connected (externally)  lo 

[root@localhost system-connections]# nmcli d
DEVICE  TYPE      STATE                   CONNECTION 
ens160  ethernet  connected               ens160     
lo      loopback  connected (externally)  lo

# 查看网关
[root@localhost system-connections]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.72.2    0.0.0.0         UG    100    0        0 ens160
192.168.72.0    0.0.0.0         255.255.255.0   U     100    0        0 ens160

# 查看详细信息
[root@localhost system-connections]# nmcli d show
GENERAL.DEVICE:                         ens160
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         00:0C:29:08:DA:BA
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     ens160
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/4
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         192.168.72.121/24
IP4.GATEWAY:                            192.168.72.2
IP4.ROUTE[1]:                           dst = 192.168.72.0/24, nh = 0.0.0.0, mt = 100
IP4.ROUTE[2]:                           dst = 0.0.0.0/0, nh = 192.168.72.2, mt = 100
IP4.DNS[1]:                             8.8.8.8
IP4.DNS[2]:                             192.168.72.2
IP6.ADDRESS[1]:                         fe80::20c:29ff:fe08:daba/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 1024

GENERAL.DEVICE:                         lo
GENERAL.TYPE:                           loopback
GENERAL.HWADDR:                         00:00:00:00:00:00
GENERAL.MTU:                            65536
GENERAL.STATE:                          100 (connected (externally))
GENERAL.CONNECTION:                     lo
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/3
IP4.ADDRESS[1]:                         127.0.0.1/8
IP4.GATEWAY:                            --
IP6.ADDRESS[1]:                         ::1/128
IP6.GATEWAY:                            --

# 显示指定设备的详细信息
[root@localhost system-connections]# nmcli d show ens160
GENERAL.DEVICE:                         ens160
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         00:0C:29:08:DA:BA
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     ens160
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/4
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         192.168.72.121/24
IP4.GATEWAY:                            192.168.72.2
IP4.ROUTE[1]:                           dst = 192.168.72.0/24, nh = 0.0.0.0, mt = 100
IP4.ROUTE[2]:                           dst = 0.0.0.0/0, nh = 192.168.72.2, mt = 100
IP4.DNS[1]:                             8.8.8.8
IP4.DNS[2]:                             192.168.72.2
IP6.ADDRESS[1]:                         fe80::20c:29ff:fe08:daba/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 1024


# 查看当前连接设备信息
[root@localhost system-connections]# nmcli d connect ens160
Device 'ens160' successfully activated with '1f212fa6-bb52-3c62-ab97-66818fb871a1'.

# 断开网络,也就是把网卡禁用了
[root@localhost system-connections]# nmcli d disconnect ens160
# 执行完上面的命令后,xshell 会断掉,要想重新连接上,我们需要在 虚拟机中执行
# nmcli d connet ens160

7.3.2 查看网卡配置

语法:nmcli connectioin 或简写为 nmcli c

# 查看网卡信息
[root@localhost ~]# nmcli connection
NAME    UUID                                  TYPE      DEVICE 
ens160  1f212fa6-bb52-3c62-ab97-66818fb871a1  ethernet  ens160 
lo      5a6a3e72-0ca9-46a8-82b3-8b6696d14cff  loopback  lo 

[root@localhost ~]# nmcli c
NAME    UUID                                  TYPE      DEVICE 
ens160  1f212fa6-bb52-3c62-ab97-66818fb871a1  ethernet  ens160 
lo      5a6a3e72-0ca9-46a8-82b3-8b6696d14cff  loopback  lo 


# 查看自动获取的网关地址
[root@localhost ~]# nmcli c show ens160 | grep -i gateway
connection.gateway-ping-timeout:        0
ipv4.gateway:                           192.168.72.2
ipv6.gateway:                           --
IP4.GATEWAY:                            192.168.72.2
IP6.GATEWAY:                            --

# 查看当前网卡的dns配置
[root@localhost ~]# nmcli c show ens160 | grep -i dns
connection.mdns:                        -1 (default)
connection.dns-over-tls:                -1 (default)
ipv4.dns:                               8.8.8.8,192.168.72.2
ipv4.dns-search:                        --
ipv4.dns-options:                       --
ipv4.dns-priority:                      0
ipv4.ignore-auto-dns:                   no
ipv6.dns:                               --
ipv6.dns-search:                        --
ipv6.dns-options:                       --
ipv6.dns-priority:                      0
ipv6.ignore-auto-dns:                   no
IP4.DNS[1]:                             8.8.8.8
IP4.DNS[2]:                             192.168.72.2

7.3.3 配置网卡IP

1)非交互式*

需求:修改 ens160 的 ip 地址为 192.168.72.128,并且是 ipv4,自动连接,dns为 114.114.114.114,网关为 192.168.72.2,子网掩码为 255.255.255.0,然后再添加一个 ip 地址:192.168.72.125

# 配置网卡
[root@localhost ~]# nmcli c modify ens160 ipv4.method manual autoconnect yes ipv4.addresses 192.168.72.128/24 ipv4.gateway 192.168.72.2 ipv4.dns 114.114.114.114 +ipv4.addresses 192.168.72.125/24

# 查看配置的结果
[root@localhost ~]# cat /etc/NetworkManager/system-connections/ens160.nmconnection 
[connection]
id=ens160
uuid=1f212fa6-bb52-3c62-ab97-66818fb871a1
type=ethernet
autoconnect-priority=-999
interface-name=ens160
timestamp=1702817105

[ethernet]

[ipv4]
address1=192.168.72.128/24,192.168.72.2
address2=192.168.72.125/24
dns=114.114.114.114;
method=manual

[ipv6]
addr-gen-mode=eui64
method=auto

[proxy]

命令说明:

  • nmcli c modify ens160:表示要修改的网卡

  • ipv4.method manual:表示网络连接方式为手动配置

  • autoconnect yes:表示自动连接,即系统一启动网络就可以用

  • ipv4.addresses 192.168.72.128/24:配置 ipv4 格式的 IP 地址和子网掩码

  • ipv4.gateway 192.168.72.2:配置网关信息

  • ipv4.dns 114.114.114.114:配置dns服务

  • +ipv4.addresses 192.168.72.125/24:表示再添加一个 IP 地址

2)交互式

[root@localhost ~]# nmcli c edit ens160

===| nmcli interactive connection editor |===

Editing existing '802-3-ethernet' connection: 'ens160'

Type 'help' or '?' for available commands.
Type 'print' to show all the connection properties.
Type 'describe [.]' for detailed property description.

You may edit the following settings: connection, 802-3-ethernet (ethernet), 802-1x, dcb, sriov, ethtool, match, ipv4, ipv6, hostname, tc, proxy
nmcli> goto ipv4
You may edit the following properties: method, dns, dns-search, dns-options, dns-priority, addresses, gateway, routes, route-metric, route-table, routing-rules, replace-local-rule, ignore-auto-routes, ignore-auto-dns, dhcp-client-id, dhcp-iaid, dhcp-timeout, dhcp-send-hostname, dhcp-hostname, dhcp-fqdn, dhcp-hostname-flags, never-default, may-fail, required-timeout, dad-timeout, dhcp-vendor-class-identifier, link-local, dhcp-reject-servers, auto-route-ext-gw
nmcli ipv4> set dns 192.168.72.2
nmcli ipv4> activate
Error: connection is not saved. Type 'save' first.
nmcli ipv4> save
Connection 'ens160' (1f212fa6-bb52-3c62-ab97-66818fb871a1) successfully updated.
nmcli ipv4> activate
Monitoring connection activation (press any key to continue)
nmcli ipv4> quit


[root@localhost ~]# nmcli c show ens160 | grep -i dns
connection.mdns:                        -1 (default)
connection.dns-over-tls:                -1 (default)
ipv4.dns:                               114.114.114.114,192.168.72.2
ipv4.dns-search:                        --
ipv4.dns-options:                       --
ipv4.dns-priority:                      0
ipv4.ignore-auto-dns:                   no
ipv6.dns:                               --
ipv6.dns-search:                        --
ipv6.dns-options:                       --
ipv6.dns-priority:                      0
ipv6.ignore-auto-dns:                   no
IP4.DNS[1]:                             114.114.114.114
IP4.DNS[2]:                             192.168.72.2

7.3.4 ping命令

这个命令是用于测试网络是否申通。

语法:ping [options]

# 测试本机的网卡是否可用,按 ctrl + c 退出
[root@localhost ~]# ping 192.168.72.121
PING 192.168.72.121 (192.168.72.121) 56(84) bytes of data.
64 bytes from 192.168.72.121: icmp_seq=1 ttl=64 time=0.033 ms
64 bytes from 192.168.72.121: icmp_seq=2 ttl=64 time=0.076 ms
64 bytes from 192.168.72.121: icmp_seq=3 ttl=64 time=0.115 ms
64 bytes from 192.168.72.121: icmp_seq=4 ttl=64 time=0.083 ms
64 bytes from 192.168.72.121: icmp_seq=5 ttl=64 time=0.091 ms
64 bytes from 192.168.72.121: icmp_seq=6 ttl=64 time=0.063 ms
64 bytes from 192.168.72.121: icmp_seq=7 ttl=64 time=0.066 ms
64 bytes from 192.168.72.121: icmp_seq=8 ttl=64 time=0.067 ms
64 bytes from 192.168.72.121: icmp_seq=9 ttl=64 time=0.085 ms
^C
--- 192.168.72.121 ping statistics ---
9 packets transmitted, 9 received, 0% packet loss, time 8211ms
rtt min/avg/max/mdev = 0.033/0.075/0.115/0.021 ms

# 测试网络是否申通
[root@localhost ~]# ping www.baidu.com
PING www.a.shifen.com (220.181.38.150) 56(84) bytes of data.
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=1 ttl=128 time=68.3 ms
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=2 ttl=128 time=57.4 ms
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=3 ttl=128 time=67.2 ms

# 指定发送包的次数,通过 -c选项来指定,后面跟上发包的次数
[root@localhost ~]# ping -c2 www.baidu.com
PING www.a.shifen.com (220.181.38.150) 56(84) bytes of data.
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=1 ttl=128 time=60.3 ms
64 bytes from 220.181.38.150 (220.181.38.150): icmp_seq=2 ttl=128 time=59.8 ms

--- www.a.shifen.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 59.762/60.038/60.314/0.276 ms

7.3.5 连接、断开和删除设备

# 连接网络,即让网卡可用
[root@localhost ~]# nmcli c up ens160
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/8)

# 断开网络
[root@localhost ~]# nmcli c down ens160

# 删除网络,注意这个命令不要执行
[root@localhost ~]# nmcli c delete ens160

7.4 nmtui 命令

这个命令是用于可视化的方式来进行配置,要想实现这个功能,我们需要先执行如下的操作。

# 启用 cockpit 服务
[root@localhost ~]# systemctl start cockpit

打开浏览器,然后访问:https://192.168.72.128:9090/,并用普通用户登录。

7.5 从网站上下载文件

7.5.1 wget

下面以 QQ 安装文件为例:

[root@localhost ~]# wget https://dldir1.qq.com/qqfile/qq/QQNT/06d558c3/linuxqq_3.2.3-19189_x86_64.rpm
--2023-12-17 21:24:57--  https://dldir1.qq.com/qqfile/qq/QQNT/06d558c3/linuxqq_3.2.3-19189_x86_64.rpm
Resolving dldir1.qq.com (dldir1.qq.com)... 175.153.171.177
Connecting to dldir1.qq.com (dldir1.qq.com)|175.153.171.177|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 129160016 (123M) [application/octet-stream]
Saving to: ‘linuxqq_3.2.3-19189_x86_64.rpm’

linuxqq_3.2.3-19189_x86 100%[============================>] 123.18M  4.71MB/s    in 26s     

2023-12-17 21:25:24 (4.68 MB/s) - ‘linuxqq_3.2.3-19189_x86_64.rpm’ saved [129160016/129160016]

7.5.2 curl

这个命令是一个网络工具,其主要是通过 http、https、ftp 等方式下载或上传文件

[root@localhost ~]# curl www.baidu.com -o index.html
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2381  100  2381    0     0  11787      0 --:--:-- --:--:-- --:--:-- 11787


[root@localhost ~]# cat index.html 

 百度一下,你就知道  

关于百度 About Baidu

©2017 Baidu 使用百度前必读  意见反馈 京ICP证030173号 

你可能感兴趣的:(Linux,php,网络,服务器,linux)