迹忆客 EN >

当前位置:主页 > 学无止境 > 网络 >

如何使用 SFTP 与远程服务器传输文件

作者:迹忆客 最近更新:2023/01/08 浏览次数:

《如何使用 SFTP 与远程服务器进行交互》 这篇文章中,我们介绍了 SFTP 与服务器进行连接和交互的方式。下面我们主要看一下,在建立了连接之后,该如何进行主要的操作——文件传输。

使用 SFTP 传输文件

如果我们想从远程主机下载文件,可以使用 get 命令:

sftp> get remoteFile

下载过程以及下载成功的输出内容如下

Fetching /home/demouser/remoteFile to remoteFile
/home/demouser/remoteFile                       100%   37KB  36.8KB/s   00:

可以看到,默认情况下,get 命令将远程文件下载到本地文件系统上的同名文件中。

我们可以通过在之后指定名称来将远程文件复制成不同的名称:

sftp> get remoteFile localFile

get 命令还可以跟一些参数。 例如,我们可以通过指定递归选项(-r)来复制目录及其所有内容:

sftp> get -r someDirectory

我们可以使用 -P 或 -p 标志告诉 SFTP 保留适当的权限和访问时间:

sftp> get -Pr someDirectory

将本地文件传输到远程系统 (上传文件)

将文件传输到远程系统的工作方式和下载相同,但是我们要使用 put 命令:

sftp> put localFile

上传过程以及上传成功的输出内容如下

Uploading localFile to /home/demouser/localFile
localFile                                     100% 7607     7.4KB/s   00:00

与 get 一起使用的选项也适用于 put。 所以要复制整个本地目录,可以运行 put -r

sftp> put -r localDirectory

下载和上传文件时有用的一种熟悉的工具是 df 命令,它的工作方式与命令行中的类似。 使用它,我们可以检查是否有足够的空间来完成我们的传输:

sftp> df -h

输出结果如下

Size     Used    Avail   (root)    %Capacity
  19.9GB   1016MB   17.9GB   18.9GB           4%

注意,此命令不能直接用于查看本地的空间,但我们可以通过使用 ! 命令。

命令将我们放入本地 shell,我们可以在其中运行本地系统上可用的任何命令。 我们可以通过键入以下内容来检查磁盘使用情况:

sftp> !

然后再使用下面的命令即可

$ df -h

这时就可以查看我们的本地系统的空间了

Filesystem      Size   Used  Avail Capacity  Mounted on
/dev/disk0s2   595Gi   52Gi  544Gi     9%    /
devfs          181Ki  181Ki    0Bi   100%    /dev
map -hosts       0Bi    0Bi    0Bi   100%    /net
map auto_home    0Bi    0Bi    0Bi   100%    /home

任何其他本地命令都将正常工作。 要返回我们的 SFTP 会话,使用下面的命令:

$ exit

现在应该会看到 SFTP 提示符,表示我们已经返回了。


使用 SFTP 进行简单的文件操作

SFTP 允许我们执行某些类型的文件系统管理。 例如,可以使用以下命令更改远程系统上文件的所有者:

sftp> chown userID file

请注意,与系统 chmod 命令不同,SFTP 命令不接受用户名,而是使用 UID。 不幸的是,没有内置方法可以从 SFTP 界面中了解适当的 UID。

作为一种解决方法,可以从 /etc/passwd 文件中读取,该文件在大多数 Linux 环境中将用户名与 UID 相关联:

sftp> get /etc/passwd
sftp> !less passwd

输出结果如下所示

root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
sys:x:3:3:sys:/dev:/bin/sh
sync:x:4:65534:sync:/bin:/bin/sync
games:x:5:60:games:/usr/games:/bin/sh
man:x:6:12:man:/var/cache/man:/bin/sh
. . .

注意上面的第二个命令,不用单独使用 ! 命令,我们使用它作为本地 shell 命令的前缀。 这可以运行我们本地机器上可用的任何命令,并且之前可以与本地 df 命令一起使用。

UID 将位于文件的第三列,由冒号字符表示。

同样,我们可以通过以下方式更改文件的组所有者:

sftp> chgrp groupID file

同样,没有内置方法可以获取远程系统组的列表。 我们可以使用以下命令解决这个问题:

sftp> get /etc/group
sftp> !less group

输出结果如下

