質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
公開鍵認証

公開鍵認証とは、公開鍵と秘密鍵の2つの鍵の組を利用する、SSHで利用される認証方式です。

SSH

SSH(Secure Shell)は、セキュアチャネルを通してデータを交換するためのネットワークプロトコルです。リモートサーバーへのコマンド実行やファイル転送を行う時に一般的に使用されます。

VPS

VPS(バーチャル・プライベート・サーバ)は、仮想化されたサーバをレンタルするサービスで、共有サーバでありながら専門サーバと同等の機能を果たします。物理的な専門サーバより安価で提供できるメリットがあります。

Q&A

解決済

2回答

3498閲覧

公開鍵方式のssh接続ログイン時にパスワード入力が求められてしまう。

decatail

総合スコア41

公開鍵認証

公開鍵認証とは、公開鍵と秘密鍵の2つの鍵の組を利用する、SSHで利用される認証方式です。

SSH

SSH(Secure Shell)は、セキュアチャネルを通してデータを交換するためのネットワークプロトコルです。リモートサーバーへのコマンド実行やファイル転送を行う時に一般的に使用されます。

VPS

VPS(バーチャル・プライベート・サーバ)は、仮想化されたサーバをレンタルするサービスで、共有サーバでありながら専門サーバと同等の機能を果たします。物理的な専門サーバより安価で提供できるメリットがあります。

0グッド

1クリップ

投稿2021/05/08 12:47

編集2021/05/09 01:55

ドットインストール(ここここ)を参考に、公開鍵方式でのssh接続を試しています。接続自体は出来るのですが、パスワードを求められます。

MAC ~ % ssh -i ~/.ssh/id_rsa VPSUSER@IP.ADD.RE.SS The authenticity of host 'IP.ADD.RE.SS (IP.ADD.RE.SS)' can't be established. ECDSA key fingerprint is SHA256:Rjz8BLk1~~~~~~~~~~~~~~~中略~~~~~~~~~~~~wFAxk4. Are you sure you want to continue connecting (yes/no/[fingerprint])? yes Warning: Permanently added 'IP.ADD.RE.SS' (ECDSA) to the list of known hosts. VPSUSER@IP.ADD.RE.SS's password: MAC ~ % ssh -i ~/.ssh/id_rsa VPSUSER@IP.ADD.RE.SS VPSUSER@IP.ADD.RE.SS's password:

通常、公開鍵方式での接続の場合は、このようなパスワードは求められないと認識しているのですが、間違いないですよね?。
このあと、パスワードでのログインが出来ないように設定をするつもりですので、何とかしたいと思うのですが、わかる人がいましたらご教授いただければ幸いです。パスフレーズの設定はしていません。

また、Cyberduckでは、SFTPにより、パスワード無しでのログインができました。
検索するとknownhostなるものをいじるとよいと、聞いて、上記のknown_hostsを消してみましたが、症状は改善しませんでした。
他に必要な情報があればおっしゃっていただければと存じます。

下記にサーバー上の公開鍵の権限など

$ pwd /home/VPSUSER $ ls -la drwx------ 2 VPSUSER VPSUSER 4096 5月 8 20:59 .ssh $ ls -la .ssh -rw------- 1 VPSUSER VPSUSER 582 5月 8 20:52 authorized_keys

VPSの環境

Ubuntu 20.04.2 LTS (GNU/Linux 5.4.0-72-generic x86_64)

使用しているPCはmacOS 11.3.1です。

MAC ~ % cat .ssh/known_hosts IP.ADD.RE.SS ecdsa-sha2-nistp256 AAA略

試した事
①PC側で公開鍵の作成

MAC ~ % ssh-keygen -t rsa -v Generating public/private rsa key pair. Enter file in which to save the key (/Users/user/.ssh/id_rsa): //エンターキーのみ Created directory '/Users/user/.ssh'. Enter passphrase (empty for no passphrase): //エンターキーのみ(パスフレーズの設定はしていません) Enter same passphrase again: //エンターキーのみ Your identification has been saved in /Users/user/.ssh/id_rsa. Your public key has been saved in /Users/user/.ssh/id_rsa.pub. The key fingerprint is: SHA256:R5LEAQ9xyd~~~~~~中略~~~~~ user@Macbook-Air.local The key's randomart image is: +---[RSA 3072]----+ | ああ| +----[SHA256]-----+ MAC ~ % chmod 600 .ssh/id_rsa.pub

②VPSサーバー側で公開鍵の受け皿の作成

$ mkdir .ssh $ chmod 700 .ssh

③MAC側で公開鍵のアップロード

