3
2

More than 5 years have passed since last update.

SSH鍵認証の方法

Last updated at Posted at 2014-07-27

鍵の名前は適当にユーザ名などに変更しておく。デフォルトはid_rsaになるので、ログイン元のPC内の.ssh配下でかぶってなければそれでOK。

基本的には名前を変えた方が無難。

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/<home>/<foo>/.ssh/<foo>_rsa): /<home>/<foo>/.ssh/<foo>_rsa

パスワード聞かれるので空でよし。

Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 

そうすると///.ssh/の下に

・<foo>_rsa
・<foo>_rsa.pub

が出来ているはず。

その後、ログイン先のサーバで一般ユーザを作成する。パスワード設定は必要ない。

# useradd <foo>

作成したユーザに変更する。

# su <foo>
$ mkdir ~/.ssh
$ chmod 700 ~/.ssh

PCで作成した<foo>_rsa.pubをログイン先のサーバに対してSCPでコピーする。

$ scp /<home>/<foo>/.ssh/<foo>_rsa.pub <foo>@<IP Address>:~/.ssh/authorized_keys

サーバにてauthorized_keysの所有権変更。

$ chmod 600 ~/.ssh/authorized_keys

その後、rootに戻り、/etc/ssh/sshd_configを編集。

#Port22 
→Port**** 

#PermitRootLogin yes 
→PermitRootLogin no 

PasswordAuthentication yes 
→PasswordAuthentication no

sshdを再起動。

# service sshd restart

ログインしてみる。

ssh -p <PortNumber> -i ~/.ssh/<foo>_rsa <foo>@<IP Address>

いちいち秘密鍵や、変更したポート番号を指定したりするのがめんどくさいので、PC内の~/.ssh/configを作成しログイン先のサーバをHostとして登録しておく。

Host <myServerName> → (任意)
    HostName <IP Address>
    User <foo>
    IdentityFile ~/.ssh/<foo>_rsa
    Port <PortNumber>

下記のコマンドでログインできるはず。

$ ssh <myServerName>
3
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
2