CentOS7.2 64bit ネットワークの設定

CentOS7.2 64bit ネットワークの設定

当サイトではCentOS7に関する記事を公開していますが、管理人がその動作確認を行う際にはMacにインストールしたVirtualBoxに仮想マシンを作成して、そこにCentOS7をインストールして使用しています。

ここでは、インストールしたCentOS7.2 64bitのネットワークの設定について、以下に示します。

※VirtualBoxに作成している仮想マシンの内容については、当サイトのVirtualBoxにCentOS7 64bit用の仮想マシンを作成のページをご覧ください。

※CentOS7 64bitのインストールの手順については、当サイトのCentOS7 64bitをインストールのページをご覧ください。

VirtualBoxでブリッジアダプターを使用している場合のネットワークの設定

VirtualBoxのネットワークの設定でブリッジアダプターを使用するようにしている場合、ゲストOSはホストOSと同じネットワークに、同じネットワークインターフェイスを使用して接続するようになります。

※物理的なマシンにCentOSを直接インストールしている場合と、同様になります。

ネットワークインターフェイスのデフォルトの状態は、以下のようになっています。

# ip addr 1: lo: <,LOOPBACK,UP,LOWER_UP>, mtu 65536 qdisc noqueue state UNKNOWN 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 pfifo_fast state UP qlen 1000 link/ether xx:xx:xx:xx:xx:xx brd ff:ff:ff:ff:ff:ff

ネットワークインターフェイス「enp0s3」が「ブリッジアダプター」に割り当てられています。

※割り当てられるネットワークインターフェイスの名前は、環境によって異なります。

※「xx:xx:xx:xx:xx:xx」の部分に表示されているMACアドレスは、VirtualBoxのネットワークの設定で、アダプターの高度な設定を開いたときに確認できるMACアドレスになっています。

このネットワークインターフェイスの設定ファイル「/etc/sysconfig/network-scripts/ifcfg-enp0s3」は直接編集はせず、nmcliコマンドを使用して編集していきます。

※設定の詳細は、コマンド「man nm-settings」で確認することができます。

設定ファイル「ifcfg-enp0s3」のデフォルトの内容

設定ファイル「ifcfg-enp0s3」のデフォルトの内容は、以下のようになっています。

TYPE=Ethernet BOOTPROTO=dhcp DEFROUTE=yes PEERDNS=yes PEERROUTES=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_PEERDNS=yes IPV6_PEERROUTES=yes IPV6_FAILURE_FATAL=no NAME=enp0s3 UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx DEVICE=enp0s3 ONBOOT=no

ネットワークインターフェイス「enp0s3」の接続設定のデフォルトの内容

接続設定の内容は、以下のコマンドで確認することができます。

ネットワークインターフェイス「enp0s3」の接続設定のデフォルトの内容は、以下のようになっています。

# nmcli -p connection show enp0s3 =============================================================================== Connection profile details (enp0s3) =============================================================================== connection.id: enp0s3 connection.uuid: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx connection.interface-name: enp0s3 connection.type: 802-3-ethernet connection.autoconnect: no 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: unknown ------------------------------------------------------------------------------- 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.mac-address-blacklist: 802-3-ethernet.mtu: auto 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.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-send-hostname: yes ipv4.dhcp-hostname: -- ipv4.never-default: no ipv4.may-fail: yes ------------------------------------------------------------------------------- :(略) -------------------------------------------------------------------------------
  • nmcliコマンドで接続に関する操作を行う際には、引数に「connection」を指定します。

  • 接続設定の内容を確認する際には、引数の「connection」に続けて「show」と、確認するネットワークインターフェイスの名前(ここでは「enp0s3」)を指定します。

  • 「-p」オプションを指定すると、表示内容が見やすいように線で区切られて表示されます。

OSの起動時にネットワークの接続が自動で行われるようにする

OSの起動時にネットワークの接続が自動で行われるようにするには、以下のコマンドを実行します。

# nmcli connection modify enp0s3 connection.autoconnect yes
  • nmcliコマンドで接続に関する操作を行う際には、引数に「connection」を指定します。

  • 接続設定の編集を行う際には、引数の「connection」に続けて「modify」と、編集するネットワークインターフェイスの名前(ここでは「enp0s3」)を指定し、続けて編集するプロパティの名前と値を指定します。

  • 「connection.autoconnect」の値に「yes」と指定すると、OSの起動時にネットワークの接続が自動で行われるようになります。

