首頁>技術>

ifconfig命令慢慢要被拋棄了,推薦使用ip和nmcli/nmtui命令配置網路引數

檢視幫助

[root@vm2 ~]# nmcli --helpUsage: 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 [root@vm2 ~]#

顯示網口概況

[root@vm2 ~]# nmcli connection showNAME     UUID                                  TYPE      DEVICEenp0s8   154c1fcb-94cc-3a90-8b7e-11341c4bfc28  ethernet  enp0s8enp0s3   774306cb-6089-48ce-97e2-01d4834b1d6b  ethernet  enp0s3docker0  b6476980-b286-4487-bf83-44b3161b889d  bridge    docker0[root@vm2 ~]# nmcli connectionNAME     UUID                                  TYPE      DEVICEenp0s8   154c1fcb-94cc-3a90-8b7e-11341c4bfc28  ethernet  enp0s8enp0s3   774306cb-6089-48ce-97e2-01d4834b1d6b  ethernet  enp0s3docker0  b6476980-b286-4487-bf83-44b3161b889d  bridge    docker0[root@vm2 ~]#

如果對enp0s8做了修改,可以使用up命令啟用

[root@vm2 ~]# nmcli connection up enp0s8Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/6)[root@vm2 ~]#

顯示enp0s8的詳細資訊,第一列是屬性值,以後可以透過修改屬性對網口做調整

[root@vm2 ~]# nmcli connection show enp0s8connection.id:                          enp0s8connection.uuid:                        154c1fcb-94cc-3a90-8b7e-11341c4bfc28connection.stable-id:                   --connection.type:                        802-3-ethernetconnection.interface-name:              enp0s8connection.autoconnect:                 yesconnection.autoconnect-priority:        0connection.autoconnect-retries:         -1 (default)connection.multi-connect:               0 (default)connection.auth-retries:                -1connection.timestamp:                   1610441554connection.read-only:                   noconnection.permissions:                 user:rootconnection.zone:                        --connection.master:                      --connection.slave-type:                  --connection.autoconnect-slaves:          -1 (default)connection.secondaries:                 --connection.gateway-ping-timeout:        0connection.metered:                     unknownconnection.lldp:                        defaultconnection.mdns:                        -1 (default)connection.llmnr:                       -1 (default)connection.wait-device-timeout:         -1802-3-ethernet.port:                    --802-3-ethernet.speed:                   0802-3-ethernet.duplex:                  --802-3-ethernet.auto-negotiate:          no802-3-ethernet.mac-address:             --802-3-ethernet.cloned-mac-address:      --802-3-ethernet.generate-mac-address-mask:--802-3-ethernet.mac-address-blacklist:   --802-3-ethernet.mtu:                     auto802-3-ethernet.s390-subchannels:        --802-3-ethernet.s390-nettype:            --802-3-ethernet.s390-options:            --802-3-ethernet.wake-on-lan:             default802-3-ethernet.wake-on-lan-password:    --ipv4.method:                            manualipv4.dns:                               --ipv4.dns-search:                        --ipv4.dns-options:                       --ipv4.dns-priority:                      0ipv4.addresses:                         192.168.56.102/24ipv4.gateway:                           192.168.56.1ipv4.routes:                            --ipv4.route-metric:                      -1ipv4.route-table:                       0 (unspec)ipv4.routing-rules:                     --ipv4.ignore-auto-routes:                noipv4.ignore-auto-dns:                   noipv4.dhcp-client-id:                    --ipv4.dhcp-iaid:                         --ipv4.dhcp-timeout:                      0 (default)ipv4.dhcp-send-hostname:                yesipv4.dhcp-hostname:                     --ipv4.dhcp-fqdn:                         --ipv4.dhcp-hostname-flags:               0x0 (none)ipv4.never-default:                     noipv4.may-fail:                          noipv4.dad-timeout:                       -1 (default)ipv6.method:                            autoipv6.dns:                               --ipv6.dns-search:                        --ipv6.dns-options:                       --ipv6.dns-priority:                      0ipv6.addresses:                         --ipv6.gateway:                           --ipv6.routes:                            --ipv6.route-metric:                      -1ipv6.route-table:                       0 (unspec)ipv6.routing-rules:                     --ipv6.ignore-auto-routes:                noipv6.ignore-auto-dns:                   noipv6.never-default:                     noipv6.may-fail:                          yesipv6.ip6-privacy:                       -1 (unknown)ipv6.addr-gen-mode:                     stable-privacyipv6.ra-timeout:                        0 (default)ipv6.dhcp-duid:                         --ipv6.dhcp-iaid:                         --ipv6.dhcp-timeout:                      0 (default)ipv6.dhcp-send-hostname:                yesipv6.dhcp-hostname:                     --ipv6.dhcp-hostname-flags:               0x0 (none)ipv6.token:                             --proxy.method:                           noneproxy.browser-only:                     noproxy.pac-url:                          --proxy.pac-script:                       --GENERAL.NAME:                           enp0s8GENERAL.UUID:                           154c1fcb-94cc-3a90-8b7e-11341c4bfc28GENERAL.DEVICES:                        enp0s8GENERAL.IP-IFACE:                       enp0s8GENERAL.STATE:                          activatedGENERAL.DEFAULT:                        yesGENERAL.DEFAULT6:                       noGENERAL.SPEC-OBJECT:                    --GENERAL.VPN:                            noGENERAL.DBUS-PATH:                      /org/freedesktop/NetworkManager/ActiveConnection/3GENERAL.CON-PATH:                       /org/freedesktop/NetworkManager/Settings/1GENERAL.ZONE:                           --GENERAL.MASTER-PATH:                    --IP4.ADDRESS[1]:                         192.168.56.102/24IP4.GATEWAY:                            192.168.56.1IP4.ROUTE[1]:                           dst = 192.168.56.0/24, nh = 0.0.0.0, mt = 101IP4.ROUTE[2]:                           dst = 0.0.0.0/0, nh = 192.168.56.1, mt = 101IP6.ADDRESS[1]:                         fe80::e9db:1baf:3520:5d63/64IP6.GATEWAY:                            --IP6.ROUTE[1]:                           dst = fe80::/64, nh = ::, mt = 101IP6.ROUTE[2]:                           dst = ff00::/8, nh = ::, mt = 256, table=255[root@vm2 ~]#

