1. 程式人生 > >linux ssh免密登入/scp命令

linux ssh免密登入/scp命令

● 生成本機的密碼(私鑰和公鑰)

#一直回車下去即可(敲三下)
[[email protected] ~]# ssh-key
ssh-keygen   ssh-keyscan  
[[email protected] ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? 
[[email protected]
~]#

● 拷貝公鑰至需要遠端登入的伺服器

[[email protected] .ssh]# scp ./id_rsa.pub 192.168.0.22:/root/.ssh/id.11
[email protected]'s password: 
id_rsa.pub                                                                                                                                                                                                                                  100%  408     0.4KB/s   00:00    
[
[email protected]
.ssh]# ll total 16 -rw-r--r-- 1 root root 408 Oct 26 16:17 id.11 -rw------- 1 root root 1679 Oct 26 15:20 id_rsa -rw-r--r-- 1 root root 408 Oct 26 15:20 id_rsa.pub -rw-r--r-- 1 root root 174 Oct 26 16:03 known_hosts [[email protected] .ssh]# pwd /root/.ssh

● 若/root/.ssh目錄下無authorized_keys,則手動建立一個檔案

[[email protected] .ssh]# cat id.11 >> authorized_keys 

● 此時連線遠端伺服器無需再輸入密碼

[[email protected] .ssh]# ssh 192.168.0.22
Last failed login: Fri Oct 26 16:15:25 GST 2018 from 192.168.0.22 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Fri Oct 26 16:04:02 2018 from 192.168.0.22
[[email protected] ~]# 

● 將遠端伺服器檔案拷貝至本地

[[email protected] ~]# scp -r 192.168.0.22:/root/hello ./
hello 

● 將本地檔案拷貝至遠端伺服器

#a 本地當前目錄檔案或目錄
[[email protected] ~]# scp -r a 192.168.0.22:/root
a  

● 當遠端伺服器預設埠(22)發生改變,則如下:

scp -P 埠號 -r a 192.168.0.22:/root

● scp 一些引數詳解

-1: 強制scp命令使用協議ssh1
-2: 強制scp命令使用協議ssh2
-4: 強制scp命令只使用IPv4定址
-6: 強制scp命令只使用IPv6定址
-B: 使用批處理模式(傳輸過程中不詢問傳輸口令或短語)
-C: 允許壓縮。(將-C標誌傳遞給ssh,從而開啟壓縮功能)
-p:保留原檔案的修改時間,訪問時間和訪問許可權。
-q: 不顯示傳輸進度條。
-r: 遞迴複製整個目錄。
-v:詳細方式顯示輸出。scp和ssh(1)會顯示出整個過程的除錯資訊。這些資訊用於除錯連線,驗證和配置問題。
-c cipher: 以cipher將資料傳輸進行加密,這個選項將直接傳遞給ssh。
-F ssh_config: 指定一個替代的ssh配置檔案,此引數直接傳遞給ssh。
-i identity_file: 從指定檔案中讀取傳輸時使用的金鑰檔案,此引數直接傳遞給ssh。
-l limit: 限定使用者所能使用的頻寬,以Kbit/s為單位。
-o ssh_option: 如果習慣於使用ssh_config(5)中的引數傳遞方式,
-P port:注意是大寫的P, port是指定資料傳輸用到的埠號
-S program: 指定加密傳輸時所使用的程式。此程式必須能夠理解ssh(1)的選項。