サーバーに接続するための.pemファイルを作成したいと思います。
ssh-keygenを使用できることを知っていますが、特定のユーザーにそれを使用したいすべてのプロセスを実行できるスクリプトが必要です。私の必要性はサーバーXでスクリプトを一度実行することですので、Xサーバーに接続しようとするたびに、自分のコンピューターにある.pemファイルを使用してXサーバーに接続できます。
ベストアンサー1
私はこれを別の方法で行うことをお勧めします。
リモートコンピュータに秘密鍵を保存する必要はありません。
ローカルコンピュータでキーペアを生成します。
ssh-keygen -f .ssh/somekey -t rsa -b 4096
その後、リモートコンピュータにコピーします。
ssh-copy-id -i .ssh/somekey user@hostname
次にローカルを調整します
.ssh/config
。
$ cat << BLURB >> .ssh/config
Host shorthand
HostName server.com
User serveruser
IdentityFile ~/.ssh/somekey
BLURB
次の3つの手順をスクリプトに簡単に含めることができますgenandcopykey.sh
。
#!/bin/bash -
# USAGE: ./genandcopykey.sh [email protected] [email protected] ...
#
# DESCRIPTION: creates an ssh-keypair, copies pubkey to remotehost
# and updates .ssh/config to use it
set -o nounset # exit on unset variables.
set -o errexit # exit on any error.
unalias -a # avoid rm being aliased to rm -rf and similar issues
LANG=C # avoid locale issues
for item in $@; do
remoteuser="${item%@*}" # everything in front of the first "@"
remotehost="${item#*@}" # everything after
ssh-keygen -f "${HOME}/.ssh/${item}" -t rsa -b 4096
ssh-copy-id -i "${HOME}/.ssh/${item}.pub" "$item"
printf '%s\n' "
Host $remotehost
HostName $item
User $remoteuser
IdentityFile ${HOME}/.ssh/${item}" >> $HOME/.ssh/config
done
それでもスクリプトにオプションを追加し、機能を使用できます。これは単純な例に過ぎず、存在しないホストまたは.ssh
。