nmcli 的互動式編輯器

儘管 nmcli 是一個命令列工具,但它還包含一個基本的互動式編輯器,edit 子命令將為你指定的連線開啟一個互動式編輯器,例如

$ nmcli connection edit ethernet-enp0s8

它將顯示少量的幫助文字,接著是 nmcli 的命令提示符:

[root@vm2 ~]# nmcli connection edit enp0s8 ===| nmcli interactive connection editor |=== Editing existing '802-3-ethernet' connection: 'enp0s8' Type 'help' or '?' for available commands.Type 'print' to show all the connection properties.Type 'describe [<setting>.<prop>]' for detailed property description. You may edit the following settings: connection, 802-3-ethernet (ethernet), 802-1x, dcb, sriov, ethtool, match, ipv4, ipv6, tc, proxynmcli>nmcli> help---------------------------------------------------------------------------------[ Main menu ]---goto     [<setting> | <prop>]        :: go to a setting or propertyremove   <setting>[.<prop>] | <prop> :: remove setting or reset property valueset      [<setting>.<prop> <value>]  :: set property valuedescribe [<setting>.<prop>]          :: describe propertyprint    [all | <setting>[.<prop>]]  :: print the connectionverify   [all | fix]                 :: verify the connectionsave     [persistent|temporary]      :: save the connectionactivate [<ifname>] [/<ap>|<nsp>]    :: activate the connectionback                                 :: go one level up (back)help/?   [<command>]                 :: print this helpnmcli    <conf-option> <value>       :: nmcli configurationquit                                 :: exit nmcli------------------------------------------------------------------------------nmcli> print===============================================================================                      Connection profile details (enp0s8)===============================================================================connection.id:                          enp0s8connection.uuid:                        154c1fcb-94cc-3a90-8b7e-11341c4bfc28connection.stable-id:                   --connection.type:                        802-3-ethernetconnection.interface-name:              enp0s8connection.autoconnect:                 yesconnection.autoconnect-priority:        0connection.autoconnect-retries:         -1 (default)connection.multi-connect:               0 (default)connection.auth-retries:                -1connection.timestamp:                   1610441854connection.read-only:                   noconnection.permissions:                 user:rootconnection.zone:                        --connection.master:                      --connection.slave-type:                  --connection.autoconnect-slaves:          -1 (default)connection.secondaries:                 --connection.gateway-ping-timeout:        0connection.metered:                     unknownconnection.lldp:                        defaultconnection.mdns:                        -1 (default)connection.llmnr:                       -1 (default)connection.wait-device-timeout:         -1-------------------------------------------------------------------------------802-3-ethernet.port:                    --802-3-ethernet.speed:                   0802-3-ethernet.duplex:                  --802-3-ethernet.auto-negotiate:          no802-3-ethernet.mac-address:             --802-3-ethernet.cloned-mac-address:      --802-3-ethernet.generate-mac-address-mask:--802-3-ethernet.mac-address-blacklist:   --802-3-ethernet.mtu:                     auto802-3-ethernet.s390-subchannels:        --802-3-ethernet.s390-nettype:            --802-3-ethernet.s390-options:            --802-3-ethernet.wake-on-lan:             default802-3-ethernet.wake-on-lan-password:    ---------------------------------------------------------------------------------ipv4.method:                            manualipv4.dns:                               --ipv4.dns-search:                        --ipv4.dns-options:                       --ipv4.dns-priority:                      0ipv4.addresses:                         192.168.56.102/24ipv4.gateway:                           192.168.56.1ipv4.routes:                            --ipv4.route-metric:                      -1ipv4.route-table:                       0 (unspec)ipv4.routing-rules:                     --ipv4.ignore-auto-routes:                noipv4.ignore-auto-dns:                   noipv4.dhcp-client-id:                    --ipv4.dhcp-iaid:                         --ipv4.dhcp-timeout:                      0 (default)ipv4.dhcp-send-hostname:                yesipv4.dhcp-hostname:                     --ipv4.dhcp-fqdn:                         --ipv4.dhcp-hostname-flags:               0x0 (none)ipv4.never-default:                     noipv4.may-fail:                          noipv4.dad-timeout:                       -1 (default)-------------------------------------------------------------------------------ipv6.method:                            autoipv6.dns:                               --ipv6.dns-search:                        --ipv6.dns-options:                       --ipv6.dns-priority:                      0ipv6.addresses:                         --ipv6.gateway:                           --ipv6.routes:                            --ipv6.route-metric:                      -1ipv6.route-table:                       0 (unspec)ipv6.routing-rules:                     --ipv6.ignore-auto-routes:                noipv6.ignore-auto-dns:                   noipv6.never-default:                     noipv6.may-fail:                          yesipv6.ip6-privacy:                       -1 (unknown)ipv6.addr-gen-mode:                     stable-privacyipv6.ra-timeout:                        0 (default)ipv6.dhcp-duid:                         --ipv6.dhcp-iaid:                         --ipv6.dhcp-timeout:                      0 (default)ipv6.dhcp-send-hostname:                yesipv6.dhcp-hostname:                     --ipv6.dhcp-hostname-flags:               0x0 (none)ipv6.token:                             ---------------------------------------------------------------------------------proxy.method:                           noneproxy.browser-only:                     noproxy.pac-url:                          --proxy.pac-script:                       ---------------------------------------------------------------------------------nmcli>