root:x:0:
daemon:x:1:
bin:x:2:
sys:x:3:
adm:x:4:
tty:x:5:
disk:x:6:
lp:x:7:
. . .

第三列包含与第一列中名称关联的组的 ID。 这就是我们要寻找的。

chmod SFTP 命令在远程文件系统上正常工作:

sftp> chmod 777 publicFile
Changing mode on /home/demouser/publicFile

没有对等的操作本地文件权限的命令,但是可以设置本地的 umask ,这样任何复制到本地系统的文件都会有相应的权限。

这可以通过 lumask 命令完成:

sftp> lumask 022
Local umask: 022

现在所有下载的常规文件(只要不使用 -p 标志)将具有 644 权限。

SFTP 还允许我们分别使用 lmkdirmkdir 在本地和远程系统上创建目录。

其余文件命令仅针对远程文件系统:

sftp> ln
sftp> rm
sftp> rmdir

这些命令复制了它们的 shell 等效命令的核心功能。 如果需要在本地文件系统上执行这些操作,请记住可以通过使用以下命令进入 shell:

sftp> !

或者通过在命令前加上 !,从而可以在本地系统上执行单个命令 像这样:

sftp> !chmod 644 somefile

完成 SFTP 会话后,使用 exit 或 bye 关闭连接。

sftp> bye

总结

尽管 SFTP 语法远不如现代 shell 工具全面,但它对于提供与传统 FTP 语法的兼容性或谨慎限制某些环境的远程用户可用的功能是很有帮助的。

例如,我们可以使用 SFTP 使特定用户能够在没有 SSH 访问权限的情况下传输文件。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Get the sum of multiple columns in MySQL

发布时间:2025/04/23 浏览次数:126 分类:MySQL

In this article, we will learn how to sum multiple columns in MySQL. Sum multiple columns in MySQL You can use aggregate functions SUM() to calculate the total value in a collection. SUM() The function calculation does not consider NULL val

MySQL gets the data for the last 30 days

发布时间:2025/04/23 浏览次数:140 分类:MySQL

This article describes how to use SQL query to obtain the data records of the last 30 days from the database. Import data into the database First, let's import some sample data into the database using SQL query. Before we import the dataset

Get command history in MySQL

发布时间:2025/04/23 浏览次数:196 分类:MySQL

This article highlights various ways to get a history of MySQL commands we have executed in Windows and Linux. MySQL Command History We will use Windows and Linux (Ubuntu 20.04) operating systems to understand different ways to get MySQL co

Get the last inserted ID using PHP MySQLi function

发布时间:2025/04/22 浏览次数:100 分类:MySQL

This article briefly introduces the PHP mysqli() function and demonstrates how to use it to get the last inserted ID from a MySQL database. PHP mysqli() Function It is an extended version of the MySQL driver called mysqli and is typically u

Get the current date and time in MySQL

发布时间:2025/04/22 浏览次数:95 分类:MySQL

In this article, we will learn about NOW(), CURRENT_TIMESTAMP() (also written as CURRENT_TIMESTAMP), and SYSDATE() to get the current date and time in MySQL. We will also see a comparison between these three functions. Get the current date

Loop PHP MySQLi Get Array Function

发布时间:2025/04/21 浏览次数:105 分类:MySQL

MySQLi fetch function is used to access data from the database server. After fetching the data, you can also iterate over it MySQLi with queries. In this article, we will see mysqli_fetch_array() the use of functions and methods to iterate

Getting Timestamp in Bash

发布时间:2025/04/21 浏览次数:148 分类:OPERATING SYSTEM

This article discusses the date Bash command used to obtain the system date/time and UNIX timestamp. Get Timestamp Using date Command in Bash The Linux terminal uses the date command to print the current date and time. The simplest version

Get the current branch in Git

发布时间:2025/04/20 浏览次数:58 分类:OPERATING SYSTEM

This article describes how to use git branch the command and git symbolic-ref the command to get the branch you are currently working on in git. Get the current branch Use git branch the command to get a list of all branches. The branch nam

Use TypeScript's getElementById replacement in Angular

发布时间:2025/04/18 浏览次数:196 分类:Angular

This tutorial guide provides a brief description of replacing AngularJS with TypeScript document.getElementById . This also provides getElementById the best practices for using in Angular with code examples. We will also see the use of DOM

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便