目標:設定主機必須同時使用私鑰及密碼才能登入。
   
Internet 隨時都有駭客用各種方式試圖入侵網站,因此主機建置後的第一件事就是加強安全性。
本篇示範,設定主機需同時使用私鑰及密碼才能登入;換句話說,若要登入主機,必須先取得私鑰以及您設定的私鑰密碼才能登入,這樣一來安全性就會提高很多。
   
詳細步驟也可參考:https://www.digitalocean.com/community/tutorials/how-to-set-up-ssh-keys
   
主機登入程序:
1.先使用權限較小的一般帳號登入主機,例如:demo
2.再使用 su root 指令切換至特權使用者
   
STEP 1:使用一般帳號登入後,使用下列指令產生公鑰和私鑰。     
$ ssh-keygen -t rsa      
Generating public/private rsa key pair.      
      
STEP 2:回答存放公鑰和私鑰的位置。       
Enter file in which to save the key (/home/demo/.ssh/id_rsa):       
Created directory ‘/home/demo/.ssh’.      
      
STEP 3:輸入私鑰的密碼。       
Enter passphrase (empty for no passphrase): ************       
Enter same passphrase again: ************      
      
說明私鑰與公鑰的存放目錄。       
Your identification has been saved in /home/demo/.ssh/id_rsa.       
 Your public key has been saved in /home/demo/.ssh/id_rsa.pub.       
      
產生公鑰與私鑰中…       
The key fingerprint is:       
 SHA256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx demo@sfo2-1       
 The key’s randomart image is:       
 +—[RSA 2048]—-+       
…       
+—-[SHA256]—–+      
     
STEP 4:設定此主機可使用新產生的公鑰登入。     
$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys      
     
STEP 5:設定主機只能使用一般帳號登入。       
設定主機 SSH config (使用 root 特權使用者)      
# vi /etc/ssh/sshd_config      
PermitRootLogin no       
PubkeyAuthentication yes       
PasswordAuthentication no       
      
STEP 6:重啟 sshd 服務,以套用新的設定值。       
# systemctl reload sshd.service        
        
STEP 7:完成。 
每個精彩的瞬間,每杯柏克斯咖啡 接單後烘培,新鮮健康、容易沖煮、風味久遠 [廣告] 柏克斯咖啡 CoffeeBox