Up 固定IPアドレスを設定 作成: 2021-02-21
更新: 2021-02-21


    PC から同じ IPアドレスで ssh接続ができるよう,RasPi の IPアドレスを固定する。

    1. ネットワークインタフェースのいまの状態を見る:
      $ ip a
      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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether b8:27:eb:49:40:35 brd ff:ff:ff:ff:ff:ff inet 192.168.1.12/24 brd 192.168.1.255 scope global dynamic eth0 valid_lft 12570sec preferred_lft 12570sec inet6 2400:4060:e62:a700:ba27:ebff:fe49:4035/64 scope global dynamic mngtmpaddr noprefixroute valid_lft 14007sec preferred_lft 14007sec inet6 fe80::ba27:ebff:fe49:4035/64 scope link valid_lft forever preferred_lft forever 3: wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether b8:27:eb:1c:15:60 brd ff:ff:ff:ff:ff:ff

    2. 固定IPアドレスを設定する:
      /etc/netplan/50-cloud-init.yaml を編集する
      $ sudo vi /etc/netplan/50-cloud-init.yaml
        network: ethernets: eth0: addresses: - 192.168.1.150/24 dhcp4: true gateway4: 192.168.1.1 nameservers: addresses: - 192.168.1.1 - 8.8.8.8 search: [] optional: true renderer: networkd version: 2
    • 「gateway4」の「4」は, 「IPv4」の「4」
    • 「8.8.8.8」は, 「Google Public DNS」のIPアドレス





    3. 新規設定を反映する:
      $ sudo netplan --debug apply

    4. 結果を見る:
      $ ip a
      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: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether b8:27:eb:49:40:35 brd ff:ff:ff:ff:ff:ff inet 192.168.1.150/24 brd 192.168.1.255 scope global eth0 valid_lft forever preferred_lft forever inet 192.168.1.12/24 brd 192.168.1.255 scope global secondary dynamic eth0 valid_lft 14393sec preferred_lft 14393sec inet6 2400:4060:e62:a700:ba27:ebff:fe49:4035/64 scope global dynamic mngtmpaddr noprefixroute valid_lft 14393sec preferred_lft 14393sec inet6 fe80::ba27:ebff:fe49:4035/64 scope link valid_lft forever preferred_lft forever 3: wlan0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether b8:27:eb:1c:15:60 brd ff:ff:ff:ff:ff:ff

    5. 192.168.1.12/24 を削除する
      $ sudo ip addr del 192.168.1.12/24 dev eth0

      しかし,poweroff で復活する──問題保留


    6. IPアドレス 192.168.1.150 で ssh 接続テスト
      $ ssh ubuntu@192.168.1.150
      @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ @ 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 8a:24:24:c5:e7:3d:59:c9:ed:47:05:f2:f9:68:a3:fc. Please contact your system administrator. Add correct host key in /Users/m/.ssh/known_hosts to get rid of this message. Offending RSA key in /Users/m/.ssh/known_hosts:2 RSA host key for 192.168.1.150 has changed and you have requested strict checking. Host key verification failed.
      /Users/[ユーザ名]/.ssh/known_hosts ファイルを空にする:
      ~ $ \cp /dev/null ./.ssh/known_hosts

      改めて ssh接続
      $ ssh ubuntu@192.168.1.150

      接続される。