📅  最后修改于: 2023-12-03 15:06:38.954000             🧑  作者: Mango
在Shell-Bash中,我们可以使用scp
命令从远程服务器复制文件。scp
的语法为:
scp [options] [source] [destination]
其中,options
为选项(例如,-r
表示递归复制目录),source
为源文件或目录,destination
为目标文件或目录。
下面是一个从远程服务器复制文件的例子:
scp username@remote:/path/to/file /path/to/destination
其中,username
为远程服务器的用户名,remote
为远程服务器的地址,/path/to/file
为要复制的文件的路径,/path/to/destination
为目标文件的路径。
如果要复制目录,我们需要使用-r
选项。下面是一个从远程服务器复制目录的例子:
scp -r username@remote:/path/to/directory /path/to/destination
其中,username
、remote
、/path/to/directory
和/path/to/destination
的含义与复制文件相同。-r
选项表示递归复制目录。
有时候,我们需要指定远程服务器的SSH端口号。可以使用-P
选项来指定端口号。下面是一个从远程服务器复制文件并指定端口号的例子:
scp -P 2222 username@remote:/path/to/file /path/to/destination
其中,2222
为SSH端口号。-P
选项表示指定端口号。
如果我们在本地生成了一对公钥和私钥,并将公钥上传到了远程服务器上,就可以使用密钥认证来登录到远程服务器。在使用密钥认证的情况下,可以省去输入密码的步骤。
下面是一个使用密钥认证的例子:
scp -i /path/to/private/key username@remote:/path/to/file /path/to/destination
其中,/path/to/private/key
为私钥文件的路径。-i
选项表示使用指定的私钥文件进行认证。
以上就是在Shell-Bash中从远程服务器复制文件或目录的方法。可以根据实际情况选择不同的选项,并使用密钥认证来提高安全性。