レンタルサーバのロリッポップでは、スタンダードプラン以上で契約すると、sshログイン権限を手に入れることができます。SSHのログインはユーザID/パスワードでの認証と、SSHキーファイルによる認証などがありますが、ロリッポップではデフォルトでパスワードによる認証でのログインのみが提供されています。
ですが、CIツールなどから自動でロリポップサーバへアクセスしたい場合、SSHキーファイルによる認証でログインしたい場合があります。
そこで今回は、ロリポップのサーバへSSHキー認証によるログインを行う方法を説明します。
SSHキーファイルを作成する。
まず、手元の環境でssh-keygenコマンドを実行してsshキーを作成します。
下記の例では、キーの名前を指定しておりコマンドの実行が完了すると、/Users/xxx/.ssh/id_rsa_lolipopと/Users/xxx/.ssh/id_rsa_lolipop.pubが作成されます。
キー作成に聞かれるpassphraseは、今回は無しにしました。
$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/xxx/.ssh/id_rsa): /Users/xxx/.ssh/id_rsa_lolipop
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/xxx/.ssh/id_rsa_lolipop.
Your public key has been saved in /Users/xxx/.ssh/id_rsa_lolipop.pub.
The key fingerprint is:
SHA256:...
秘密鍵をロリポップのサーバにアップする
次に、作成された公開鍵(/Users/xxx/.ssh/id_rsa_lolipop.pub)を、ロリポップのサーバにコピーします。
$ mkdir .ssh
$ vi .ssh/authorized_keys
ファイルを作成したら、ユーザの権限を600にしておきます。
# 権限をセット
$ chmod 600 .ssh/authorized_keys
# 権限が-rw-------になっていることを確認
```language-bash
$ ls -l .ssh/authorized_keys
-rw------- 1 user LolipopUser 1679 2018-08-12 20:29 .ssh/authorized_key
ローカルのPCで設定ファイルを作成する
以下の内容でローカルPCにロリポップの接続設定を作詠します。
IdentifyFileに、先ほど作成した秘密鍵のファイル名を指定します。
User, Hostnameに設定する値はロリポップの管理画面で確認してください
Host lolipop
User xxx
Hostname ssh999.lolipop.jp
IdentityFile ~/.ssh/id_rsa_lolipop
Port 2222
SSHキーによるログインを行う
設定が終わったらsshコマンドでログインできることを確認します。
$ ssh lolipop
Last login: Sun Aug 12 00:11:22 2018 from 11.22.33.44
[xxxx@users999 ~]$
こちらもおススメ