AWS Transfer for SFTP で接続に関するエラーメッセージと対処方法をまとめてみた

AWS Transfer for SFTP でエラーメッセージに対応するトラブルシューティングをまとめました
2021.12.01

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

AWSロゴ入りの1Uサーバが気になるアノテーション株式会社の hato です。 (個人でも買えたら良いな〜)

sftpコマンドで AWS Transfer for SFTP に繋がらない場合のトラブルシューティングをご案内します。

環境

  • OS:macOS Big Sur
  • ターミナル:MacOS標準

【全般】ログレベルを変更する

デフォルトでは原因が異なるエラーでも、同じエラーメッセージが表示される場合があるため、トラブルシューティングの際は-vオプションの設定を推奨します。

# コマンド例
sftp -v -i .ssh/sftp.pem hato@s-0123456789.server.transfer.ap-northeast-1.amazonaws.com

Permission denied

考えられる原因:ユーザ名もしくは秘密鍵が間違っている

このエラーは、ユーザ名もしくは指定した秘密鍵が誤っていることを意味します。

-vオプションを指定しても、クライアント側のログからはユーザ名と秘密鍵、どちらが原因か判別できないため、サーバ側のログ(CloudWatch Logs)の確認し、ユーザ名もしくは指定した秘密鍵が適切かご確認ください。

参考:AWS SFTPログをCloudWatch Logsに出力してみた #reinvent | DevelopersIO

 $ sftp -i .ssh/sftp.pem hato@s-0123456789.server.transfer.ap-northeast-1.amazonaws.com
hato@s-0123456789.server.transfer.ap-northeast-1.amazonaws.com: Permission denied (publickey).
Connection closed

ユーザ名が間違っている場合

サーバ側のログ(CloudWatch Logs)の出力例

ERRORS AUTH_FAILURE Method=publickey User=hato Message="The user is unrecognized" SourceIP=203.0.113.100

指定した秘密鍵が間違っている場合

サーバ側のログ(CloudWatch Logs)の出力例

ERRORS AUTH_FAILURE Method=publickey User=hato Message="RSA SHA256:Z8puNdFSK3j8NggyyW2E5IttgYRoIkOAGtYm2uRJcHY" SourceIP=203.0.113.100

-vオプションを設定すると、指定した秘密鍵のフィンガープリント(28行目)が確認できるため、公開鍵のフィンガープリント(20行目)と一致しているかご確認ください。

$ sftp -v -i .ssh/sftp_dummry hato@s-0123456789.server.transfer.ap-northeast-1.amazonaws.com 
OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug1: Connecting to s-0123456789.server.transfer.ap-northeast-1.amazonaws.com port 22.
debug1: Connection established.
debug1: identity file .ssh/sftp_dummry type 0
debug1: identity file .ssh/sftp_dummry-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1
debug1: Remote protocol version 2.0, remote software version AWS_SFTP_1.0
debug1: no match: AWS_SFTP_1.0
debug1: Authenticating to s-0123456789.server.transfer.ap-northeast-1.amazonaws.com:22 as 'hato'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: ecdh-sha2-nistp256
debug1: kex: host key algorithm: rsa-sha2-512
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:YyYLHPH4SuGipCA7KjvPTDaDxZN3jHGuxphMabxcchg
debug1: Host 's-0123456789.server.transfer.ap-northeast-1.amazonaws.com' is known and matches the RSA host key.
debug1: Found key in /Users/hato/.ssh/known_hosts:31
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: .ssh/sftp_dummry RSA SHA256:Z8puNdFSK3j8NggyyW2E5IttgYRoIkOAGtYm2uRJcHY explicit
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: .ssh/sftp_dummry RSA SHA256:Z8puNdFSK3j8NggyyW2E5IttgYRoIkOAGtYm2uRJcHY explicit
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
hato@s-0123456789.server.transfer.ap-northeast-1.amazonaws.com: Permission denied (publickey).
Connection closed
$

invalid format

考えられる原因:秘密鍵の形式が不正な場合

秘密鍵の形式が不正な場合(秘密鍵のかわりに公開鍵を指定した等)は次のメッセージが表示されます。

$ sftp -i .ssh/sftp.pem.pub hato@s-0123456789.server.transfer.ap-northeast-1.amazonaws.com
Load key ".ssh/sftp.pem.pub": invalid format
hato@s-0123456789.server.transfer.ap-northeast-1.amazonaws.com: Permission denied (publickey).
Connection closed

