Linux運維工程師必備知識 – SCP端口的設置和使用

一、SCP是什麼?

SCP(Secure Copy)是一個在Linux系統中使用的基於SSH協議進行加密傳輸的文件傳輸工具,它可以安全地在本地系統和遠程系統間複製文件。

通過SCP命令可以將本地的文件複製到遠程服務器上,或者將遠程服務器上的文件複製到本地,它的使用方式和Linux中常用的cp命令非常相似。

二、SCP的基本用法

SCP的基本用法非常簡單,下面是一些常用的SCP命令:

# 將文件從本地複製到遠程服務器
scp /path/to/local/file username@remote:/path/to/remote/directory

# 將文件從遠程服務器複製到本地
scp username@remote:/path/to/remote/file /path/to/local/directory

# 將整個目錄從本地複製到遠程服務器
scp -r /path/to/local/directory username@remote:/path/to/remote/directory

# 將整個目錄從遠程服務器複製到本地
scp -r username@remote:/path/to/remote/directory /path/to/local/directory

上述命令中,/path/to/local/file是本地文件的路徑,username是遠程服務器的用戶名,remote是遠程服務器的IP地址或域名,/path/to/remote/directory是文件存放的目錄。

其中,-r選項表示遞歸複製整個目錄(包括子目錄)。

三、SCP使用不同的端口

默認情況下,SCP使用的端口是22,如果遠程服務器使用的是非標準的SSH端口,我們需要指定SCP使用的端口。

下面是指定SCP使用不同端口的命令:

# 將文件從本地複製到遠程服務器(指定端口)
scp -P 2222 /path/to/local/file username@remote:/path/to/remote/directory

# 將文件從遠程服務器複製到本地(指定端口)
scp -P 2222 username@remote:/path/to/remote/file /path/to/local/directory

# 將整個目錄從本地複製到遠程服務器(指定端口)
scp -r -P 2222 /path/to/local/directory username@remote:/path/to/remote/directory

# 將整個目錄從遠程服務器複製到本地(指定端口)
scp -r -P 2222 username@remote:/path/to/remote/directory /path/to/local/directory

上述命令中,-P選項指定SCP使用的端口號。

四、使用SSH配置文件

如果我們需要頻繁地使用SCP進行文件傳輸,每次都輸入完整的命令比較麻煩,我們可以使用SSH配置文件進行簡化。

下面是一個簡單的SSH配置文件,可以指定遠程服務器的IP地址、用戶名、端口號等信息:

Host remote
    Hostname 192.168.0.100
    User username
    Port 2222

上述配置文件中,remote是主機別名,可以是任意名稱,Hostname指定遠程服務器的IP地址或域名,User指定用戶名,Port指定端口號。

使用SSH配置文件後,我們可以簡化SCP命令為:

# 將文件從本地複製到遠程服務器
scp /path/to/local/file remote:/path/to/remote/directory

# 將文件從遠程服務器複製到本地
scp remote:/path/to/remote/file /path/to/local/directory

# 將整個目錄從本地複製到遠程服務器
scp -r /path/to/local/directory remote:/path/to/remote/directory

# 將整個目錄從遠程服務器複製到本地
scp -r remote:/path/to/remote/directory /path/to/local/directory

上述命令中,remote是配置文件中指定的主機別名。

五、總結

SCP是Linux系統中常用的文件傳輸工具,它可以安全地在本地系統和遠程系統間複製文件。使用SCP命令可以將本地的文件複製到遠程服務器上,或者將遠程服務器上的文件複製到本地。通過指定端口或使用SSH配置文件,可以方便地進行文件傳輸。

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/249437.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-12 17:14
下一篇 2024-12-12 17:14

相關推薦

發表回復

登錄後才能評論