一直以來都是用帳號密碼來登入Linux,雖然已經知道可以用金鑰登入的方式,可是始終沒有養成習慣,主因是金鑰要好好保管有點麻煩,有時候重灌或是偷懶就會找不到,但最近覺得如果需要安全又方便的話,還是多練一下用金鑰登入主機會比較好。
一.建立ssh的公私鑰
\\首先要先開啟Powershell
PS C:\Users\TEMP> ssh-keygen -t rsa -b 4096 \\用指令加密金鑰,複雜度是4096bit
Generating public/private rsa key pair.
Enter file in which to save the key (C:\Users\TEMP/.ssh/id_rsa): labssh \\命名金鑰名稱
Enter passphrase (empty for no passphrase): \\這邊可以留空
Enter same passphrase again:
Your identification has been saved in labssh.
Your public key has been saved in labssh.pub.
The key fingerprint is:
SHA256:0ibshfimtrcc5n7riRYPeOP0NSvgWNUV4p2SP93M3YA yji\ian@NB02-Ian
The key's randomart image is:
+---[RSA 4096]----+
| . .. |
| . +.o |
| .+.E . |
| o o. .o . =o|
| ..=.S o . *|
| .oO= o . |
| B*B . o |
| o*=o=.. |
| .o=*++o |
PS C:\Users\TEMP> dir \\看一下金鑰產生在哪裡,會有一個私鑰和公鑰
目錄: C:\Users\TEMP
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2024/7/29 下午 01:58 .ms-ad
d----- 2024/7/30 上午 10:23 .vscode
d-r--- 2024/7/29 下午 01:36 3D Objects
d-r--- 2024/7/29 下午 01:36 Contacts
d-r--- 2024/8/16 上午 11:11 Desktop
d-r--- 2024/8/16 上午 11:43 Documents
d-r--- 2024/8/16 上午 11:11 Downloads
d-r--- 2024/7/29 下午 01:36 Favorites
d-r--- 2024/7/29 下午 01:36 Links
d-r--- 2024/7/29 下午 01:36 Music
d-r--- 2024/8/7 下午 01:34 OneDrive
d-r--- 2024/7/29 下午 01:36 Pictures
d----- 2023/11/24 下午 04:29 Roaming
d-r--- 2024/7/29 下午 01:36 Saved Games
d-r--- 2024/7/29 下午 01:53 Searches
d-r--- 2024/8/2 下午 02:05 Videos
-a---- 2024/8/16 上午 11:45 3381 labssh
-a---- 2024/8/16 上午 11:45 743 labssh.pub
---- ------------- ------ ----
d----- 2024/7/29 下午 01:58 .ms-ad
d----- 2024/7/30 上午 10:23 .vscode
d-r--- 2024/7/29 下午 01:36 3D Objects
d-r--- 2024/7/29 下午 01:36 Contacts
d-r--- 2024/8/16 上午 11:11 Desktop
d-r--- 2024/8/16 上午 11:43 Documents
d-r--- 2024/8/16 上午 11:11 Downloads
d-r--- 2024/7/29 下午 01:36 Favorites
d-r--- 2024/7/29 下午 01:36 Links
d-r--- 2024/7/29 下午 01:36 Music
d-r--- 2024/8/7 下午 01:34 OneDrive
d-r--- 2024/7/29 下午 01:36 Pictures
d----- 2023/11/24 下午 04:29 Roaming
d-r--- 2024/7/29 下午 01:36 Saved Games
d-r--- 2024/7/29 下午 01:53 Searches
d-r--- 2024/8/2 下午 02:05 Videos
-a---- 2024/8/16 上午 11:45 3381 labssh
-a---- 2024/8/16 上午 11:45 743 labssh.pub
PS C:\Users\TEMP> mkdir .ssh \\建立一個隱藏的.ssh資料夾,在把私鑰和公鑰放進去
目錄: C:\Users\TEMP
Mode LastWriteTime Length Name
---- ------------- ------ ----
d----- 2024/8/16 上午 11:46 .ssh
PS C:\Users\TEMP> mv .\labssh .\.ssh\
PS C:\Users\TEMP> mv .\labssh.pub .ssh
PS C:\Users\TEMP\.ssh> scp .\labssh.pub root@43.254.16.228:/root/.ssh \\把公鑰傳到遠端的Linux主機上
The authenticity of host '43.254.16.228 (43.254.16.228)' can't be established.
ECDSA key fingerprint is SHA256:TimU1wkAExkXEYPUAb+hj+URbt588tv75dlfIgIeAXA.
Are you sure you want to continue connecting (yes/no/[fingerprint])?
Warning: Permanently added '43.254.16.228' (ECDSA) to the list of known hosts.
root@43.254.16.228's password:
labssh.pub 100% 743 93.3KB/s 00:00
二.將公鑰傳到Linux,並將用私鑰登入
\\在Linux主機上面,要先把剛剛傳來的pub key寫入authorized_keys裡面,再設定權限
root@lab-ubuntu2204:~# cat ~/.ssh/labssh.pub >> ~/.ssh/authorized_keys
root@lab-ubuntu2204:~# chmod 700 ~/.ssh/
root@lab-ubuntu2204:~# chmod 600 ~/.ssh/*
\\登入工具可以用xshell或putty,我選用xshell,並將pub key匯入以登入
![]() |
登入時選「Public Key」>「瀏覽」>「使用者金鑰」 |
![]() |
瀏覽到金鑰放置的路徑,把.ssh的pub key匯進來 |
![]() |
接下來就選這支pub key,以後登入就不用敲密碼了 |
\\最後到Linux主機上的/etc/ssh/sshd_condfig,將PermitRootLogin yes設定給關閉(還要重啟sshd),就可以禁止root用密碼登入
其實也沒有所謂絕對安全的方法,ssh key登入的方式是比較密碼稍微安全一點,若私鑰搞丟或流出的話,也是會有很多麻煩的。
沒有留言:
張貼留言