Last active
January 12, 2022 05:30
-
-
Save mykysyk/1e6a59a6dc72ecc583f2fe74298e9a92 to your computer and use it in GitHub Desktop.
[systemd-nspawn] Debian11 コンテナ作成 (Debian 11)
This file contains hidden or 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
# systemd-containerパケージをインストール | |
apt install systemd-container | |
# パッケージマネージャーのインストール | |
apt install dnf debootstrap | |
# コンテナ名 | |
CONTAINER_NAME=debian11 | |
# Debian 11 bullseye インストール | |
debootstrap \ | |
--variant=minbase \ | |
--include=systemd,dbus,locales,sudo,vim \ | |
--arch amd64 \ | |
bullseye \ | |
/var/lib/machines/${CONTAINER_NAME} \ | |
https://deb.debian.org/debian/ | |
# コンテナのホスト名変更 | |
echo ${CONTAINER_NAME} > /var/lib/machines/${CONTAINER_NAME}/etc/hostname | |
# hostsファイル作成 | |
echo "127.0.0.1 localhost ${CONTAINER_NAME}" > /var/lib/machines/${CONTAINER_NAME}/etc/hosts | |
# nspawnディレクトr作成 | |
mkdir -p /etc/systemd/nspawn/ | |
# ネットワークをプライベートからホストネットワークに変更 | |
cat<<EOF>/etc/systemd/nspawn/${CONTAINER_NAME}.nspawn | |
[Network] | |
VirtualEthernet=no | |
EOF | |
# コンテナを起動 | |
systemd-nspawn -D /var/lib/machines/${CONTAINER_NAME} | |
# ユーザ作成 | |
useradd -m your-name -s /bin/bash | |
# パスワード設定 | |
passwd your-name | |
# sudo権限付与 | |
gpasswd -a your-name sudo | |
# コンテナから一旦ぬける | |
exit | |
# コンテナをブート | |
systemd-nspawn -bD /var/lib/machines/${CONTAINER_NAME}/ | |
# resolv.confを静的ファイルに変更 | |
sudo unlink /etc/resolv.conf | |
echo 'nameserver 8.8.8.8 8.8.4.4' | sudo tee /etc/resolv.conf | |
# systemd-resolved停止 | |
sudo systemctl stop systemd-resolved | |
sudo systemctl disable systemd-resolved | |
# 外部につながるか確認 | |
sudo apt update | |
# locale を UTF-8 にする | |
sudo localectl set-locale LANG=ja_JP.UTF8 | |
# 「Ctrl+]」 x 3 で終了 | |
# コンテナ起動 | |
machinectl start ${CONTAINER_NAME} | |
# コンテナの起動状態確認 | |
machinectl status ${CONTAINER_NAME} | |
# コンテナへログイン | |
machinectl login ${CONTAINER_NAME} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment