itcore TOPTIPSaws_ssm_ssh.php  タグ:aws ec2 ssm ssh

AWS EC2 ssm経由のsshがエラーになる。 | itcore 2021年

デバッグモード mtu変更 sshdリスタート パーミッション GSSAPIAuthentication AuthorizedKeysCommand

デバッグモード

■デバッグモードを指定すると接続出来るときがある。
ssh xxx -vvv
■一度つながらると通常モードでもつながる時がある。
ssh xxx
■known_hostsに古い情報があるとつながらないかも
rm ~/.ssh/known_hosts

mtu変更

デバッグモードで以下でとまることがある。
debug3: send packet: type 20
debug1: SSH2_MSG_KEXINIT sent

EC2 インスタンスのネットワークの最大送信単位 (MTU)
同じ Wavelength Zone 内の VPC で、あるインスタンスから別のインスタンスへ送られるトラフィックの MTU は 1300 です。

クライアントとサーバのmtuをそれぞれ変更して見る。
sudo ifconfig eth0 mtu 1300

sshdリスタート

サーバにssmやシリアルコンソールなどでログインして、sshdをリスタートすると接続できるときがある。
service sshd restart

パーミッション

■パーミッション設定
chmod 700 ~/
chmod 700 ~/.ssh
chmod 600 ~/.ssh/*
sudo chmod 755 /etc/ssh
sudo chmod 644 /etc/ssh/*_key.pub
sudo chmod 600 /etc/ssh/*_key
sudo chmod 711 /var/empty/sshd

クライアント側 GSSAPIAuthentication

sudo vi /etc/ssh/ssh_config
GSSAPIAuthentication no

サーバ側 AuthorizedKeysCommand

■sshエラー
Bad packet length 168445304.
ssh_dispatch_run_fatal: Connection to UNKNOWN port 65535: message authentication code incorrect

■サーバ側エラー
/var/log/secure
error: AuthorizedKeysCommand /opt/aws/bin/eic_run_authorized_keys ec2-user SHA256:xxx failed, status 22

■サーバ側対処
sudo vi /etc/ssh/sshd_config
PubkeyAuthentication yes
#AuthorizedKeysCommand /opt/aws/bin/eic_run_authorized_keys %u %f
#AuthorizedKeysCommandUser ec2-instance-connect
UseDNS no

service sshd restart