ホームディレクトリなしでSSHキーを保存する方法は?

ホームディレクトリなしでSSHキーを保存する方法は?

remoteB別のサーバーからリモートコンピュータにログインしようとしていますが、毎回パスワードを入力したくremoteAありません。remoteBこれを行うためにSSHキーを生成したいが、問題はremoteA私のremoteB

.sshAの(ホームではない)ディレクトリ内にアクセスできるディレクトリを作成しようとしましたが、そうすると

ssh-copy-id -i id_rsa.pub username@remoteB

これによりエラーが返されます。

Could not create directory '/home/name/.ssh'
The authenticity of host 'remoteB' can't be established

/home/にディレクトリがないため、これは意味がありますremoteA。しかし、ホームディレクトリ外のフォルダをSSHキーとして使用する方法はありますか?

ベストアンサー1

このメッセージはCould not create directory '/home/test3/.ssh'.エラーではなく警告です。安全な場所にキーを保存できますが、デフォルトの場所はsshホームディレクトリです。

たとえば、ローカルユーザーにはtest3ホームディレクトリはありませんが、ユーザーtest4@otherhostにはあります。まず、test3ユーザーとしてローカルにログインします。

「セキュリティ」ディレクトリの生成と証明書のペアの生成

mkdir -m700 /tmp/ssh
ssh-keygen -t rsa -f /tmp/ssh/id_rsa
Generating public/private rsa key pair.
...
Your public key has been saved in /tmp/ssh/id_rsa.pub.
...

ターゲットにコピーしてみてください

ssh-copy-id -i /tmp/ssh/id_rsa.pub test4@otherhost
/usr/bin/ssh-copy-id: 59: cd: can't cd to /home/test3
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/tmp/ssh/id_rsa.pub"
The authenticity of host 'otherhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:pqNd/9gP69W2hzcosj+GI2DY2uw3+Upvvg22KV8sq5A.
Are you sure you want to continue connecting (yes/no)? yes
mktemp: failed to create file via template ‘/home/test3/.ssh/ssh-copy-id_id.XXXXXXXXXX’: No such file or directory
/usr/bin/ssh-copy-id: ERROR: mktemp failed

この時点で参照してくださいインストール失敗したがって、それに対応する手動プロセスに戻る必要があります。また、ファイルを使用するたびに文句を言わないように、known_hostsファイルを安全な場所に保管してください。ssh

ssh -o UserKnownHostsFile=/tmp/ssh/known_hosts test@otherhost 'mkdir -m700 -p .ssh && cat >>.ssh/authorized_keys' </tmp/ssh/id_rsa.pub
Could not create directory '/home/test3/.ssh'.
The authenticity of host 'otherhost (127.0.0.1)' can't be established.
ECDSA key fingerprint is SHA256:pqNd/9gP69W2hzcosj+GI2DY2uw3+Upvvg22KV8sq5A.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'otherhost' (ECDSA) to the list of known hosts.
Password:

これには多くの注意事項がありますが、本質的にプロセスは成功しており、キーは現在リモートアカウントのauthorized_keysファイルにあります。テストしてみましょう

ssh -o UserKnownHostsFile=/tmp/ssh/known_hosts -i /tmp/ssh/id_rsa test4@otherhost date
Could not create directory '/home/test3/.ssh'.
Thu 26 Nov 10:16:23 GMT 2020

リモートホストから日付文字列を取得したので、それがすべて機能していることを証明できます。

注:ファイルと秘密は、使用するたびにssh明示的に定義する必要があります。known_hostsid_rsa

ssh -o UserKnownHostsFile=/tmp/ssh/known_hosts -i /tmp/ssh/id_rsa ...

おすすめ記事