修改ipv4相關的,輸入 goto ipv4就可以,其實就是輸入print輸出裡面的 . 符號前面值

nmcli> goto ipv4You may edit the following properties: method, dns, dns-search, dns-options, dns-priority, addresses, gateway, routes, route-metric, route-table, routing-rules, 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, dad-timeoutnmcli ipv4> help---------------------------------------------------------------------------------[ Main menu ]---goto     [<setting> | <prop>]        :: go to a setting or propertyremove   <setting>[.<prop>] | <prop> :: remove setting or reset property valueset      [<setting>.<prop> <value>]  :: set property valuedescribe [<setting>.<prop>]          :: describe propertyprint    [all | <setting>[.<prop>]]  :: print the connectionverify   [all | fix]                 :: verify the connectionsave     [persistent|temporary]      :: save the connectionactivate [<ifname>] [/<ap>|<nsp>]    :: activate the connectionback                                 :: go one level up (back)help/?   [<command>]                 :: print this helpnmcli    <conf-option> <value>       :: nmcli configurationquit                                 :: exit nmcli------------------------------------------------------------------------------nmcli ipv4>

你想改變的屬性是 method,再繼續敲 set method auto 然後敲 Enter 鍵:

	方法一:nmcli ipv4> set methodAllowed values for 'method' property: auto, link-local, manual, shared, disabledEnter 'method' value: autoDo you also want to clear 'ipv4.addresses'? [yes]: no 方法二:nmcli ipv4> set method autonmcli ipv4>

最後輸入 save 來儲存你的更改:

nmcli ipv4> saveConnection 'ethernet-enp0s8' (09d26960-25a0-440f-8b20-c684d7adc2f5) successfully updated.nmcli ipv4>

10
最新評論
  • BSA-TRITC(10mg/ml) TRITC-BSA 牛血清白蛋白改性標記羅丹明
  • 為什麼Python的f-string可以連線字串與數字?