MAC ~ % scp ~/.ssh/id_rsa.pub VPSUSER@IP.ADD.RE.SS:~/.ssh/authorized_keys The authenticity of host 'IP.ADD.RE.SS (IP.ADD.RE.SS)' can't be established. ECDSA key fingerprint is SHA256:Rjz8BLk~~~~~~~~~~~~~~~~~~~~~~~~. Are you sure you want to continue connecting (yes/no/[fingerprint])? y Please type 'yes', 'no' or the fingerprint: yes Warning: Permanently added 'IP.ADD.RE.SS' (ECDSA) to the list of known hosts. VPSUSER@IP.ADD.RE.SS's password: scp: /home/VPSUSER/.ssh/authorized_keys: No such file or directory MAC ~ % scp ~/.ssh/id_rsa.pub VPSUSER@IP.ADD.RE.SS:~/.ssh/authorized_keys VPSUSER@IP.ADD.RE.SS's password: id_rsa.pub 100% 582 35.5KB/s 00:00

追記
「Mac ~ % ssh -v VPSUSER@IP.ADD.RE.SS」を実行。

Mac ~ % ssh -v VPSUSER@IP.ADD.RE.SS 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 IP.ADD.RE.SS [IP.ADD.RE.SS] port 22. debug1: Connection established. debug1: identity file /Users/user/.ssh/id_rsa type 0 debug1: identity file /Users/user/.ssh/id_rsa-cert type -1 debug1: identity file /Users/user/.ssh/id_dsa type -1 debug1: identity file /Users/user/.ssh/id_dsa-cert type -1 debug1: identity file /Users/user/.ssh/id_ecdsa type -1 debug1: identity file /Users/user/.ssh/id_ecdsa-cert type -1 debug1: identity file /Users/user/.ssh/id_ed25519 type -1 debug1: identity file /Users/user/.ssh/id_ed25519-cert type -1 debug1: identity file /Users/user/.ssh/id_xmss type -1 debug1: identity file /Users/user/.ssh/id_xmss-cert type -1 debug1: Local version string SSH-2.0-OpenSSH_8.1 debug1: Remote protocol version 2.0, remote software version OpenSSH_8.2p1 Ubuntu-4ubuntu0.2 debug1: match: OpenSSH_8.2p1 Ubuntu-4ubuntu0.2 pat OpenSSH* compat 0x04000000 debug1: Authenticating to IP.ADD.RE.SS:22 as 'VPSUSER' debug1: SSH2_MSG_KEXINIT sent debug1: SSH2_MSG_KEXINIT received debug1: kex: algorithm: curve25519-sha256 debug1: kex: host key algorithm: ecdsa-sha2-nistp256 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: ecdsa-sha2-nistp256 SHA256:Rjz8BLk12zzXoBrcG5vZlzT+RWUosls93VYylwFAxk4 debug1: Host 'IP.ADD.RE.SS' is known and matches the ECDSA host key. debug1: Found key in /Users/user/.ssh/known_hosts:1 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: /Users/user/.ssh/id_rsa RSA SHA256:R5LEAQ9xydhROZe3c9la/qbFxFM1UB0DNJQuv6XTjY8 debug1: Will attempt key: /Users/user/.ssh/id_dsa debug1: Will attempt key: /Users/user/.ssh/id_ecdsa debug1: Will attempt key: /Users/user/.ssh/id_ed25519 debug1: Will attempt key: /Users/user/.ssh/id_xmss debug1: SSH2_MSG_EXT_INFO received debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,sk-ssh-ed25519@openssh.com,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521,sk-ecdsa-sha2-nistp256@openssh.com> debug1: SSH2_MSG_SERVICE_ACCEPT received debug1: Authentications that can continue: publickey,password debug1: Next authentication method: publickey debug1: Offering public key: /Users/user/.ssh/id_rsa RSA SHA256:R5LEAQ9xydhROZe3c9la/qbFxFM1UB0DNJQuv6XTjY8 debug1: Authentications that can continue: publickey,password debug1: Trying private key: /Users/user/.ssh/id_dsa debug1: Trying private key: /Users/user/.ssh/id_ecdsa debug1: Trying private key: /Users/user/.ssh/id_ed25519 debug1: Trying private key: /Users/user/.ssh/id_xmss debug1: Next authentication method: password VPSUSER@IP.ADD.RE.SS's password: debug1: Authentication succeeded (password). Authenticated to IP.ADD.RE.SS ([IP.ADD.RE.SS]:22). debug1: channel 0: new [client-session] debug1: Requesting no-more-sessions@openssh.com debug1: Entering interactive session. debug1: pledge: network debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0 debug1: Remote: Ignored authorized keys: bad ownership or modes for direcuser /home/VPSUSER debug1: Sending environment. debug1: Sending env LANG = ja_JP.UTF-8 Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.4.0-72-generic x86_64) * Documentation: https://help.ubuntu.com * Management: https://landscape.canonical.com * Support: https://ubuntu.com/advantage System information as of 2021年 5月 9日 日曜日 09:00:19 JST System load: 0.08 Processes: 107 Usage of /: 27.8% of 29.40GB Users logged in: 0 Memory usage: 39% IPv4 address for eth0: IP.ADD.RE.SS Swap usage: 1% * Pure upstream Kubernetes 1.21, smallest, simplest cluster ops! https://microk8s.io/ Last login: Sat May 8 21:07:14 2021 from 1 $

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

