Last active
March 18, 2021 18:35
-
-
Save miminashi/e74071713618f20b2a684ce710e90b0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/sh | |
# | |
# Copyright (c) 2021 Shiro Miminashi Nohara | |
# Released under the MIT license | |
# https://opensource.org/licenses/mit-license.php | |
# | |
# USAGE 使い方: | |
# - Armbianの初回起動後、 root/1234 でログインして、`pi`ユーザを作成する | |
# - 以下のコマンドを実行する | |
# $ curl -L https://gist.githubusercontent.com/miminashi/e74071713618f20b2a684ce710e90b0c/raw | sh -s HOSTNAME [RPFW_PORT] | |
# | |
# 引数の説明: | |
# HOSTNAME: 設定したいホスト名(必須) | |
# RPFW_PORT: リバースポートフォワードで用いるポート番号(オプショナル) | |
# | |
set -eux | |
hostname_new="${1:?"ホスト名を第1引数に指定してください"}" | |
rpfw_port="${2:-""}" # $1 が存在しなければ, 空文字列を設定する | |
# rootで実行されていなければ終了する | |
if [ "$(id -u)" != "0" ]; then | |
printf '\e[31mエラー: rootユーザで実行してください\e[m\n' 1>&2 | |
exit 1 | |
fi | |
# piユーザが存在しなければ終了する | |
if ! id pi; then | |
printf '\e[31mエラー: pi ユーザが存在しません\e[m\n' 1>&2 | |
exit 1 | |
fi | |
# ホスト名の設定 | |
#which armbian-config && armbian-config main=Personal selection=Hostname | |
hostname_current=$(cat /etc/hostname) | |
#printf 'ホスト名を入力してください: ' | |
#read -r hostname_new | |
if [ -n "${hostname_new}" ]; then | |
sed -i "s/${hostname_current}/${hostname_new}/g" /etc/hosts | |
sed -i "s/${hostname_current}/${hostname_new}/g" /etc/hostname | |
hostname "${hostname_new}" | |
systemctl restart systemd-logind.service | |
else | |
printf '\e[31mエラー: ホスト名が空です\e[m\n' 1>&2 | |
exit 1 | |
fi | |
# ssh | |
mkdir -p /home/pi/.ssh | |
curl https://github.com/nimu7.keys > /home/pi/.ssh/authorized_keys | |
curl https://github.com/miminashi.keys >> /home/pi/.ssh/authorized_keys | |
chmod 600 /home/pi/.ssh/authorized_keys | |
chown -R pi:pi /home/pi/.ssh | |
# update package list | |
apt-get -y update | |
# GNU screen | |
#sudo apt-get -y install screen | |
cat > /home/pi/.screenrc <<'EOF' | |
startup_message off | |
vbell off | |
caption always " %n %t $USER@%H" | |
termcapinfo xterm* ti@:te@ | |
term xterm-color | |
shell bash | |
EOF | |
chown pi:pi /home/pi/.screenrc | |
# avahi | |
# libnss-mymachinesはなくてもいいが, その場合は /etc/nssswitch.conf から mymachines をはずさないといけないと思う(未検証) | |
apt-get -y install avahi-daemon libnss-mdns libnss-mymachines | |
# POSIX compatibility | |
# POSIX互換性のための設定 | |
apt-get -y install pax ncompress | |
# other utility | |
# その他のユーティリティ | |
apt-get -y install vim | |
# Dockerのセットアップ | |
apt-get -y install \ | |
apt-transport-https \ | |
ca-certificates \ | |
curl \ | |
gnupg-agent \ | |
software-properties-common | |
curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add - | |
if [ "$(uname -m)" = "armv7l" ]; then | |
add-apt-repository "deb [arch=armhf] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | |
elif [ "$(uname -m)" = "aarch64" ]; then | |
add-apt-repository "deb [arch=arm64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | |
elif [ "$(uname -m)" = "x86_64" ]; then | |
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/debian $(lsb_release -cs) stable" | |
else | |
printf '\e[31mエラー: Dockerがサポートされていないアーキテクチャです\e[m\n' 1>&2 | |
exit 1 | |
fi | |
apt-get -y update | |
apt-get -y install docker-ce docker-ce-cli containerd.io | |
groupadd docker || : | |
usermod -aG docker pi | |
# sudoでパスワードを要求しないようにする | |
echo "pi ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/010_pi-nopasswd | |
# SSHリバースポートフォワードの設定 | |
if [ -n "${rpfw_port}" ]; then | |
tee /etc/systemd/system/ssh-rpfw.service <<EOF | |
[Unit] | |
Description=ssh reverse port forwarding service | |
After=network.target auditd.service | |
[Service] | |
User=pi | |
Group=pi | |
WorkingDirectory=/home/pi | |
ExecStart=/usr/bin/ssh -o ServerAliveInterval=5 -o ServerAliveCountMax=3 -o ExitOnForwardFailure=yes -o TCPKeepAlive=no -N -R ${rpfw_port}:127.0.0.1:22 -i /home/pi/.ssh/id_ed25519 -p 22 rpfw@l.or6.jp | |
Restart=always | |
RestartSec=1 | |
StartLimitBurst=0 | |
[Install] | |
WantedBy=multi-user.target | |
EOF | |
# サービスの有効化 | |
systemctl enable ssh-rpfw.service | |
# known_hosts ファイルにリバースフォワードサーバの公開鍵を登録する | |
printf 'l.or6.jp ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBPJFTcrFAHsI5DmBDALvizX6Uc+MUNl7JjfFg1T6WdkH8mkM6zTWzqJO2/JAH5iVJ3bgCu1HfoLdFk4BSxSgYRA=\n' > /etc/ssh/ssh_known_hosts | |
# ssh | |
if ! [ -f /home/pi/.ssh/id_ed25519 ]; then | |
ssh-keygen -t ed25519 -f /home/pi/.ssh/id_ed25519 -N '' -C "pi@${hostname_new}" | |
fi | |
chown pi /home/pi/.ssh/id_ed25519 | |
chgrp pi /home/pi/.ssh/id_ed25519 | |
chown pi /home/pi/.ssh/id_ed25519.pub | |
chgrp pi /home/pi/.ssh/id_ed25519.pub | |
set +x | |
printf '%s の公開鍵: %s\n' "$(hostname)" "$(cat /home/pi/.ssh/id_ed25519.pub)" | |
printf '~/.ssh/config に以下の設定を追加してください\n' | |
printf -- '----------------------------------------\n' | |
cat <<EOF | |
Host ${hostname_new} | |
HostName localhost | |
Port ${rpfw_port} | |
User pi | |
ProxyCommand ssh -W %h:%p -o ServerAliveInterval=3 -o ServerAliveCountMax=3 rpfw@l.or6.jp | |
#UserKnownHostsFile ~/.ssh/known_hosts.d/${hostname_new} # 使うときは \`mkdir -p ~/.ssh/known_hosts.d/\` してから使ってね | |
ServerAliveInterval 3 | |
ServerAliveCountMax 3 | |
Host ${hostname_new}.local | |
HostName ${hostname_new}.local | |
User pi | |
#UserKnownHostsFile ~/.ssh/known_hosts.d/${hostname_new} # 使うときは \`mkdir -p ~/.ssh/known_hosts.d/\` してから使ってね | |
ServerAliveInterval 3 | |
ServerAliveCountMax 3 | |
EOF | |
printf -- '----------------------------------------\n' | |
fi | |
printf 'セットアップが完了しました。再起動します...\n' | |
reboot |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment