ubuntu ssh 免密碼

  • Post author:
  • 帖子最後修改:2021 年 8 月 7 日
  • Post comments:0評論

1. 第一步工作站上生成 RSA 密鑰。

				
					ssh-keygen -t rsa -b 4096
				
			

這將要求一個位置來保存 RSA 密鑰。

				
					Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
				
			

按ENTER到主.ssh目錄內目錄上的默認位置

下一個提示要求輸入主觀長度的密碼短語以保護您的私鑰。ENTER如果您不需要密碼短語,請按將其留空。如果您提供任何密碼,我們每次使用私鑰時都必須使用它,作為額外的安全措施。

				
					Enter passphrase (empty for no passphrase):
Enter same passphrase again:
				
			

輸出將是

				
					Your identification has been saved in /home/user/.ssh/id_rsa
Your public key has been saved in /home/user/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:zxczxczxczxczxczxczxczxczxczxc user@user_server
The key's randomart image is:
+---[RSA 4096]----+
|      .o.oo==*oo.|
|     .o o =o+o=+=|
|    .o.+ o ..o.*+|
|   o..o .   . o.o|
|    +.  C       o|
|   o=  .         |
|   K..o          |
|    ++..         |
|   o+=o          |
+----[SHA256]-----+
				
			

檢查.ssh隱藏目錄,我們可以看到裡面有 3 個文件。

				
					ll .ssh
total 8.0K
-rw------- 1    0 Nov 25 03:02 authorized_keys
-rw------- 1 3.4K Nov 29 09:03 id_rsa
-rw-r--r-- 1  756 Nov 29 09:03 id_rsa.pub
				
			
  • id_rsa: 私鑰。不要分享這個文件!
  • id_rsa.pub:關聯的公鑰。這個可以共享

2. 將您的公共 SSH 密鑰複製到服務器

				
					cat ~/.ssh/id_rsa.pub | ssh user@remote_host "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
				
			

提示要求輸入遠程服務器的密碼

				
					The authenticity of host 'xx.xxx.xx.xxx (xx.xxx.xx.xxx)' can't be established.
ECDSA key fingerprint is SHA256:asdasdasdasdasdasdasdasdasd.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'xx.xxx.xx.xxx' (ECDSA) to the list of known hosts.
user@xx.xxx.xx.xxx's password:
				
			

作為額外的步驟,正確訪問遠程主機密鑰文件和 ssh 文件夾

				
					#on 遠程主機
chmod 700 .ssh 
chmod 600 .ssh/authorized_keys
				
			

設定檔

				
					Port 22
MaxAuthTries 3
PermitRootLogin no
ChallengeResponseAuthentication no
UsePAM yes
				
			

將 檔案/資料夾 從本地拷至遠端 Ubuntu 機(scp)

				
					$scp -r localfile.txt username@192.168.0.1:/tmp
				
			

將 檔案/資料夾 從遠端 Ubuntu 機拷至本地(scp) ./ 為當前目錄

				
					$scp -r username@192.168.0.1:/home/username/remotefile.txt ./
				
			

發佈留言