linux使用ftp命令_如何在Linux上使用FTP命令

linux使用ftp命令

linux使用ftp命令

Stylized Linux terminal prompt
Fatmawati Achmad Zaenuri/Shutterstock.com Fatmawati Achmad Zaenuri / Shutterstock.com

The File Transfer Protocol is older than most of our readers, but it’s still going strong. FTP doesn’t have the security of a modern protocol, but you may need to use it anyway. Here’s how to do it.

文件传输协议比大多数读者都老,但是它仍然很强大。 FTP不具有现代协议的安全性,但是您仍然可能需要使用它。 这是操作方法。

警告:请勿通过Internet使用FTP (Warning: Don’t Use FTP Over the Internet)

Let’s make this clear right from the outset: The File Transfer Protocol (FTP) dates back to the early 1970s and was written without any regard to security. It does not use encryption for anything. Login credentials like your username and password, as well as the data you download or upload, are transferred in clear text. Anyone along the way can view your secrets. However, FTP still has its uses.

让我们从一开始就明确指出这一点:文件传输协议(FTP)的历史可以追溯到1970年代初,其编写与安全无关。 它不对任何内容使用加密。 登录凭据(如用户名和密码)以及您下载或上传的数据以明文形式传输。 沿途任何人都可以查看您的秘密。 但是,FTP仍然有其用途。

If you’re transferring files within your network, you should be safe–as long as no one on the network is packet-sniffing and eavesdropping on any sensitive documents as you transfer them. If your files aren’t confidential or sensitive in any way, moving them around your internal network with FTP should be fine. Linux has the standard ftp command line program to deal with precisely that scenario.

如果要在网络内传输文件,则应该安全—只要网络上没有人在传输敏感文件时监听和窃听任何敏感文件。 如果您的文件不以任何方式保密或敏感,则可以使用FTP在内部网络中移动它们。 Linux有标准的ftp 命令行程序来处理这种情况。

But definitely don’t use the ftp command to access external resources across the internet. For that, use the sftp command line program, which uses the secure SSH File Transfer Protocol. We’ll introduce both of these programs in this tutorial.

但是绝对不要使用ftp命令通过Internet访问外部资源。 为此,请使用sftp 命令行程序 ,该程序使用安全的SSH文件传输协议。 在本教程中,我们将介绍这两个程序。

To clarify just why you never want to use FTP over the Internet, take a look at the below screenshot. It shows the FTP password in plaintext. Anyone on your network or between you and the FTP server on the Internet can easily see the password is “MySecretPassword.”

为了弄清为什么您永远不想通过Internet使用FTP,请看以下屏幕截图。 它以纯文本形式显示FTP密码。 网络上或您与Internet上的FTP服务器之间的任何人都可以轻松地看到密码为“ MySecretPassword”。

Without the encryption, a malicious actor could modify files you’re downloading or uploading in transit, too.

如果没有加密,恶意参与者也可能会修改您正在传输中正在下载或上传的文件。

Network packet trace with clear text password

ftp命令 (The ftp Command)

Assuming you have a valid account on an FTP site, you can connect to it with the following command. Throughout this article, substitute the IP address in the commands with the IP address of the FTP server you’re connecting to.

假设您在FTP站点上具有有效的帐户,则可以使用以下命令连接到该帐户。 在本文中,将命令中的IP地址替换为您要连接的FTP服务器的IP地址。

ftp  192.168.4.25

Warning: You should only use the ftp command to connect to servers on a trusted local network. Use the sftp command, covered below, for transferring files over the internet.

警告 :仅应使用ftp命令连接到受信任的本地网络上的服务器。 使用下面介绍的sftp命令通过Internet传输文件。

ftp connection command in a terminal window

The FTP server responds with a welcome message. The wording of the greeting will vary from server to server. It then asks for the username of the account you are logging into.

FTP服务器以欢迎消息响应。 问候语将因服务器而异。 然后,它要求您登录的帐户的用户名。

Notice that the IP  address of the site you’re connecting to is displayed, followed by your Linux user name. If your account name on the FTP server is the same as your Linux user name, simply press the Enter key. This will use your Linux user name as the account name on the FTP server. If your Linux user name and the FTP account name are different, type in the FTP account user name and then press Enter.

请注意,将显示您要连接的站点的IP地址,后跟您Linux用户名。 如果您在FTP服务器上的帐户名与Linux用户名相同,只需按Enter键。 这将使用您Linux用户名作为FTP服务器上的帐户名。 如果您Linux用户名和FTP帐户名不同,请输入FTP帐户用户名,然后按Enter。

登录FTP服务器 (Logging In to the FTP Server)

You will be prompted to enter your password for the FTP site. Enter your password and press Enter. Your password is not displayed on the screen. If your FTP user account name and password combination are verified by the FTP server, you are then logged into the FTP server.

系统将提示您输入FTP站点的密码。 输入密码,然后按Enter。 您的密码未显示在屏幕上。 如果FTP服务器验证了您的FTP用户帐户名和密码组合,则您将登录到FTP服务器。

