The file transfer protocol is older than most of our readers, but it continues to work. FTP does not have the security of a modern protocol, but you may need to use it anyway. Here's how to do it.
Warning: Do not use FTP on the Internet
Let's be clear from the start: File Transfer Protocol (FTP) dates back to the early 1970s and was written without any consideration for security. It does not use encryption for nothing. Login credentials such as your username and password, as well as the data you upload or transfer, are transferred in clear text. All the way can see your secrets. However, FTP still has its uses.
If you are transferring files to your network, you should be safe, as long as no one on the network detects packets and listens for sensitive documents when they are forwarded. If your files are not confidential or sensitive in any way, it is not difficult to move them to your internal network via FTP. Linux has the standard ftp command line program to accurately deal with this scenario.
But certainly do not use the ftp command to access external resources on the Internet. To do this, use the sftp command line program, which uses secure SSH file transfer protocol. We will introduce these two programs in this tutorial.
To understand why you never want to use FTP over the Internet, take a look at the screen shot below. It shows the FTP password in plain text. Anyone on your network or between you and the FTP server on the Internet can easily see that the password is "MySecretPassword".
Without encryption, a malicious actor could also modify files that you download or download in transit.
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, replace the IP address of the commands with the IP address of the FTP server to which you are connecting.
WarningNote: You must use the ftp command only to connect to servers on a trusted local area network. Use the sftp command, described below, to transfer files over the Internet.
The FTP server responds with a welcome message. The wording of the greeting varies from one server to the other. It then asks you for the name of the user of the account to which you are connecting.
Note that the IP address of the site you are connecting to is displayed, followed by your Linux user name. If the name of your account on the FTP server is the same as your Linux user name, just hit the Enter key. This will use your Linux user name as the account name on the FTP server. If your Linux user name and FTP account name are different, type the user name of the FTP account, and then press Enter.
Connect 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 the username / password combination of your FTP account is verified by the FTP server, you are then connected to the FTP server.
The ftp prompt will be presented to you.
Watch and recover files
First, you will probably want to get a list of files on the FTP server. That's exactly what the ls command does. Our user sees that the file gc.c is on the FTP server and wants to download it on his own computer. His computer is "the local computer" in 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 that they wish to retrieve.
The FTP server responds by transferring the file to the local computer and confirming that the transfer has occurred. The size of the file and the time required for the transfer are also indicated.
To recover 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. Answer by pressing "y" for yes and "n" for no.
This would be tedious for a large number of files. For this reason, linked file collections are typically stored on ftp sites as tar.gz or tar.bz2 files.
mget * .c
Downloading files to the FTP server
Depending on the permissions granted to your FTP account, you may be able to download (or "upload") files to the server. To download a file, use the put command. In our example, the user uploads a file called Songs.tar.gz to the FTP server.
As you probably guess, a command allows you to place several files simultaneously on the FTP server. This is called mput (multiple put). Just like the mget command, mput will ask for a "y" or "n" confirmation to download each file one by one.
The same argument for placing sets of files in tar archives applies as for obtaining files. Our user downloads several ".odt" files with the following command:
mput * .odt
Creating and editing directories
If your user account on the FTP server allows 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.
To change the directory 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 job directory) command will show you your current directory.
Our ftp user creates a directory called music, modifies them in this new directory, confirms their location with the help of the pwd command, and then uploads a file to this directory.
To quickly move to the parent directory of the current directory, use the cdup command.
Access the local computer
To change the directory on the local computer, you can use the lcd command at the ftp> prompt. It's easy to lose track of where you are in the local file system. A more convenient way to access the local file system is to use the file! order.
The ! This command opens a shell window on the local computer. You can do everything you can in this shell in a standard terminal window. When you type exit, you are returned to the ftp> prompt.
Our user used the! and entered a shell window on the local computer. They issued an ls command to see which files are in that directory, and then typed exit to return to the ftp> prompt.
To rename files on the FTP server, use the Rename command. Here, our FTP user renames a file by renaming it, and then uses the ls command to list the files in the directory.
rename songs.tar.gz rock_songs.tar.gz
To delete files on the FTP server, use the delete command. To delete multiple files at once, use the mdelete command. You will be asked to provide a "y" or "n" confirmation for deleting each file.
Here, our FTP user listed the files to see their names, then chose one to delete. They then decide to delete them all.
mdelete * .o
Use 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 examples above 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.
If you want to connect to a remote or public FTP server, use the sftp command. Our user will connect to an SFTP account called demo on the public FTP server located on test.trebex.net.
When they connect, they are informed that the connection has been established. They are also informed that the authenticity of the host can not be verified. This is normal for the first connection with a new host. They press "y" to accept the connection.
Since the name of the user account (demo) was passed on the command line, the user account name is not requested. They are only invited for the password. This is entered, verified and accepted, and the sftp> prompt is presented to them.
The FTP commands described above work exactly the same way in an SFTP session, with the following exceptions.
To delete a file, use rm (FTP uses delete)
To delete multiple files, use rm (FTP uses mdelete)
To switch to the parent directory, use cd .. (FTP uses cdup)
Our user has used some commands in his SFTP session. They used ls to list the files and cd to switch to the pub directory. They used the pwd to print the working directory.
There are other options for transferring files to the Linux world, including scp (secure copy), but we focused on FTP and SFTP here. Used in the applicable scenarios, these two commands will serve you well and will meet your storage and file recovery needs.