sftpキー認証



Sftp Key Authentication



1。概要

キーログインでは、ユーザーがパスワードを設定する必要はありません。 RSAキーペアの暗号化と復号化の検証を通じて、クライアントとサーバーの間に安全な接続が確立されます。簡単に言うと、公開鍵はサーバー側に配置されます。つまり、以下で構成されたauthorized_keysであり、秘密鍵はクライアントに配置されます。一方、クライアントは接続要求を開始します。サーバーは、要求されたユーザー名に従って、対応するクライアントの公開鍵を識別します。 sshdサービスは乱数を生成します。この乱数は公開鍵で暗号化され、クライアントに返送されます。クライアントは秘密鍵を使用して乱数を復号化します。 、クライアントは復号化された乱数をサーバーに送り返し、サーバーはマッチングを実行し、マッチングは成功し、認証に合格してログインを許可します。この方法は、パスワードブルートフォースクラッキングの試みの危険を回避します。もちろん、鍵認証には暗号化と復号化、乱数送信検証のプロセスがあるため、接続時間は当然パスワード方式よりも長くなります。



2.キー認証ログインを実現します

リモートサーバーIP:192.168.191.128、ホスト名はcatyuan
ローカルクライアントIP:192.168.191.131、ホスト名はlocalhost



2.1ローカルでキーペアを作成する

[root@xxxxx ~]# ssh-keygen -t rsa #Create a key pair Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): #My default path here is this, or you can enter the key storage path by yourself Enter passphrase (empty for no passphrase): #Enter, do not encrypt the key Enter same passphrase again: #Enter, confirm again Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: bc:fa:ba:a0:70:41:50:64:3a:a4:99:41:d7:a9:44:fe root@xxxxx The key's randomart image is: +--[ RSA 2048]----+ |+==o. . | |oBo. o | |* o.. | | o .. . | | . E S | | . . | |. . . . | | o . . . | | . ++. | +-----------------+

2.2ディレクトリで生成された公開鍵(id_rsa.pub)と秘密鍵(id_rsa)を表示する

[root@xxxxx ~]# ls /root/.ssh/ id_rsa id_rsa.pub known_hosts

2.3公開鍵をリモートサーバーのファイル〜/ .ssh / authorized_keysにコピーします

[root@xxxxx ~]# ssh-copy-id root@xxxxx This service allows sftp connections only. #The server only allows sftp connections [root@xxxxx ~]# ssh-copy-id root@xxxxx # First pass to root, then cp [root@xxxxx ~]# mkdir /data/sftp/test/.ssh [root@xxxxx ~]# cp .ssh/authorized_keys /data/sftp/test/.ssh/

いくつかのコピー方法

1.公開鍵をscpを介してサーバーにコピーし、それを〜/ .ssh / authorized_keysファイルに追加します。これはさらに面倒です。 scp -P 22〜 / .ssh / id_rsa.pub root @ xxxxx:〜/。
2.私が示した方法であるssh-copy-idプログラムを使用すると、ssh-copy-id root @xxxxxで問題ありません。
3. cat〜 / .ssh / id_rsa.pub |を使用できます。 ssh -p 22 root @ xxxxx'cat >>〜/ .ssh / authorized_keys '。これもより一般的に使用される方法です。ポート番号は変更できるからです。

2.4権限の変更

.sshディレクトリのアクセス許可は700で、その下のファイルauthorized_keysと秘密鍵のアクセス許可は600です。それ以外の場合、アクセス許可のためにパスワードなしでログインすることはできません。ログイン後にknown_hostsファイルが生成されることがわかります。

[root@xxxxx ~]# chown -R test:sftp /data/sftp/test/.ssh/ [root@xxxxx ~]# chmod 600 /data/sftp/test/.ssh/authorized_keys [root@xxxxx ~]# chmod 700 /data/sftp/test/.ssh/

2.5構成ファイルを変更する

ユーザーにキー認証のみでのログインを許可する場合は、/ etc / ssh / sshd_configを設定できます
PasswordAuthenticationいいえ
変更しない場合は、キー認証とキー認証の両方のログイン方法が許可されます。

[root@xxxxx ~]# vim /etc/ssh/sshd_config PermitRootLogin yes #Allow root user to log in RSAAuthentication yes #Allow RSA security authentication PubkeyAuthentication yes #Allow key login AuthorizedKeysFile /data/sftp/test/.ssh/authorized_keys #Point out the location of the public key. This item is included in the configuration file, so comment it out.

RSAセキュリティ認証
RSAの公開鍵がサードパーティに認識された後、サードパーティは公開鍵を使用してデータを暗号化および送信します
例えば:
AがBに書き込みたい:
1.Bは公開鍵をAに送信します
2. Aは公開鍵暗号化レターのコンテンツを取得し、そのコンテンツをBに送信します
3. BがAの手紙を受け取ったら、秘密鍵でそれを解読します。
次に、問題が発生します。
質問1:BがAに公開鍵を与えると、それはサードパーティのCに知られていますが、CもBに書き込むことができますか? ?
質問2:Aは、Bがこのキーを与えたことをどのようにして知るのですか? ?
質問3:手紙がAによって書かれたことをBはどうやって知るのですか? ?

2.6サービスの再開

[root@xxxxx ~]# systemctl restart sshd`

2.7テストキーでログインする

[root@xxxxx ~]# sftp root@xxxxx Connected to 192.168.191.128. #At this time, no password is required to log in, use the secret key to log in sftp>