Dockerでsshでログインしようとしたら思ったよりハマったのでメモをしておく。
まずはDockerでdebianを起動する。
docker run --name ssh_sample -it -p 127.0.0.1:2022:2022 debian:stable
設定ファイルなど必要なものをインストールする
apt -y update apt install -y sudo ssh vim
vim /etc/ssh/sshd_config
- Port
- 2022
- PasswordAuthentication
- yes
- PermitRootLogin yes
- yes
編集が終わったらservice ssh restart
でsshを再起動する。
ローカルPCに戻って、鍵を作成する。 さらに、鍵をDockerの中へ送る。
ssh-keygen -f ssh_sample ssh-copy-id -p 2022 -i ~/.ssh/ssh_sample.pub root@localhost ssh-add -K ~/.ssh/ssh_sample
公開鍵認証にするために再度設定を変更する。
vim /etc/ssh/sshd_config
- PubkeyAuthentication
- yes
- PasswordAuthentication
- no
編集が終わったらservice ssh restart
でsshを再起動する。
これで公開鍵でログインすることができる
ssh -p 2022 root@localhost