LoginSignup
32
48

More than 5 years have passed since last update.

nmcliの使い方メモ

Last updated at Posted at 2016-12-29

CentOS7 からは nmcli というツールでネットワークを管理するらしいので、自宅の仮想サーバ(CentOS7 が無かったので Fedora)で使ってみました。

nmcliNetworkManager に含まれるツールの一つです。nmtui というツールもあるようですが、この記事では触れません。

以前までの CentOS では NIC デバイスとその設定は 1 対 1 でしたが、NetworkManager では複数の設定(接続プロファイル)を予め用意しておき、必要に応じて何れかの設定をデバイスに適用する方式になっているようです1

以下では eth0 という接続プロファイルを新たに作成し、その接続プロファイルをデバイス enp0s3 に適用する例です。
接続プロファイルは、これまでと同様 /etc/sysconfig/network-scripts/ の配下に保存されます。

$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    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:ab:65:58 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.130/24 brd 192.168.0.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 2001:a450:4b:b300:15e:7fa9:9f21:6aaf/64 scope global noprefixroute dynamic
       valid_lft 2591964sec preferred_lft 604764sec
    inet6 fe80::26d5:22a5:8b7c:8ec2/64 scope link
       valid_lft forever preferred_lft forever
$
$ su -
パスワード:
#
# id
uid=0(root) gid=0(root) groups=0(root) context=unconfined_u:unconfined_r:unconfined_t:s0-s0:c0.c1023
#
# uname -a
Linux fedora25 4.8.14-300.fc25.x86_64 #1 SMP Mon Dec 12 16:31:04 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
#
# rpm -q NetworkManager
NetworkManager-1.4.2-2.fc25.x86_64
#
# nmcli dev
デバイス  タイプ    状態      接続
enp0s3    ethernet  接続済み  enp0s3
lo        loopback  管理無し  --
#
# nmcli con
名前    UUID                                  タイプ          デバイス
enp0s3  a85a0edf-2fec-30d0-a01b-ed6c9544f9d6  802-3-ethernet  enp0s3
#
# nmcli con add type eth ifname '*' con-name eth0
接続 'eth0' (04c1f188-2c6e-4159-b530-f2c465c1128c) が正常に追加されました。
#
# nmcli con
名前    UUID                                  タイプ          デバイス
enp0s3  a85a0edf-2fec-30d0-a01b-ed6c9544f9d6  802-3-ethernet  enp0s3
eth0    04c1f188-2c6e-4159-b530-f2c465c1128c  802-3-ethernet  --
#
# nmcli con show eth0
connection.id:                          eth0
connection.uuid:                        04c1f188-2c6e-4159-b530-f2c465c1128c
connection.stable-id:                   --
connection.interface-name:              --
connection.type:                        802-3-ethernet
connection.autoconnect:                 yes
connection.autoconnect-priority:        0
connection.timestamp:                   0
connection.read-only:                   no
connection.permissions:
connection.zone:                        --
connection.master:                      --
connection.slave-type:                  --
connection.autoconnect-slaves:          -1 (default)
connection.secondaries:
connection.gateway-ping-timeout:        0
connection.metered:                     不明
connection.lldp:                        -1 (default)
802-3-ethernet.port:                    --
802-3-ethernet.speed:                   0
802-3-ethernet.duplex:                  --
802-3-ethernet.auto-negotiate:          yes
802-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:                     自動
802-3-ethernet.s390-subchannels:
802-3-ethernet.s390-nettype:            --
802-3-ethernet.s390-options:
802-3-ethernet.wake-on-lan:             1 (default)
802-3-ethernet.wake-on-lan-password:    --
ipv4.method:                            auto
ipv4.dns:
ipv4.dns-search:
ipv4.dns-options:                       (デフォルト)
ipv4.dns-priority:                      0
ipv4.addresses:
ipv4.gateway:                           --
ipv4.routes:
ipv4.route-metric:                      -1
ipv4.ignore-auto-routes:                no
ipv4.ignore-auto-dns:                   no
ipv4.dhcp-client-id:                    --
ipv4.dhcp-timeout:                      0
ipv4.dhcp-send-hostname:                yes
ipv4.dhcp-hostname:                     --
ipv4.dhcp-fqdn:                         --
ipv4.never-default:                     no
ipv4.may-fail:                          yes
ipv4.dad-timeout:                       -1 (default)
ipv6.method:                            auto
ipv6.dns:
ipv6.dns-search:
ipv6.dns-options:                       (デフォルト)
ipv6.dns-priority:                      0
ipv6.addresses:
ipv6.gateway:                           --
ipv6.routes:
ipv6.route-metric:                      -1
ipv6.ignore-auto-routes:                no
ipv6.ignore-auto-dns:                   no
ipv6.never-default:                     no
ipv6.may-fail:                          yes
ipv6.ip6-privacy:                       -1 (不明)
ipv6.addr-gen-mode:                     stable-privacy
ipv6.dhcp-send-hostname:                yes
ipv6.dhcp-hostname:                     --
ipv6.token:                             --
#
# nmcli con mod eth0 connection.autoconnect yes
# nmcli con mod eth0 ipv4.method manual
エラー: 接続 'eth0' の変更に失敗しました: ipv4.addresses: 'method=manual' のこのプロパティーを空にすることはできません
# nmcli con mod eth0 ipv4.addresses 192.168.0.78/24
# nmcli con mod eth0 ipv4.method manual
# nmcli con mod eth0 ipv4.gateway 192.168.0.1
# nmcli con mod eth0 ipv4.dns 192.168.0.1
#
# nmcli con show eth0
connection.id:                          eth0
connection.uuid:                        04c1f188-2c6e-4159-b530-f2c465c1128c
connection.stable-id:                   --
connection.interface-name:              --
connection.type:                        802-3-ethernet
connection.autoconnect:                 yes
connection.autoconnect-priority:        0
connection.timestamp:                   0
connection.read-only:                   no
connection.permissions:
connection.zone:                        --
connection.master:                      --
connection.slave-type:                  --
connection.autoconnect-slaves:          -1 (default)
connection.secondaries:
connection.gateway-ping-timeout:        0
connection.metered:                     不明
connection.lldp:                        -1 (default)
802-3-ethernet.port:                    --
802-3-ethernet.speed:                   0
802-3-ethernet.duplex:                  --
802-3-ethernet.auto-negotiate:          yes
802-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:                     自動
802-3-ethernet.s390-subchannels:
802-3-ethernet.s390-nettype:            --
802-3-ethernet.s390-options:
802-3-ethernet.wake-on-lan:             1 (default)
802-3-ethernet.wake-on-lan-password:    --
ipv4.method:                            manual
ipv4.dns:                               192.168.0.1
ipv4.dns-search:
ipv4.dns-options:                       (デフォルト)
ipv4.dns-priority:                      0
ipv4.addresses:                         192.168.0.78/24
ipv4.gateway:                           192.168.0.1
ipv4.routes:
ipv4.route-metric:                      -1
ipv4.ignore-auto-routes:                no
ipv4.ignore-auto-dns:                   no
ipv4.dhcp-client-id:                    --
ipv4.dhcp-timeout:                      0
ipv4.dhcp-send-hostname:                yes
ipv4.dhcp-hostname:                     --
ipv4.dhcp-fqdn:                         --
ipv4.never-default:                     no
ipv4.may-fail:                          yes
ipv4.dad-timeout:                       -1 (default)
ipv6.method:                            auto
ipv6.dns:
ipv6.dns-search:
ipv6.dns-options:                       (デフォルト)
ipv6.dns-priority:                      0
ipv6.addresses:
ipv6.gateway:                           --
ipv6.routes:
ipv6.route-metric:                      -1
ipv6.ignore-auto-routes:                no
ipv6.ignore-auto-dns:                   no
ipv6.never-default:                     no
ipv6.may-fail:                          yes
ipv6.ip6-privacy:                       -1 (不明)
ipv6.addr-gen-mode:                     stable-privacy
ipv6.dhcp-send-hostname:                yes
ipv6.dhcp-hostname:                     --
ipv6.token:                             --
#
# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=eth0
UUID=04c1f188-2c6e-4159-b530-f2c465c1128c
ONBOOT=yes
IPADDR=192.168.0.78
PREFIX=24
GATEWAY=192.168.0.1
DNS1=192.168.0.1
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
#
# nmcli con down enp0s3 && nmcli con up eth0 ifname enp0s3

接続プロファイルの適用には、ネットワークの切断(再起動)が必要です。
SSH が切断されたら再接続します。

$ nmcli con
名前    UUID                                  タイプ          デバイス
eth0    04c1f188-2c6e-4159-b530-f2c465c1128c  802-3-ethernet  enp0s3
enp0s3  a85a0edf-2fec-30d0-a01b-ed6c9544f9d6  802-3-ethernet  --
$ 
$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1
    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:ab:65:58 brd ff:ff:ff:ff:ff:ff
    inet 192.168.0.78/24 brd 192.168.0.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 2001:a450:4b:b300:8633:4f:6ae2:e32f/64 scope global tentative noprefixroute dynamic
       valid_lft 2591998sec preferred_lft 604798sec
    inet6 fe80::d10a:4b3c:60d8:ec22/64 scope link
       valid_lft forever preferred_lft forever
$ 

接続プロファイル eth0 がデバイス enp0s3 に適用されたことが確認できました。


  1. ファイアウォール(firewalld)も似たような感じになっています。どうもモバイル用途を意識しているそうです。つまりコンピュータの設置場所が頻繁に変更され、その度に設定を変更する必要が生じると。 

32
48
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
48