+5 votes
319 views
How to send a file via FTP from the console in Linux

in Guides by (551k points)
reopened | 319 views

1 Answer

+3 votes
Best answer

How to send a file via FTP from the console in Linux.
Other methods to upload files via FTP from the Linux terminal.
Using cURL:
Using LFTP:

Today we are going to talk about one of those small tasks that bring us a little closer to the Linux terminal and that shows us once again the great functionality of this. We will show you a complete tutorial on how to upload or send a file via FTP from the command console in Linux .

image

We know that dealing with the console is not one of the favorite tasks of users who are approaching the Linux universe for the first time, but this tutorial is suitable for all types of users, both new and advanced. For our tests we have used an Ubuntu Server distribution that we access through SSH with PuTTY , although what is said here is applicable to other Linux distributions.

If you are an advanced user, we understand that you do not need to read the entire explanation and that you only need the following command:

ftp -n <<EOF
open ftp.ejemplo.com
user usuario contraseña
put archivo.tar.gz
EOF

How to send a file via FTP from the console in Linux.

In this first part of the tutorial we will show how to send a file via FTP from the console without installing any additional application or tool. For this, we will use the ftp command that we have available by default.

The ftp command allows users to interact with FTP servers and manage remote server files. In this tutorial we will not collect all the available commands, we will only talk about those necessary to upload a file to the FTP server.

We will go step by step describing the entire process, with the necessary clarifications to learn something and not just copy and paste a command to the terminal.

First we will establish the connection with the FTP server , for this we use the command:
ftp -np ftp.ejemplo.com

In this command we see that we use the -np options that mean:

  • -n : prevents authentication automatically during connection establishment. In this way it will be necessary to manually enter the username and password.
  • -p : use the passive mode for file transfers. This is useful when the FTP server is behind a firewall, but it is also necessary that the remote server has support for passive mode.

The only thing left to analyze for this first command is ejemploftp.com which is the host of the FTP server. In some cases it can be an IP and in others a domain name.

After executing this command, if everything went well, we will enter in interactive mode with the FTP server and we can send the necessary commands to upload the file.

First of all we will have to authenticate with a username and password , since our FTP server requires it. To authenticate we will use the command:
ftp> user usuario contraseña

In this command we see that there are three parts:

  • user : is the FTP command that requests the authentication of a user.
  • usuario : the username with which we authenticate on the remote server.
  • contraseña : the password corresponding to the username.

If we wanted to enter with the user Anonymous without a password, we can omit the password , so that the command would be:
ftp> user usuario

At this point we will use the FTP put command to send the file :
ftp> put /tmp/backup.tar.gz /backups/backup_copy.tar.gz

In this command three parts are distinguished:

  • put : is the FTP command that sends a file to the remote server.
  • /tmp/backup.tar.gz : it is the local file that we want to transfer via FTP.
  • /backups/backup_copy.tar.gz : is the remote file. In our example we have specified that it be stored inside the backups folder, but for everything to go well, that folder must exist and have write permissions for our user. If we do not specify a remote file name, the name of the local file will be used.

Finally, we disconnect from the FTP server with the command:
ftp> quit

As an example I leave all the text of an interactive session with the ftp command from the terminal:

zeokat@ubuntu:~$ ftp -np 192.168.247.128
Connected to 192.168.247.128.
220 (vsFTPd 3.0.3)

ftp> user zeokat contraseña
331 Please specify the password.
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.

ftp> put /tmp/backup.tar.gz /backups/backup_copy.tar.gz
local: /tmp/backup.tar.gz remote: /backups/backup_copy.tar.gz
227 Entering Passive Mode (192,168,247,128,169,254).
150 Ok to send data.
226 Transfer complete.
3363 bytes sent in 0.00 secs (89.0891 MB/s)

ftp> quit
221 Goodbye.

I think that I have not forgotten anything in the tutorial and that the basics are present. Anyway, it is always good to have the man ftp help command at hand, in case we need to consult anything. For example, in the case of wanting to upload several files, we can see in the documentation the operation of the FTP mput command.

Many of you may be wondering that the code provided for advanced users at the beginning of this tutorial is different from what we have seen step by step. At first glance it may seem different, but if we execute it we will get exactly the same, the only difference is that the first code is written non-interactively, so we can use it in a bash script.

Other methods to upload files via FTP from the Linux terminal.

All the previous explanation was done without installing any additional program, but in this section we will use different programs to upload a file via FTP from the Linux terminal .

Using cURL:

To install cURL we execute the command:
sudo apt-get install curl

To upload a file via FTP with cURL we execute from the terminal:
curl -T backup.tar.gz ftp://192.168.247.128 --user usuario:contraseña

Using LFTP:

To install LFTP we execute the command:
sudo apt-get install lftp

To send a file via FTP with LFTP we execute the command:
lftp -e "put -O directorio/remoto/ /archivo/local.txt -o nombre_remoto.txt; bye" -u usuario,contraseña ftp.ejemplo.com

In the -O and -o options it is important to look at whether it is uppercase or lowercase and the order in which they are . As an example, I usually run the following command to send a backup to a remote server:
lftp -e "put -O /backups /tmp/backup.tar.gz -o backup_copy.tar.gz; bye" -u zeokat,contraseña 192.168.247.128

Personally I like to use cURL because it is a library that supports more protocols besides FTP and has many years of development behind it. By this I do not mean that the rest of the alternatives are bad, it is only a personal preference. There are more programs with which to send a file via FTP from the console in Linux, for example a very good one is NCFTP , but I think that with those shown there is more than enough.


by (3.5m points)

Related questions

+5 votes
1 answer
+5 votes
1 answer
asked Aug 9, 2019 in Guides by backtothefuture (551k points) | 202 views
+3 votes
1 answer
asked Aug 9, 2019 in Guides by backtothefuture (551k points) | 238 views
+3 votes
1 answer
+4 votes
1 answer
Sponsored articles cost $40 per post. You can contact us via Feedback
10,634 questions
10,766 answers
510 comments
3 users