軽く試験したいだけの端末で一々GUIで設定するのがダルくなってきたので、ヘッドレスなUbuntu Serverを導入してみました。
実際のところ
Raspberry Pi Imagerで書き込む
執筆時点でRaspberry Pi ImagerにはUbuntu Server 20系が入ってます。
WiFIの接続先を設定。
今回はWiFi越しにSSHしたいという欲があったので、その設定を。
書き込んだSDカードを読み出し、network-configという名前のファイルを探し、以下の様に記述。
固定IPにしても良さそうな気がしないでもないのですが、安全のためDHCPをtrueに。
# This file contains a netplan-compatible configuration which cloud-init
# will apply on first-boot. Please refer to the cloud-init documentation and
# the netplan reference for full details:
#
# https://cloudinit.readthedocs.io/
# https://netplan.io/reference
#
# Some additional examples are commented out below
version: 2
ethernets:
eth0:
dhcp4: true
optional: true
wifis:
wlan0:
dhcp4: true
optional: true
access-points:
YOURAP:
password: "YOURPASS"
割り振られたIPアドレスを探す
前の手順でDHCPを使った場合は、割り振られたIPアドレスを調べる必要があります。
まず繋いでない状態で同一サブネット下のアドレス群を出力するワンライナーを実行。
$ echo 192.168.3.{1..254} | xargs -P256 -n1 ping -s1 -c1 -W1 | grep ttl | cut -d" " -f 4 | tr -d
:
192.168.3.1
192.168.3.4で、つないだ状態で同じコマンドをたたくと
$ echo 192.168.3.{1..254} | xargs -P256 -n1 ping -s1 -c1 -W1 | grep ttl | cut -d" " -f 4 | tr -d :
192.168.3.1
192.168.3.4
192.168.3.8192.168.3.8が今回のお目当てである事が分かります。
初期パスワードの再設定
執筆時点ではユーザー名パスワード共にubuntu。
$ ssh ubuntu@192.168.3.8 ~$ ssh ubuntu@192.168.3.8 The authenticity of host '192.168.3.8 (192.168.3.8)' can't be established. ECDSA key fingerprint is SHA256:bDY9ac45JTUy2nYVuibhSBjQL9/6TuGLtq066GONW70. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added '192.168.3.8' (ECDSA) to the list of known hosts. ubuntu@192.168.3.8's password: You are required to change your password immediately (administrator enforced) Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.4.0-1028-raspi armv7l) ...
パスワードを変えろと脅されるので、適切に変更
WARNING: Your password has expired. You must change your password now and login again! Changing password for ubuntu. Current password: New password: Retype new password: passwd: password updated successfully Connection to 192.168.3.8 closed.
再度sshで接続
$ ssh ubuntu@192.168.3.8 ubuntu@192.168.3.8's password: Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.4.0-1028-raspi armv7l) ... To run a command as administrator (user "root"), use "sudo <command>". See "man sudo_root" for details. ubuntu@ubuntu:~$
接続情報(netplan)を弄る
先ほど書いたモンをベースに、以下のようなファイルが自動生成されておりました。
~$ cat /etc/netplan/50-cloud-init.yaml
# This file is generated from information provided by the datasource. Changes
# to it will not persist across an instance reboot. To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
ethernets:
eth0:
dhcp4: true
optional: true
version: 2
wifis:
wlan0:
access-points:
YOURAP:
password: 'YOURPASS'
dhcp4: true
optional: trueファイル冒頭のメモ書きと過去の自分のメモを参考に、自分のファイルを"/etc/netplan/99-my-network-config.yaml"として
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: false
dhcp6: false
addresses: [192.168.123.100/24]
gateway4: 192.168.123.1
nameservers:
addresses: [192.168.123.1, 8.8.8.8, 8.8.4.4]
wifis:
wlan0:
access-points:
YOURAP:
password: 'YOURPASS'
dhcp4: true
optional: true自動生成ファイルのご指示通り、元あったやつの読み込みは停止させる。
$ cat /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
network: {config: disabled}一応再起動
sudo reboot