You will be presented with the ftp> prompt.

将会显示ftp>提示符。

logged in ftp account in a terminal window

环顾四周并检索文件 (Looking Around and Retrieving Files)

First, you’ll probably want to get a listing of the files on the FTP server. The ls command does just that. Our user sees the file gc.c is on the FTP server, and he wants to download it to his own computer. His computer is the “local computer” in FTP parlance.

首先,您可能需要获取FTP服务器上文件的列表。 ls命令就是这样做的。 我们的用户看到文件gc.c在FTP服务器上,并且想要将其下载到自己的计算机上。 用FTP的话来说,他的计算机是“本地计算机”。

The command to retrieve (or “get”) a file is get.  Our user, therefore, issues the command get gc.c. They type get, a space, and then the name of the file they wish to retrieve.

检索(或“获取”)文件的命令是get 。 因此,我们的用户发出命令get gc.c 他们输入get ,一个空格,然后输入他们想要检索的文件名。

The FTP server responds by transferring the file to the local computer and confirming the transfer took place. The size of the file and the time it took to transfer are also shown.

FTP服务器通过将文件传输到本地计算机并确认传输已进行响应。 还显示文件的大小以及传输所需的时间。

ls
get gc.c
ftp file transfer in a terminal window

To retrieve multiple files at once, use the mget (multiple get) command. The mget command will ask you to confirm whether you want to download each file in turn. Respond by pressing “y” for yes and “n” for no.

要一次检索多个文件,请使用mget (多次获取)命令。 mget命令将要求您确认是否要依次下载每个文件。 通过按“ y”(是)和“ n”(否)进行响应。

This would be tedious for a great number of files. Because of this, collections of related files are usually stored on ftp sites as single tar.gz or tar.bz2 files.

这对于大量文件而言将是乏味的。 因此,相关文件的集合通常作为单个tar.gz或tar.bz2文件存储在ftp站点上。

mget *.c
mget command in a terminal window

上传文件到FTP服务器 (Uploading Files to the FTP Server)

Depending on the permissions that have been granted to your FTP account you might be able to upload (or “put”) files to the server. To upload a file, use the put command. In our example, the user is uploading a file called Songs.tar.gz to the FTP server.

根据已授予您的FTP帐户的权限,您也许可以将文件上传(或“放入”)到服务器。 要上传文件,请使用put命令。 在我们的示例中,用户正在将一个名为Songs.tar.gz的文件上传到FTP服务器。

put Songs.tar.gz
put command in a terminal window

As you probably expect, there is a command to put multiple files to the FTP server at once. It is called mput (multiple put). Just like the mget command did, mput will ask for a “y” or “n” confirmation for the uploading of each file, one by one.

如您所料,有一个命令可以将多个文件一次放入FTP服务器。 它称为mput (多次放置)。 就像mget命令所做的一样, mput将要求对每个文件的上载进行一次“ y”或“ n”确认。

The same argument for putting sets of files into tar archives applies for putting files as it does for getting files. Our user is uploading multiple “.odt” files with the following command:

将文件集放入tar归档文件的相同论点适用于将文件放入,与获取文件一样。 我们的用户正在使用以下命令上传多个“ .odt”文件:

mput *.odt
mput command in a terminal window

创建和更改目录 (Creating and Changing Directories)

If your user account on the ftp server permits it, you may be able to create directories. The command to do this is mkdir . To be clear, any directory you create with the mkdir command will be created on the ftp server and not on your local computer.

如果您在ftp服务器上的用户帐户允许,则可以创建目录。 执行此命令的命令是mkdir 。 为了清楚mkdir ,使用mkdir命令创建的任何目录都将在ftp服务器上而不是在本地计算机上创建。

To change directories on the ftp server, use the cd command. When you use the cd command the ftp> prompt will not change to reflect your new current directory. The pwd (print working directory) command will show you your current directory.

要更改ftp服务器上的目录,请使用cd命令。 使用cd命令时, ftp>提示符不会更改以反映您的新当前目录。 pwd (打印工作目录)命令将显示您当前的目录。

Our ftp user creates a directory called music, changes into that new directory, confirms where they are by using the pwd command then uploads a file to that directory.

我们的ftp用户创建一个名为Music的目录,切换到新目录,使用pwd命令确认它们的位置,然后将文件上传到该目录。

mkdir music
cd music
pwd
put songs.tar.gz
cd pwd and mkdir commands in a terminal window

To quickly moved to the parent directory of the current directory use the cdup command.

要快速移动到当前目录的父目录,请使用cdup命令。

cdup
cdup command in a terminal widnowindow

访问本地计算机 (Accessing the Local Computer)

To change the directory on the local computer, you can use the lcd command at the ftp> prompt. It is, however, easy to lose track of where you are in the local filesystem. A more convenient method of accessing the local filesystem is to use the ! command.