このコマンドの実行により、設定ファイル「ifcfg-enp0s3」に「ONBOOT=yes」の設定が行われます。

固定のIPアドレスを使用するようにする

固定のIPアドレスを使用するようにするには、以下のコマンドを実行します。

# nmcli connection modify enp0s3 ipv4.method manual ipv4.addresses xxx.xxx.xxx.xxx/xx
  • nmcliコマンドで接続に関する操作を行う際には、引数に「connection」を指定します。

  • 接続設定の編集を行う際には、引数の「connection」に続けて「modify」と、編集するネットワークインターフェイスの名前(ここでは「enp0s3」)を指定し、続けて編集するプロパティの名前と値を指定します。

  • 「ipv4.method」の値に「manual」と指定すると、IPv4で固定のIPアドレスを使用するようになります。

    固定のIPアドレスを使用する場合は、「ipv4.addresses」へのIPv4のIPアドレスの指定も同時に行う必要があります。

    ※「xxx.xxx.xxx.xxx/xx」の「/」の前の「xxx.xxx.xxx.xxx」にIPアドレスを指定し、後の「xx」にサブネットマスクのビット長を指定します。

このコマンドの実行により、設定ファイル「ifcfg-enp0s3」に「BOOTPROTO=none」、「IPADDR=xxx.xxx.xxx.xxx」、「PREFIX=xx」の設定が行われます。

ゲートウェイのIPアドレスを設定する

ゲートウェイのIPアドレスを設定するには、以下のコマンドを実行します。

# nmcli connection modify enp0s3 ipv4.gateway xxx.xxx.xxx.xxx
  • nmcliコマンドで接続に関する操作を行う際には、引数に「connection」を指定します。

  • 接続設定の編集を行う際には、引数の「connection」に続けて「modify」と、編集するネットワークインターフェイスの名前(ここでは「enp0s3」)を指定し、続けて編集するプロパティの名前と値を指定します。

  • IPv4のゲートウェイのIPアドレスは、「ipv4.gateway」に指定します。

    ※「xxx.xxx.xxx.xxx」にゲートウェイのIPアドレスを指定します。

このコマンドの実行により、設定ファイル「ifcfg-enp0s3」に「GATEWAY=xxx.xxx.xxx.xxx」の設定が行われます。

DNSサーバーのIPアドレスを設定する

DNSサーバーのIPアドレスを設定するには、以下のコマンドを実行します。

# nmcli connection modify enp0s3 ipv4.dns xxx.xxx.xxx.xxx
  • nmcliコマンドで接続に関する操作を行う際には、引数に「connection」を指定します。

  • 接続設定の編集を行う際には、引数の「connection」に続けて「modify」と、編集するネットワークインターフェイスの名前(ここでは「enp0s3」)を指定し、続けて編集するプロパティの名前と値を指定します。

  • IPv4のDNSサーバーのIPアドレスは、「ipv4.dns」に指定します。

    ※「xxx.xxx.xxx.xxx」にDNSサーバーのIPアドレスを指定します。

このコマンドの実行により、設定ファイル「ifcfg-enp0s3」に「DNS1=xxx.xxx.xxx.xxx」の設定が行われ、ネットワークの起動時に「/etc/resolv.conf」に「nameserver xxx.xxx.xxx.xxx」の設定が行われるようになります。

ネットワークの設定を有効化する

ネットワークの設定を有効化するには、以下のコマンドを実行してネットワークサービスを再起動します。

# systemctl restart network.service

※CentOS7になってサービス起動関連のコマンドは、「service」から「systemctl」に変わりました。

ネットワークの設定後の設定ファイル「ifcfg-enp0s3」の内容

ネットワークの設定後の設定ファイル「ifcfg-enp0s3」の内容は、以下のようになっています。