秘密鍵は-----BEGIN RSA PRIVATE KEY-----、公開鍵はssh-rsaがファイルの先頭行に記録されていますので、指定したファイルの中身を確認して、鍵が一致しているかご確認ください。

※サーバ側のログ(CloudWatch Logs)は記録されません。

No such file or directory

考えられる原因:秘密鍵が存在しない場合

指定した秘密鍵ファイルが存在しない場合(指定した場所に秘密鍵がない)は次のメッセージが表示されます。秘密鍵の指定場所に誤りがないか、また存在するかご確認ください。

※サーバ側のログ(CloudWatch Logs)は記録されません。

$ sftp -i .ssh/sftp.pe hato@s-0123456789.server.transfer.ap-northeast-1.amazonaws.com 
Warning: Identity file .ssh/sftp.pe not accessible: No such file or directory.
hato@s-0123456789.server.transfer.ap-northeast-1.amazonaws.com: Permission denied (publickey).
Connection closed

UNPROTECTED PRIVATE KEY FILE!

考えられる原因:秘密鍵のパーミッションが不適切な場合

指定した鍵ファイルのパーミッションが不適切な場合は次のメッセージが表示されます。 秘密鍵は必要最小限のパーミッションに設定してください(chmod 400 .ssh/sftp.pem

※サーバ側のログ(CloudWatch Logs)は記録されません。

$ sftp -i .ssh/sftp.pem hato@s-0123456789.server.transfer.ap-northeast-1.amazonaws.com
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
Permissions 0777 for '.ssh/sftp.pem' are too open.
It is required that your private key files are NOT accessible by others.
This private key will be ignored.
Load key ".ssh/sftp.pem": bad permissions
hato@s-0123456789.server.transfer.ap-northeast-1.amazonaws.com: Permission denied (publickey).
Connection closed

Permission denied

考えられる原因:IAMロールの権限不足

ユーザに設定された IAMロール にアクセス先ストレージ(S3等)へのアクセス権限がない場合、接続は成功しますが、実際にファイルにアクセスすると次のメッセージが表示されます。 IAMロール に設定したユーザの権限をご確認ください。

$ sftp -i .ssh/sftp.pem hato@s-0123456789.server.transfer.ap-northeast-1.amazonaws.com
Connected to s-0123456789.server.transfer.ap-northeast-1.amazonaws.com.
sftp> 
sftp> ls
remote readdir("/hato-sftp-bucket/hato"): Permission denied
sftp>

サーバ側ログ(CloudWatch Logs)の出力例

CONNECTED SourceIP=203.0.113.100 User=hato HomeDir=/hato-sftp-bucket/hato Client=SSH-2.0-OpenSSH_8.1 Role=arn:aws:iam::123456789012:role/hato-Transfer-s3 Kex=ecdh-sha2-nistp256 Ciphers=chacha20-poly1305@openssh.com,chacha20-poly1305@openssh.com
ERROR Message="Access denied"
DISCONNECTED

【参考】正常な接続できた場合(Connected to xxx)

正常に接続できた場合は、次のメッセージが表示されます。
過去に接続履歴がある場合(known hosts登録済)、2〜5行目は表示されません。

$ sftp -i .ssh/sftp.pem hato@s-0123456789.server.transfer.ap-northeast-1.amazonaws.com
The authenticity of host 's-0123456789.server.transfer.ap-northeast-1.amazonaws.com (203.0.113.100)' can't be established.
RSA key fingerprint is SHA256:YyYLHPH4SuGipCA7KjvPTDaDxZN3jHGuxphMabxcchg.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 's-0123456789.server.transfer.ap-northeast-1.amazonaws.com,203.0.113.100' (RSA) to the list of known hosts.
Connected to s-0123456789.server.transfer.ap-northeast-1.amazonaws.com.
sftp>

最後に

内容によって確認するログは異なりますので、ご注意ください。

この記事が誰かのお役にたてば幸いです。

アノテーション株式会社について

アノテーション株式会社は、クラスメソッド社のグループ企業として「オペレーション・エクセレンス」を担える企業を目指してチャレンジを続けています。「らしく働く、らしく生きる」のスローガンを掲げ、様々な背景をもつ多様なメンバーが自由度の高い働き方を通してお客様へサービスを提供し続けてきました。現在当社では一緒に会社を盛り上げていただけるメンバーを募集中です。少しでもご興味あれば、アノテーション株式会社WEBサイトをご覧ください。