OpenEuler网络管理

官网的OpenEuler网络管理文档已经够详细了,一般使用是查看网络配置、修改配置、启停网络,记住这个几个命令就非常方便了:

  • 查看网络设备nmcli device show
  • 查看网络连接nmcli connection show
  • 启动连接nmcli connection up id xxx
  • 关闭连接nmcli connection down id xxx
  • 删除连接nmcli conn delete xxx

配置网络 https://docs.openeuler.org/zh/docs/22.03_LTS_SP2/docs/Administration/配置网络.html

nmcli 命令格式

[root@olnode01 ~]# nmcli --help
Usage: nmcli [OPTIONS] OBJECT { COMMAND | help }

OPTIONS
  -a, --ask                                ask for missing parameters
  -c, --colors auto|yes|no                 whether to use colors in output
  -e, --escape yes|no                      escape columns separators in values
  -f, --fields <field,...>|all|common      specify fields to output
  -g, --get-values <field,...>|all|common  shortcut for -m tabular -t -f
  -h, --help                               print this help
  -m, --mode tabular|multiline             output mode
  -o, --overview                           overview mode
  -p, --pretty                             pretty output
  -s, --show-secrets                       allow displaying passwords
  -t, --terse                              terse output
  -v, --version                            show program version
  -w, --wait <seconds>                     set timeout waiting for finishing operations

OBJECT
  g[eneral]       NetworkManager's general status and operations
  n[etworking]    overall networking control
  r[adio]         NetworkManager radio switches
  c[onnection]    NetworkManager's connections
  d[evice]        devices managed by NetworkManager
  a[gent]         NetworkManager secret agent or polkit agent
  m[onitor]       monitor NetworkManager changes

查看命令

ask 这个提示还不错,最后直接提示了想看内容的命令

[root@olnode01 ~]# nmcli -a
enp0s8: connected to enp0s8
        "Intel 82540EM"
        ethernet (e1000), 08:00:27:8E:DF:04, hw, mtu 1500
        ip4 default
        inet4 10.0.3.15/24
        route4 0.0.0.0/0
        route4 10.0.3.0/24
        inet6 fe80::a00:27ff:fe8e:df04/64
        route6 fe80::/64

enp0s3: connected to enp0s3
        "Intel 82540EM"
        ethernet (e1000), 08:00:27:FB:32:93, hw, mtu 1500
        inet4 192.168.2.5/24
        route4 192.168.2.0/24
        inet6 fe80::a00:27ff:fefb:3293/64
        route6 fe80::/64

enp0s9: disconnected
        "Intel 82540EM"
        ethernet (e1000), 08:00:27:66:6D:82, hw, mtu 1500

lo: unmanaged
        "lo"
        loopback (unknown), 00:00:00:00:00:00, sw, mtu 65536

DNS configuration:
        servers: 202.96.128.86 114.114.114.114 223.5.5.5
        interface: enp0s8

Use "nmcli device show" to get complete information about known devices and
"nmcli connection show" to get an overview on active connection profiles.

Consult nmcli(1) and nmcli-examples(7) manual pages for complete usage details.

nmcli device show 可以看到所有网卡的所有信息

[root@olnode01 ~]# nmcli device show
GENERAL.DEVICE:                         enp0s8
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         08:00:27:8E:DF:04
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     enp0s8
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/2
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         10.0.3.15/24
IP4.GATEWAY:                            10.0.3.2
IP4.ROUTE[1]:                           dst = 0.0.0.0/0, nh = 10.0.3.2, mt = 101
IP4.ROUTE[2]:                           dst = 10.0.3.0/24, nh = 0.0.0.0, mt = 101
IP4.DNS[1]:                             202.96.128.86
IP4.DNS[2]:                             114.114.114.114
IP4.DNS[3]:                             223.5.5.5
IP6.ADDRESS[1]:                         fe80::a00:27ff:fe8e:df04/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 101

