第1步:在客戶端主機中生成“密鑰對”。
//查看id_rsa.pub 或 id_dsa.pub是否存在 ls -al ~/.ssh
[root@linuxprobe ~]# ssh-keygen -t rsa
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.
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:
40:32:48:18:e4:ac:c0:c3:c1:ba:7c:6c:3a:a8:b5:22 [email protected]
The key's randomart image is:
+--[ RSA 2048]----+
|+*..o . |
|*.o + |
|o* . |
|+ . . |
|o.. S |
|.. + |
|. = |
|E+ . |
|+.o |
+-----------------+
第2步:把客戶端主機中生成的公鑰文件傳送至遠程主機:
[root@linuxprobe ~]# ssh-copy-id 192.168.10.10 The authenticity of host '192.168.10.20 (192.168.10.10)' can't be established. ECDSA key fingerprint is 4f:a7:91:9e:8d:6f:b9:48:02:32:61:95:48:ed:1e:3f. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys [email protected]'s password:此處輸入遠程服務器密碼 Number of key(s) added: 1 Now try logging into the machine, with: "ssh '192.168.10.10'" and check to make sure that only the key(s) you wanted were added.
第3步:對服務器進行設置,使其只允許密鑰驗證,拒絕傳統的口令驗證方式。記得在修改配置文件後保存並重啓sshd服務程序。
[root@linuxprobe ~]# vim /etc/ssh/sshd_config ………………省略部分輸出信息……………… 74 75 # To disable tunneled clear text passwords, change to no here! 76 #PasswordAuthentication yes 77 #PermitEmptyPasswords no 78 PasswordAuthentication no 79 ………………省略部分輸出信息……………… [root@linuxprobe ~]# systemctl restart sshd
第4步:在客戶端嘗試登錄到服務器,此時無須輸入密碼也可成功登錄。
[root@linuxprobe ~]# ssh 192.168.10.10 Last login: Mon Apr 13 19:34:13 2017
9.2.3 遠程傳輸命令
scp(secure copy)是一個基於SSH協議在網絡之間進行安全傳輸的命令,其格式爲“scp [參數] 本地文件 遠程帳戶@遠程IP地址:遠程目錄”。
與第2章講解的cp命令不同,cp命令只能在本地硬盤中進行文件複製,而scp不僅能夠通過網絡傳送數據,而且所有的數據都將進行加密處理。例如,如果想把一些文件通過網絡從一臺主機傳遞到其他主機,這兩臺主機又恰巧是Linux系統,這時使用scp命令就可以輕鬆完成文件的傳遞了。scp命令中可用的參數以及作用如表9-2所示。
表9-2 scp命令中可用的參數及作用
參數 | 作用 |
-v | 顯示詳細的連接進度 |
-P | 指定遠程主機的sshd端口號 |
-r | 用於傳送文件夾 |
-6 | 使用IPv6協議 |
在使用scp命令把文件從本地複製到遠程主機時,首先需要以絕對路徑的形式寫清本地文件的存放位置。如果要傳送整個文件夾內的所有數據,還需要額外添加參數-r進行遞歸操作。然後寫上要傳送到的遠程主機的IP地址,遠程服務器便會要求進行身份驗證了。當前用戶名稱爲root,而密碼則爲遠程服務器的密碼。如果想使用指定用戶的身份進行驗證,可使用用戶名@主機地址的參數格式。最後需要在遠程主機的IP地址後面添加冒號,並在後面寫上要傳送到遠程主機的哪個文件夾中。只要參數正確並且成功驗證了用戶身份,即可開始傳送工作。由於scp命令是基於SSH協議進行文件傳送的,而9.2.2小節又設置好了密鑰驗證,因此當前在傳輸文件時,並不需要賬戶和密碼。
[root@linuxprobe ~]# echo "Welcome to LinuxProbe.Com" > readme.txt [root@linuxprobe ~]# scp /root/readme.txt 192.168.10.20:/home [email protected]'s password:此處輸入遠程服務器中root管理員的密碼 readme.txt 100% 26 0.0KB/s 00:00
此外,還可以使用scp命令把遠程主機上的文件下載到本地主機,其命令格式爲“scp [參數] 遠程用戶@遠程IP地址:遠程文件 本地目錄”。例如,可以把遠程主機的系統版本信息文件下載過來,這樣就無須先登錄遠程主機,再進行文件傳送了,也就省去了很多周折。
[root@linuxprobe ~]# scp 192.168.10.20:/etc/redhat-release /root [email protected]'s password:此處輸入遠程服務器中root管理員的密碼 redhat-release 100% 52 0.1KB/s 00:00 [root@linuxprobe ~]# cat redhat-release Red Hat Enterprise Linux Server release 7.0 (Maipo)