TYPE=Ethernet BOOTPROTO=none DEFROUTE=yes IPV4_FAILURE_FATAL=no IPV6INIT=yes IPV6_AUTOCONF=yes IPV6_DEFROUTE=yes IPV6_FAILURE_FATAL=no NAME=enp0s3 UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx DEVICE=enp0s3 ONBOOT=yes IPADDR=xxx.xxx.xxx.xxx PREFIX=xx GATEWAY=xxx.xxx.xxx.xxx DNS1=xxx.xxx.xxx.xxx IPV6_PEERDNS=yes IPV6_PEERROUTES=yes

ネットワークの設定後のネットワークインターフェイス「enp0s3」の接続設定の内容

ネットワークの設定後のネットワークインターフェイス「enp0s3」の接続設定の内容は、以下のようになっています。

# nmcli -p connection show enp0s3 =============================================================================== Connection profile details (enp0s3) =============================================================================== connection.id: enp0s3 connection.uuid: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx connection.interface-name: enp0s3 connection.type: 802-3-ethernet connection.autoconnect: yes connection.autoconnect-priority: 0 connection.timestamp: xxxxxxxxxx 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: 不明 ------------------------------------------------------------------------------- 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.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: xxx.xxx.xxx.xxx ipv4.dns-search: ipv4.addresses: xxx.xxx.xxx.xxx/xx ipv4.gateway: xxx.xxx.xxx.xxx ipv4.routes: ipv4.route-metric: -1 ipv4.ignore-auto-routes: no ipv4.ignore-auto-dns: no ipv4.dhcp-client-id: -- ipv4.dhcp-send-hostname: yes ipv4.dhcp-hostname: -- ipv4.never-default: no ipv4.may-fail: yes ------------------------------------------------------------------------------- :(略) ------------------------------------------------------------------------------- =============================================================================== Activate connection details (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) =============================================================================== GENERAL.名前: enp0s3 GENERAL.UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx GENERAL.デバイス: enp0s3 GENERAL.状態: アクティベート済み GENERAL.デフォルト: はい GENERAL.デフォルト6: はい GENERAL.VPN: いいえ GENERAL.ゾーン: -- GENERAL.DBUS パス: /org/freedesktop/NetworkManager/ActiveConnection/1 GENERAL.CON パス: /org/freedesktop/NetworkManager/Settings/0 GENERAL.スペックオブジェクト: / GENERAL.マスターパス: -- ------------------------------------------------------------------------------- IP4.アドレス[1]: xxx.xxx.xxx.xxx/xx IP4.ゲートウェイ: xxx.xxx.xxx.xxx IP4.DNS[1]: xxx.xxx.xxx.xxx ------------------------------------------------------------------------------- :(略) -------------------------------------------------------------------------------

以前と同じIPアドレスで再インストールを行った際のSSH接続について

CentOSを以前に使用していたIPアドレスと同じものを使用して再インストール行うと、SSHで接続する際に以下のようなエラーになり接続できなくなります。

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @ @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY! Someone could be eavesdropping on you right now (man-in-the-middle attack)! It is also possible that a host key has just been changed. The fingerprint for the RSA key sent by the remote host is SHA256:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX. Please contact your system administrator. Add correct host key in /xxxxx/.ssh/known_hosts to get rid of this message. Offending RSA key in /xxxxx/.ssh/known_hosts:xx RSA host key for xxx.xxx.xxx.xxx has changed and you have requested strict checking. Host key verification failed.

SSHで接続する先のIPアドレスと公開鍵のデータが、ホームディレクトリ配下の「.ssh/known_hosts」ファイルに初回の接続時に保存されるのですが、CentOSの再インストールによりIPアドレスが同じでも公開鍵は異なるものになるので、「.ssh/known_hosts」ファイルに保存されているデータと異なり、接続できなくなります。

SSHでの接続を可能とするには、以下のコマンドを実行して、「.ssh/known_hosts」ファイルから古いIPアドレスのデータを削除します。

$ ssh-keygen -R xxx.xxx.xxx.xxx

※「xxx.xxx.xxx.xxx」に削除したいIPアドレスを指定します。

CentOS7.2 64bit ネットワークの設定については、以上です。

Linuxの基礎知識に関するおすすめの書籍

当サイトの書籍紹介ページで、Linuxの基礎知識に関するおすすめの書籍の紹介も行っておりますので、ご覧ください。