LoginSignup
1

More than 5 years have passed since last update.

SSHの設定

Last updated at Posted at 2018-06-03

openssh

sshは公開鍵、秘密鍵を使い、rsa認証する。
クライアントとサーバ間で、同じ公開鍵を持ち、秘密鍵を使ってログイン認証する。

インストール

$ sudo apt-get install openssh-server

動作確認

$ sudo lsof -i:22

rsa鍵の生成

クライアント側で秘密鍵(id_rsa)、公開鍵(id_rsa.pub)を生成。
公開鍵をサーバに登録

$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (~/.ssh/id_rsa):  ←空Enter(任意)
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/root/.ssh/id_rsa.
Your public key has been saved in /var/root/.ssh/id_rsa.pub.

公開鍵の設置

サーバに公開鍵を設置する
サーバに$HOME/.shhフォルダを作成し、そこに公開鍵をauthorized_keysとして置く

$ sudo mkdir -m 700 ~/.ssh

$ cat id_rsa.pub >> ~/.ssh/authorized_keys

$ sudo chmod 600 authorized_keys

設定ファイル編集

$ sudo vi /etc/ssh/sshd_config
Port 10022 ←適当に
PermitRootLogin no
PasswordAuthentication no

ユーザー許可を追記
AllowUsers hoge

再起動

@server:~$ sudo service ssh restart

鍵を更新する場合

鍵を再作成した場合はknown_hostsファイルを削除。
ログインしてみる

$ sudo rm ~/.ssh/known_hosts
$ ssh hogehoge.com -l hoge
Are you sure you want to continue connecting (yes/no)? : yes

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
1