doda

2021/05/08 19:00

ssh -v VPSUSER@IP.ADD.RE.SS の出力は出せますか? パスワードを訊かれると思うので、パスワードを入力してログインする所までやって下さい。
decatail

2021/05/09 00:02

コメントありがとうございます。少し長いので、質問文に追記いたしました。
guest

回答2

0

debug1: Offering public key: .ssh/id_rsa RSA SHA256:R5LEAQ

をヒントにググってみました。

http://blog.yuryu.jp/2014/01/ssh-auth-fails-with-different-public-key.html?m=1
というのが見つかりましたが……
~/.sshにある鍵の対応が違う…ということはありませんか?
id_rsa.pubをリネームしてしまうのがかんたんかと思いますが。

サーバ側のログを確認する。
というのもありますが…sshd_configでLogLevel変更していないとたいしたログが出ないんですよね。
sshd[181298]: Accepted publickey for setoppu from 192.168.1.32 port 38238 ssh2: RSA SHA256:TKhdSut0…
sshd[181298]: pam_unix(sshd:session): session opened for user setoppu by (uid=0)
sshd[184388]: Accepted password for setoppu from 192.168.1.32 port 40270 ssh2
sshd[184388]: pam_unix(sshd:session): session opened for user setoppu by (uid=0)
こんな感じで。。

投稿2021/05/09 01:48

setoppu

総合スコア292

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

decatail

2021/05/09 07:42

コメントありがとうございました。たしかに何故出来ないのか悩んで、公開鍵秘密鍵を作りまくって、known_hostともども消しまくったので混乱してそういうへまをする場合があるかも知れません。ありがとうございました。sshのvオプションのデバッグの見方も勉強します……。
guest

0

ベストアンサー

debug1: Remote: Ignored authorized keys: bad ownership or modes for direcuser /home/VPSUSER

ホームディレクトリ/home/VPSUSERの所有者または権限が原因で~/.ssh/authorized_keysが無視されています。
以下のコマンドを実行して、所有者と権限を修正してください。

sh

1chown VPSUSER /home/VPSUSER 2chmod go-w /home/VPSUSER

投稿2021/05/09 01:47

doda

総合スコア947

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

decatail

2021/05/09 09:17 編集

コメントありがとうございました。貴殿のコメントをrootにて実行する事により、パスワードを求められずにログインする事が出来ました。正常な動作なのかは分かりませんが、Ubuntuサーバーにて作業用ユーザーを作成した際、ホームディレクトリが何故か作られていないということがあり、自分で作ったのですが、このことが混乱を招いた原因なのかも知れません。重ね重ねお礼申し上げます。 # useradd VPSUSER # passwd VPSUSER 新しいパスワード: 新しいパスワードを再入力してください: passwd: パスワードは正しく更新されました # usermod -aG sudo VPSUSER # gpasswd -a VPSUSER sudo ユーザ VPSUSER をグループ sudo に追加 MAC ~ % ssh VPSUSER@IP.ADD.RE.SS VPSUSER@IP.ADD.RE.SS's password: Welcome to Ubuntu 20.04.2 LTS (GNU/Linux 5.4.0-72-generic x86_64) (中略) Could not chdir to home directory /home/VPSUSER: No such file or directory $ pwd / $ cd home $ ls $ mkdir VPSUSER mkdir: ディレクトリ `VPSUSER' を作成できません: 許可がありません $ sudo mkdir VPSUSER $ cd VPSUSER # chown -R VPSUSER /home/VPSUSER # chmod 770 -R /home/VPSUSER/ # ls -a -l /home/VPSUSER/ 合計 16 drwxrwx--- 4 VPSUSER root 4096 5月 8 19:11 . drwxr-xr-x 3 root root 4096 5月 8 18:44 .. drwxrwx--- 2 VPSUSER VPSUSER 4096 5月 8 19:11 .cache drwxrwx--- 2 VPSUSER root 4096 5月 8 19:19 .ssh
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問