Last active
August 29, 2015 14:10
-
-
Save minimum2scp/f5ccc9b2a1646eae8216 to your computer and use it in GitHub Desktop.
Dockerでcentos:centos6イメージを起動して、docker execでopenssh-serverをインストール・起動し、sudoをインストール・設定して、sudo実行可能な一般ユーザーを作成して最後にSSHのコマンドを表示する
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
| #! /bin/sh | |
| : ${image=centos:centos6} | |
| : ${name=centos-test} | |
| : ${run_command=/sbin/init} | |
| : ${user=${USER}} | |
| : ${ssh_key=$HOME/.ssh/id_rsa.pub} | |
| set -x | |
| docker run -d --name ${name} ${image} ${run_command} | |
| docker exec -ti ${name} yum install -y openssh-server | |
| docker exec -ti ${name} service sshd start | |
| docker exec -ti ${name} yum install -y sudo | |
| docker exec -ti ${name} bash -c 'umask 226; cat > /etc/sudoers.d/local' << END_OF_SUDOERS | |
| %wheel ALL=(ALL) NOPASSWD: ALL | |
| END_OF_SUDOERS | |
| docker exec -ti ${name} adduser ${user} | |
| docker exec -ti ${name} usermod -G adm,wheel ${user} | |
| docker exec -ti ${name} sudo -u ${user} bash -c 'mkdir -m 700 ~/.ssh' | |
| docker exec -ti ${name} sudo -u ${user} bash -c 'umask 066; cat > ~/.ssh/authorized_keys' < ${ssh_key} | |
| docker exec -ti ${name} pkill cat | |
| docker exec -ti ${name} passwd ${user} | |
| ## or use chpasswd: | |
| ## bash -c 'echo "username:plainpassword" | chpasswd' | |
| ## bash -c 'echo "username:encryptedpassword" | chpasswd -e' | |
| ## openssl can create encryptedpassword by: | |
| ## echo "plainpassword" | openssl passwd -1 -stdin | |
| set +x | |
| ip=`docker inspect -f '{{.NetworkSettings.IPAddress}}' ${name}` | |
| echo "Now you can login to container by following command:" | |
| echo " ssh -o \"UserKnownHostsFile /dev/null\" -o \"StrictHostKeyChecking no\" -i ${ssh_key%.pub} ${user}@${ip}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment