Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux基礎 >> Linux技術

配置 Linux 免密碼登入:ssh-key 五步驟完整流程

整件事拆成五步:產生密鑰對、把公鑰改名為 authorized_keys、把權限改成 600、修改 sshd 設定、重啟 sshd 服務。

友情提示:如果你打算用 Xshell 這類終端工具做免密碼登入,在產生密鑰對的階段就務必先把 id_rsa 拷貝出來。否則等你全部設定好,才想用 Xftp 把檔案拉回來時,會發現根本沒有鑰匙檔可拿,陷入死循環。切記切記。

1. 產生密鑰對,一路 Enter

[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase 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:
92:98:a4:6d:a3:bf:04:67:f5:5f:d9:e2:9f:e1:b8:c1 root@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
| . .             |
| + + o o         |
| o O o S + .     |
| * . . . + .     |
| . . . E .       |
| o = o           |
| o. o.+          |
+-----------------+
[root@localhost ~]#

passphrase 直接留空按 Enter,才是真正意義上的「免密碼」;設了 passphrase 反而每次都要輸入。

2. 把公鑰改名為 authorized_keys

[root@localhost .ssh]# cd /root/.ssh/
[root@localhost .ssh]# cat id_rsa.pub > authorized_keys

3. 權限設為 600

[root@localhost .ssh]# chmod 600 authorized_keys

權限太開的話,sshd 基於安全考量會直接忽略這份 authorized_keys,這是常見的失敗原因。.ssh 目錄本身也建議設為 700。

4. 修改 sshd 設定檔

[root@localhost .ssh]# vi /etc/ssh/sshd_config
RSAAuthentication yes
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys
PasswordAuthentication no

最後一行關掉密碼登入,是為了避免暴力破解;但請先確認公鑰登入成功,再關它,否則可能把自己鎖在門外。

5. 重啟 sshd

[root@localhost .ssh]# service sshd restart
Stopping sshd: [ OK ]
Starting sshd:  [ OK ]
[root@localhost .ssh]#

最後把 id_rsa 拷貝到任何你信任的地方保存好,就能用它執行無密碼登入了。私鑰等於通行證,切勿外流。

本文出自「9462880」部落格。

Copyright © Linux教程網 All Rights Reserved