GENERAL.DEVICE:                         enp0s3
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         08:00:27:FB:32:93
GENERAL.MTU:                            1500
GENERAL.STATE:                          100 (connected)
GENERAL.CONNECTION:                     enp0s3
GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/ActiveConnection/1
WIRED-PROPERTIES.CARRIER:               on
IP4.ADDRESS[1]:                         192.168.2.5/24
IP4.GATEWAY:                            --
IP4.ROUTE[1]:                           dst = 192.168.2.0/24, nh = 0.0.0.0, mt = 100
IP6.ADDRESS[1]:                         fe80::a00:27ff:fefb:3293/64
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 100

GENERAL.DEVICE:                         enp0s9
GENERAL.TYPE:                           ethernet
GENERAL.HWADDR:                         08:00:27:66:6D:82
GENERAL.MTU:                            1500
GENERAL.STATE:                          30 (disconnected)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
WIRED-PROPERTIES.CARRIER:               on

GENERAL.DEVICE:                         lo
GENERAL.TYPE:                           loopback
GENERAL.HWADDR:                         00:00:00:00:00:00
GENERAL.MTU:                            65536
GENERAL.STATE:                          10 (unmanaged)
GENERAL.CONNECTION:                     --
GENERAL.CON-PATH:                       --
IP4.ADDRESS[1]:                         127.0.0.1/8
IP4.GATEWAY:                            --
IP6.ADDRESS[1]:                         ::1/128
IP6.GATEWAY:                            --
IP6.ROUTE[1]:                           dst = ::1/128, nh = ::, mt = 256

nmcli connection show 查看活跃的网卡

[root@olnode01 ~]# nmcli connection show
NAME    UUID                                  TYPE      DEVICE 
enp0s8  1abed0aa-7ce7-4788-85e9-d3b55c58ace2  ethernet  enp0s8 
enp0s3  c4858ad5-3910-47b2-8f28-e11f7cfb33a6  ethernet  enp0s3
[root@olnode01 ~]# nmcli connection show --active
NAME    UUID                                  TYPE      DEVICE 
enp0s8  1abed0aa-7ce7-4788-85e9-d3b55c58ace2  ethernet  enp0s8 
enp0s3  c4858ad5-3910-47b2-8f28-e11f7cfb33a6  ethernet  enp0s3

When --active option is specified, only the active profiles are shown.

nmcli general status 网络通用状态

[root@olnode01 ~]# nmcli general status
STATE      CONNECTIVITY  WIFI-HW  WIFI     WWAN-HW  WWAN    
connected  full          enabled  enabled  enabled  enabled

nmcli device status 网卡设备状态

[root@olnode01 ~]# nmcli device status
DEVICE  TYPE      STATE         CONNECTION 
enp0s8  ethernet  connected     enp0s8     
enp0s3  ethernet  connected     enp0s3     
enp0s9  ethernet  disconnected  --         
lo      loopback  unmanaged     --

管理命令

查看当前网卡

1: lo: <LOOPBACK,UP,LOWER_UP> 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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:fb:32:93 brd ff:ff:ff:ff:ff:ff
    inet 192.168.2.5/24 brd 192.168.2.255 scope global noprefixroute enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fefb:3293/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 08:00:27:8e:df:04 brd ff:ff:ff:ff:ff:ff
    inet 10.0.3.15/24 brd 10.0.3.255 scope global dynamic noprefixroute enp0s8
       valid_lft 85484sec preferred_lft 85484sec
    inet6 fe80::a00:27ff:fe8e:df04/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
4: enp0s9: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000

enp0s9网卡未连接,也可以通过nmcli device status 查看

nmcli device connect xxx 启动网卡连接,默认ONBOOT=yes

[root@olnode01 ~]# nmcli device connect enp0s9
Device 'enp0s9' successfully activated with '8f90774a-1882-4f2f-9b86-8adff03de94d'.

配置不存在时会创建ifcfg-xxx配置

[root@olnode01 network-scripts]# stat ifcfg-enp0s9
  File: ifcfg-enp0s9
  Size: 282             Blocks: 8          IO Block: 4096   regular file
Device: 253,0   Inode: 2887478     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Context: system_u:object_r:net_conf_t:s0
Access: 2024-01-29 09:29:56.700270423 +0800
Modify: 2024-01-29 09:29:56.600270486 +0800
Change: 2024-01-29 09:29:56.600270486 +0800
 Birth: 2024-01-29 09:29:56.600270486 +0800