要更改本地计算机上的目录,可以在ftp>提示符下使用lcd命令。 但是,很容易忘记本地文件系统中的位置。 访问本地文件系统的一种更方便的方法是使用! 命令。

The ! command opens a shell window to the local computer. You can do anything in this shell that you can in a standard terminal window. When you type exit you are returned to the ftp> prompt.

! 命令打开本地计算机的外壳程序窗口。 您可以在此Shell中执行任何在标准终端窗口中可以执行的操作。 当您键入exit您将返回到ftp>提示符。

Our user has used the ! command and entered a shell window on the local computer. They have issued an ls command to see what files are present in that directory and then typed exit to return to the ftp> prompt.

我们的用户使用了! 命令,并在本地计算机上输入一个Shell窗口。 他们发出了ls命令,以查看该目录中存在哪些文件,然后键入exit以返回到ftp>提示符。

!
ls
exit
! shell command in a terminal window

重命名文件 (Renaming Files)

To rename files on the FTP server use the rename command. Here our FTP user renames a file with rename and then uses the ls command to list the files in the directory.

要重命名FTP服务器上的文件,请使用rename命令。 在这里我们的FTP用户重命名一个文件rename ,然后使用ls命令列出该目录中的文件。

rename songs.tar.gz rock_songs.tar.gz
ls
rename command in the terminal window

删除文件 (Deleting Files)

To delete files on the FTP server use the delete command. To delete several files at once, use the mdelete command. You will be asked to provide a “y” or “n” confirmation for the deletion of each file.

要删除FTP服务器上的文件,请使用delete命令。 要一次删除多个文件,请使用mdelete命令。 系统将要求您提供“ y”或“ n”确认以删除每个文件。

Here our FTP user has listed the files to see their names and then chosen one to delete. They then decide to delete them all.

在这里,我们的FTP用户列出了文件以查看其名称,然后选择其中一个进行删除。 然后,他们决定全部删除。

ls
delete gc.o
mdelete *.o
ls delete and mdelete commands in a terminal window

使用sftp命令 (Using the sftp Command)

Readers familiar with the IP addressing system will have noticed that the 192.168 address of the FTP server used in the above examples is an internal IP address, also called a private IP address. As we warned at the beginning of this article, the ftp command should only be used on internal networks.

熟悉IP寻址系统的读者会注意到,以上示例中使用的FTP服务器的192.168地址是内部IP地址,也称为专用IP地址。 正如我们在本文开头警告的那样, ftp命令仅应在内部网络上使用。

If you want to connect to a remote or public FTP server use the sftp command. Our user is going to connect to an SFTP account called demo on the publicly accessible FTP server located at test.trebex.net.

如果要连接到远程或公​​共FTP服务器,请使用sftp命令。 我们的用户将连接到位于test.trebex.net上可公共访问的FTP服务器上的名为demo的SFTP帐户。

When they connect, they are informed that the connection has been established. They are also informed that the authenticity of the host cannot be verified. This is normal for the first connection a new host. They press “y” to accept the connection.

当他们连接时,会通知他们已建立连接。 他们还被告知无法验证主机的真实性。 对于新主机的第一次连接,这是正常的。 他们按“ y”接受连接。

Because the user account name (demo) was passed on the command line they are not prompted for the user account name. They are prompted only for the password. This is entered, verified and accepted, and they are presented with the sftp> prompt.

由于用户帐户名( demo )是在命令行中传递的,因此不会提示他们输入用户帐户名。 仅提示他们输入密码。 输入,验证并接受该sftp> ,并以sftp>提示显示。

sftp demo@test.rebex.net
connecting to an sftp site in a terminal window

The FTP commands we have described above will work just the same in an SFTP session, with the following exceptions.

我们上面描述的FTP命令在SFTP会话中的工作原理相同,但以下情况除外。

  • To delete a file use rm (FTP uses delete)

    要删除文件,请使用rm (FTP使用delete )

  • To delete multiple files use rm (FTP uses mdelete)

    要删除多个文件,请使用rm (FTP使用mdelete )

  • To move to the parent directory use cd .. (FTP uses cdup)

    要移至父目录,请使用cd .. (FTP使用cdup )

Our user has used a few commands in their SFTP session. They have use ls to list the files, and cd to change into the pub directory. They have used the pwd to print the working directory.

我们的用户在SFTP会话中使用了一些命令。 他们使用ls列出文件,使用cd进入pub目录。 他们已使用pwd打印工作目录。

sftp commands in a terminalwindow

There are other options to transfer files in the Linux world, notably scp (secure copy), but we’ve focused on FTP and SFTP here. Used in the applicable scenarios these two commands will serve you and your file storage and retrieval needs well.

在Linux世界中,还有其他选项可以传输文件,特别是scp ( 安全副本 ),但是这里我们重点介绍FTP和SFTP。 在适用的场景中使用这两个命令将为您提供服务,并且文件存储和检索需求也很好。

翻译自: https://www.howtogeek.com/412626/how-to-use-the-ftp-command-on-linux/

linux使用ftp命令

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值