1. 程式人生 > 實用技巧 >Linux系統中遠端傳輸命令scp

Linux系統中遠端傳輸命令scp

linux系統可以實現把檔案通過網路從一臺主機傳輸至另一臺主機,而且所有的資料都進行加密處理。

準備兩臺測試主機pc1和pc2,均為RHEL7,網路連線方式為橋接方式,可以連線外網。

測試基本用法,從pc1向pc2中傳輸資料。

1、登入pc1,檢視IP,測試外網,建立測試目錄、測試資料

[root@pc1 ~]# ifconfig | head -n 3  ## 檢視pc1IP地址
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.20  netmask 255.255
.255.0 broadcast 192.168.3.255 inet6 fe80::20c:29ff:fee4:f7b9 prefixlen 64 scopeid 0x20<link> [root@pc1 ~]# ping -c 3 www.baidu.com ## 測試外網,聯通 PING www.a.shifen.com (39.156.66.14) 56(84) bytes of data. 64 bytes from 39.156.66.14: icmp_seq=1 ttl=51 time=16.7 ms 64 bytes from 39.156.66.14
: icmp_seq=2 ttl=51 time=16.3 ms 64 bytes from 39.156.66.14: icmp_seq=3 ttl=51 time=16.4 ms --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 8041ms rtt min/avg/max/mdev = 16.376/16.541/16.778/0.201 ms [root@pc1 ~]# pwd /root [root@pc1 ~]# mkdir dir1 [root@pc1 ~]# cd dir1/ [root@pc1 dir1]# seq
5 > a.txt [root@pc1 dir1]# mkdir testdir [root@pc1 dir1]# seq 3 > testdir/b.txt [root@pc1 dir1]# tree ## 測試資料及目錄 . ├── a.txt └── testdir └── b.txt 1 directory, 2 files

2、登入pc2,檢視IP地址,測試外網,建立測試目錄

[root@pc2 ~]# ifconfig | head -n 3  ## 檢視pc2IP地址
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.3.13  netmask 255.255.255.0  broadcast 192.168.3.255
        inet6 fe80::20c:29ff:feaa:2b29  prefixlen 64  scopeid 0x20<link>
[root@pc2 ~]# ping -c 3 www.baidu.com  ## 測試外網
PING www.a.shifen.com (39.156.66.18) 56(84) bytes of data.
64 bytes from 39.156.66.18: icmp_seq=1 ttl=51 time=14.6 ms
64 bytes from 39.156.66.18: icmp_seq=2 ttl=51 time=14.4 ms
64 bytes from 39.156.66.18: icmp_seq=3 ttl=51 time=14.4 ms

--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 8037ms
rtt min/avg/max/mdev = 14.413/14.482/14.604/0.086 ms
[root@pc2 ~]# mkdir dir2
[root@pc2 ~]# cd dir2/
[root@pc2 dir2]# pwd  ## 測試目錄
/root/dir2
[root@pc2 dir2]# ls -a
.  ..

3、在pc1主機中,使用scp命令向pc2主機/root/dir2目錄中傳輸檔案a.txt

基本格式:scp [引數] 本地檔案 遠端賬戶@遠端IP地址:遠端目錄

[root@pc1 dir1]# ls
a.txt  testdir
[root@pc1 dir1]# scp a.txt root@192.168.3.13:/root/dir2/  ## 從pc1主機中向pc2主機/root/dir2/目錄中傳輸檔案a.txt
root@192.168.3.13's password: ## 此處輸入pc2的root密碼
a.txt                                                                                                              100%   10     0.0KB/s   00:00
[root@pc1 dir1]# cp a.txt x.txt  ## 將a.txt複製為x.txt,作為另一份測試資料
[root@pc1 dir1]# ls
a.txt  testdir  x.txt
## root使用者情況下,測試不輸入遠端使用者能夠傳輸,可以 [root@pc1 dir1]# scp x.txt
192.168.3.13:/root/dir2/ root@192.168.3.13's password: x.txt 100% 10 0.0KB/s 00:00

4、在pc2/root/dir2/目錄下檢查是否接收到了傳輸的檔案