当没有配置时,则无法激活网卡,或者手动先创建网卡配置位置文件 ifcfg-xxx,否则报如下错误

[root@olnode01 ~]# nmcli connection up id enp0s9
Error: unknown connection 'enp0s9'.

down and up 网卡

原先是 up 的状态

[root@olnode01 network-scripts]# nmcli device status
DEVICE  TYPE      STATE      CONNECTION 
enp0s8  ethernet  connected  enp0s8     
enp0s3  ethernet  connected  enp0s3     
enp0s9  ethernet  connected  enp0s9     
lo      loopback  unmanaged  --

nmcli connection down id enp0s9 down 之后

[root@olnode01 network-scripts]# nmcli connection down id enp0s9
Connection 'enp0s9' successfully deactivated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
[root@olnode01 network-scripts]# nmcli device status
DEVICE  TYPE      STATE         CONNECTION 
enp0s8  ethernet  connected     enp0s8     
enp0s3  ethernet  connected     enp0s3     
enp0s9  ethernet  disconnected  --         
lo      loopback  unmanaged     --

nmcli connection up id enp0s9 up 之后

[root@olnode01 network-scripts]# nmcli connection up id enp0s9
Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/4)
[root@olnode01 network-scripts]# nmcli device status
DEVICE  TYPE      STATE      CONNECTION 
enp0s8  ethernet  connected  enp0s8     
enp0s3  ethernet  connected  enp0s3     
enp0s9  ethernet  connected  enp0s9     
lo      loopback  unmanaged  --

nmcli device disconnect xxx断开设备,防止自动激活

[root@olnode01 network-scripts]# nmcli device disconnect enp0s9
Device 'enp0s9' successfully disconnected.
[root@olnode01 network-scripts]# nmcli device status
DEVICE  TYPE      STATE         CONNECTION 
enp0s8  ethernet  connected     enp0s8     
enp0s3  ethernet  connected     enp0s3     
enp0s9  ethernet  disconnected  --         
lo      loopback  unmanaged     --

再看连接状态

[root@olnode01 network-scripts]# nmcli conn show
NAME    UUID                                  TYPE      DEVICE 
enp0s8  1abed0aa-7ce7-4788-85e9-d3b55c58ace2  ethernet  enp0s8 
enp0s3  c4858ad5-3910-47b2-8f28-e11f7cfb33a6  ethernet  enp0s3 
enp0s9  8f90774a-1882-4f2f-9b86-8adff03de94d  ethernet  --     
[root@olnode01 network-scripts]# nmcli conn show --active
NAME    UUID                                  TYPE      DEVICE 
enp0s8  1abed0aa-7ce7-4788-85e9-d3b55c58ace2  ethernet  enp0s8 
enp0s3  c4858ad5-3910-47b2-8f28-e11f7cfb33a6  ethernet  enp0s3

删除连接

[root@olnode01 network-scripts]# nmcli conn delete enp0s9
Connection 'enp0s9' (16dfa56a-70fd-4742-b4f6-1e73906205c2) successfully deleted.
[root@olnode01 network-scripts]# ls
ifcfg-enp0s3  ifcfg-enp0s8

删除连接后,配置文件也被删除了

误删除掉配置文件后,也可以先删除连接再创建设备连接,再重新生成即可

[root@olnode01 network-scripts]# nmcli device connect enp0s9
Device 'enp0s9' successfully activated with '16dfa56a-70fd-4742-b4f6-1e73906205c2'.

nmcli 同样可以修改主机名

[root@olnode01 network-scripts]# nmcli general hostname
olnode01.bluemoon.ltd
[root@olnode01 network-scripts]# nmcli general hostname hname.xxx.com
[root@olnode01 network-scripts]# nmcli general hostname
hname.xxx.com
[root@olnode01 network-scripts]# hostnamectl set-hostname olnode01.bluemoon.ltd
[root@olnode01 network-scripts]# nmcli general hostname
olnode01.bluemoon.ltd
[root@olnode01 network-scripts]#
  • 11
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Jenvid.yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值