0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[CentOS 8]SSHについてのメモ

Posted at

はじめに

sshについて知らないこともまだまだあったので、そのメモです。

OSバージョン

$ cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)

公開鍵を使用したパスワードなしのログイン

公開鍵の作成

[client]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user01/.ssh/id_rsa): #パスワードを入れずにEnter
Created directory '/home/user01/.ssh'.
Enter passphrase (empty for no passphrase): #パスワードを入れずにEnter
Enter same passphrase again: #パスワードを入れずにEnter
Your identification has been saved in /home/user01/.ssh/id_rsa.
Your public key has been saved in /home/user01/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:0/SAWMk9845x3ZV5ausHNJ/5QE1D3992wYGWEz5cBck user01@centos01
The key's randomart image is:
+---[RSA 3072]----+
|       ..o   o**=|
|       oo.+ o=E+B|
|      . . o+.=.B*|
|         o.oo O *|
|        S .=.+ +B|
|         .. . ++o|
|             . o.|
|              . o|
|               . |
+----[SHA256]-----+

[client]$ ls -l .ssh/
合計 8
-rw-------. 1 user01 user01 2602  7月 13 15:38 id_rsa
-rw-r--r--. 1 user01 user01  569  7月 13 15:38 id_rsa.pub

公開鍵のコピー

ここではclientのuser01で作成した公開鍵をdbsrvのdbadminにコピーします。

[client]$ ssh-copy-id dbadmin@dbsrv
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/user01/.ssh/id_rsa.pub"
The authenticity of host 'dbsrv (192.168.100.102)' can't be established.
ECDSA key fingerprint is SHA256:oCUEvqcZwBDQP4NvbRwU92gSWj58nyoJV7qxhq17QOY.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
dbadmin@dbsrv's password:

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'dbadmin@dbsrv'"
and check to make sure that only the key(s) you wanted were added.
[dbsrv]$ ls -l .ssh/
合計 4
-rw-------. 1 dbadmin dbadmin 569  7月 13 15:46 authorized_keys

ログインの確認

[client]$ ssh dbadmin@dbsrv
Activate the web console with: systemctl enable --now cockpit.socket

Last login: Mon Jul 13 15:48:05 2020
[dbsrv]$

スーパーユーザーによる SSH を使用したログインの禁止

/etc/ssh/sshd_configファイルの PermitRootLoginパラメーターを設定して、ユーザーが root としてシステムにログインすることを許可または禁止する。

  • yes : rootでログインできる(デフォルト)
  • no : rootでログインできない
/etc/ssh/sshd_config

     43 # Authentication:
     44
     45 #LoginGraceTime 2m
     46 PermitRootLogin yes
     47 #StrictModes yes
     48 #MaxAuthTries 6
     49 #MaxSessions 10

ファイルを編集したら、systemctl reload sshdコマンドを実行する。

systemctl reload sshdコマンドは、サービスを完全に再起動するのではなく、設定ファイルを再度読み込む動作。
systemctl restart sshdコマンドでも変更は適用されるが、SSHサービスが再起動するので、もしそのホストへのアクティブな SSH 接続がある場合はすべて切断されてしまう。

SSH のパスワードベース認証の禁止

/etc/ssh/sshd_configファイルの PasswordAuthentication パラメーターを設定して、ユーザーがパスワードベース認証を使用してシステムにログインできるかどうかを設定できる。
パスワード認証を禁止した場合、鍵認証でのログインとなる。

  • yes : パスワード認証でログインできる(デフォルト)
  • no : パスワード認証でログインできない
/etc/ssh/sshd_config
     69
     70 # To disable tunneled clear text passwords, change to no here!
     71 #PasswordAuthentication yes
     72 #PermitEmptyPasswords no
     73 PasswordAuthentication yes
     74

ファイルを編集したら、同様にsystemctl reload sshdコマンドで設定ファイルを再読み込みする。

ログインせずに任意のコマンドを実行する

[client]$ ssh dbadmin@dbsrv pwd
/home/dbadmin
0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?