Last active
February 8, 2024 03:39
-
-
Save mykysyk/09d1bd6db607f4fb4d26bdde348c4a4d to your computer and use it in GitHub Desktop.
[systemd-nspawn] ubuntu 20.04 LTS コンテナ作成 (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=ubuntu2004 | |
# Ubuntu 20.04 LTS focal インストール | |
debootstrap \ | |
--variant=minbase \ | |
--include=systemd,dbus,locales,language-pack-ja,sudo,vim \ | |
--arch amd64 \ | |
focal \ | |
/var/lib/machines/${CONTAINER_NAME} \ | |
http://jp.archive.ubuntu.com/ubuntu | |
# コンテナのホスト名変更 | |
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 | |
# sudo: setrlimit(RLIMIT_CORE): Operation not permitted 対策 | |
# https://github.com/sudo-project/sudo/issues/42 | |
echo "Set disable_coredump false" >> /etc/sudo.conf | |
# コンテナから一旦ぬける | |
exit | |
# コンテナをブート | |
systemd-nspawn -bD /var/lib/machines/${CONTAINER_NAME}/ | |
# securettyの設置 | |
sudo cp /usr/share/doc/util-linux/examples/securetty /etc/securetty | |
# 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