1. 程式人生 > >Linux伺服器設定SSH登陸

Linux伺服器設定SSH登陸

Linux伺服器設定SSH登陸


製作linux伺服器金鑰對

[[email protected] ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/exchange/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/exchange/.ssh/id_rsa.
Your public key has been saved in /home/exchange/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:HwO0jv+tPpO9vfNkGk5qsnWw67UhtdlAB4zqKy3X1Jk 
[email protected]
The key's randomart image is: +---[RSA 2048]----+ | . o. | | . . . .. | | o . . .| | o .. . . | | . S.o ..oo | | . ..o.+E= | | ...*+ O +| | o.Oo+X.B | | ==O*.*+.| +----[SHA256]-----+

再開啟

[[email protected]
~]$ cd ~/.ssh [[email protected] .ssh]$ ls authorized_keys id_rsa id_rsa.pub [[email protected] .ssh]$ ll total 12 -rw-------. 1 exchange exchange 747 9月 11 18:17 authorized_keys -rw-------. 1 exchange exchange 1679 9月 18 20:46 id_rsa -rw-r--r--. 1 exchange exchange 412 9月 18 20:46 id_rsa.pub

這裡有兩種做法
1.將伺服器公鑰匯入到 authorized_keys 中,將私鑰下載到客戶端,通過工具連線。

[[email protected] .ssh]$ cat id_rsa.pub >> authorized_keys

把 id_rsa 下載下來
2.電腦端生成 ssh-key,將公鑰發給管理員加到 authorized_keys 檔案中。

上述兩種方法都可以
修改許可權

[[email protected] .ssh]$ chmod 600 authorized_keys 
[[email protected] .ssh]$ chmod 700 ~/.ssh

#修改SSH配置
[[email protected] .ssh]$ sudo vim /etc/ssh/sshd_config 
主要是以下三個:
RSAAuthentication yes    # 啟用 RSA 認證
PubkeyAuthentication yes    # 啟用公鑰認證
PermitRootLogin yes    # 允許root使用者ssh登陸
最後重啟SSH服務
[[email protected] .ssh]$ service sshd restart

儲存之後就可以用ssh 直接登陸了,特別是電腦裝的linux系統,用ssh命令不用輸入密碼,很方便。windows下的ssh 客戶端很多,可能感覺不出來這種方式的便捷。