0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

RaspberryPi 4BにUbuntu Serverをインストール

Last updated at Posted at 2023-10-31

この記事について

会社でラズパイUbuntu環境作る(ただ作る)機会が稀にあり、自分自身が使わない(忘れる)のでメモを作りました。

自分がよく使う

  • 日本語環境
  • Network(wlan0/eth0)
  • nftables
  • mqtt broker

の各設定をUbuntu 20.04と22.04で確認しました。

環境

Raspberry Pi: Raspberry Pi 4 Model B Rev 1.2
OS: Ubuntu Server 20.04.5 LTS(64bit) / Ubuntu Server 22.04.3 LTS(64bit)
microSDカード: 32GB
クライアント機: DesktopPC(Windows11 Pro)

構築手順

microSDカード作成

Raspberry Pi Imagerを使いOther General-purpose OS > Ubuntu > Ubuntu Server 20.04.5 LTS(64bit) / Ubuntu Server 22.04.3 LTS(64bit)で選択します。

初期起動時にはImagerで設定したwifiしかNICが使えないのでssh接続したければwifiの設定はMUSTです。

update & upgrade

$ sudo apt update && sudo apt upgrade -y

locale設定

$ sudo dpkg-reconfigure keyboard-configuration
$ sudo timedatectl set-timezone Asia/Tokyo
$ timedatectl
$ sudo apt -y install language-pack-ja-base language-pack-ja
$ sudo localectl set-locale LANG=ja_JP.UTF-8 LANGUAGE="ja_JP:ja"
$ source /etc/default/locale

Network設定

Network Managerを使います。

$ sudo apt  install network-manager -y
$ sudo systemctl enable NetworkManager

netplanでNetworkManagerの設定をします。「APのSSID」と「APのpassphrase 」は実際に使う環境の値に置き換えます。

$ sudo nano /etc/netplan/99-custom-network.yaml
$ sudo mv /etc/netplan/50-cloud-init.yaml /etc/netplan/50-cloud-init.yaml.bak

dhcpを使う場合

/etc/netplan/99-custom-network.yaml
network:
    version: 2
    wifis:
        wlan0:
            access-points:
                APのSSID:
                    password: APのpassphrase 
            dhcp4: true
            optional: true
    ethernets:
        eth0:
            dhcp4: true
            optional: true
    renderer: NetworkManager

static ipにしたい場合

/etc/netplan/99-custom-network.yaml
network:
    version: 2
    wifis:
        wlan0:
            access-points:
                APのSSID:
                    password: APのpassphrase 
            dhcp4: false
            addresses: [192.168.xx.xx/24]
            routes:
              - to: default
                via: 192.168.xx.1
                metric: 100
            nameservers:
                addresses: [192.168.xx.1]
            optional: true
    ethernets:
        eth0:
            dhcp4: false
            addresses: [192.168.yy.xx/24]
            routes:
              - to: default
                via: 192.168.yy.1
                metric: 100
            nameservers:
                addresses: [192.168.yy.1]
            optional: true
    renderer: NetworkManager

NetworkManagerの設定を適用します。

$ sudo netplan apply
$ sudo reboot

ipv6無効化

ipv4のみnftablesを設定して使いたいのでipv6を無効化します。

$ echo "net.ipv6.conf.all.disable_ipv6=1" | sudo tee -a /etc/sysctl.conf 
$ echo "net.ipv6.conf.default.disable_ipv6=1" | sudo tee -a /etc/sysctl.conf
$ echo "net.ipv6.conf.lo.disable_ipv6=1" | sudo tee -a /etc/sysctl.conf
$ sudo sysctl -p

ssh port番号変更

Port 61000を使うことを想定します。

$ sudo sed -i "s/#Port 22/Port 61000/g" /etc/ssh/sshd_config
$ sudo systemctl restart ssh

nftables設定

nftablesと言いつつiptables-persistentをインストールして設定します()
外部とのmqttsとloでのmqttを許可します。

インストール後に再起動してからset_fw.shを実行します。

$ echo iptables-persistent iptables-persistent/autosave_v4 boolean true | sudo debconf-set-selections
$ echo iptables-persistent iptables-persistent/autosave_v6 boolean false | sudo debconf-set-selections
$ sudo apt install -y iptables iptables-persistent
$ sudo reboot
set_fw.sh
#!/bin/bash

sudo cp /etc/iptables/rules.v4 /etc/iptables/rules.v4.bak
sudo rm /etc/iptables/rules.v4

sudo iptables -F
sudo iptables -X
sudo iptables -P INPUT DROP
sudo iptables -P FORWARD DROP
sudo iptables -P OUTPUT ACCEPT
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -d 127.0.0.0/8 ! -i lo -j REJECT --reject-with icmp-port-unreachable
sudo iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
sudo iptables -A INPUT -s 127.0.0.0/8 -p tcp -m tcp --dport 1883 -j ACCEPT
sudo iptables -A INPUT -s 192.168.xx.0/24 -p udp -m udp --dport 53 -j ACCEPT
sudo iptables -A INPUT -s 192.168.xx.1/32 -p udp -m udp --dport 123 -j ACCEPT
sudo iptables -A INPUT -s 192.168.xx.0/24 -p udp -m udp --dport 5353 -j ACCEPT
sudo iptables -A INPUT -p tcp -m tcp --dport 8883 -j ACCEPT
sudo iptables -A INPUT -s 192.168.xx.0/24 -p tcp -m state --state NEW -m tcp --dport 61000 --tcp-flags FIN,SYN,RST,ACK SYN -m hashlimit --hashlimit-upto 1/min --hashlimit-burst 10 --hashlimit-mode srcip --hashlimit-name t_sshd --hashlimit-htable-expire 120000 -j ACCEPT
sudo iptables -A INPUT -s 169.254.0.0/16 -p tcp -m state --state NEW -m tcp --dport 61000 --tcp-flags FIN,SYN,RST,ACK SYN -m hashlimit --hashlimit-upto 1/min --hashlimit-burst 10 --hashlimit-mode srcip --hashlimit-name t_sshd --hashlimit-htable-expire 120000 -j ACCEPT
sudo iptables -A INPUT -s 192.168.xx.0/24 -p icmp -m icmp --icmp-type 8 -j ACCEPT
sudo iptables -A INPUT -s 169.254.0.0/16 -p icmp -m icmp --icmp-type 8 -j ACCEPT
sudo iptables -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
sudo iptables -A INPUT -j REJECT --reject-with icmp-port-unreachable

sudo service netfilter-persistent reload
sudo service netfilter-persistent save
sudo sh -c "iptables-save >/etc/iptables.ipv4.filter"
sudo iptables -L

mqtt brokerインストール

Server OSなのでpip3のインストールが必要です。

$ sudo apt install -y python3-dev python3-pip 
$ python3 -m pip install --upgrade pip

mqtt関連をインストールします。

$ sudo apt install -y mosquitto mosquitto-clients
$ sudo apt install -y python3-rpi.gpio
$ sudo pip3 install paho-mqtt

参考文献

以下記事を参考にさせて頂きました。ありがとうございました。

0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?