LFTP is a Linux command-line utility that downloads and uploads files from one server to another. It can transfer files via FTP, FTPS, HTTP, HTTPS, FISH, SFTP, BitTorrent, and FTP over HTTP proxy. It also supports the FXP protocol that allows data transfers between two FTP servers bypassing the client machine. It offers some valuable features. Some of them are listed below:
- File directories recursive copying
- Segmented file transfer
- Recursive copying of file directories
- Resuming partial downloads
- Transfer queues
This post will show you how to install and use LFTP to download and upload files in Linux.
Install LFTP
By default, the LFTP package is included in the official repository of all major Linux operating systems.
For Ubuntu and Debian operating systems, use the following command to install the LFTP package:
apt-get install lftp -y
For CentOS, RHEL, and Fedora operating systems, use the following command to install the LFTP package:
dnf install lftp -y
Once LFTP is installed, verify the LFTP version using the following command:
lftp --version
You will get the following output:
LFTP | Version 4.9.2 | Copyright (c) 1996-2020 Alexander V. Lukyanov LFTP is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
Also Read
How to Download Files Using Wget in Linux
Configure LFTP Passwordless Access
LFTP supports SSH protocol, so it is a good idea to configure passwordless authentication between two servers. To do so, generate an SSH key with the following command:
ssh-keygen -t rsa
You should see the following output:
Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:gBnx9dwo3RXNgdaaDjpVBIPjNY507+HQtquTjxiwFXs root@vyompc The key's randomart image is: +---[RSA 2048]----+ | o. . .oo.==.| | = . B B.= .o| | o o +.@ O o | | . +o= B | | .SooEB o | | +o. = | | . .. . . | | oo.. | | . o+. | +----[SHA256]-----+
Next, copy the newly generated SSH key to the remote Linux server using the following command:
ssh-copy-id root@remote-server-ip
You will get the following output:
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub" The authenticity of host '216.98.10.172 (216.98.10.172)' can't be established. ECDSA key fingerprint is SHA256:Bj/4/r1XP0LQ0Vl2LMsfywLX5vu5Zaqr723K7wGUvkY. Are you sure you want to continue connecting (yes/no)? yes /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys Password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh '[email protected]'" and check to make sure that only the key(s) you wanted were added.
Next, verify the SSH passwordless authentication using the following command:
ssh root@remote-server-ip
If everything is fine, you will get the remote server shell in the following output:
Linux linux 5.10.0-11-amd64 #1 SMP Debian 5.10.92-1 (2022-01-18) x86_64 The programs included with the Debian GNU/Linux system are free software; the exact distribution terms for each program are described in the individual files in /usr/share/doc/*/copyright. Debian GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent permitted by applicable law. root@linux:~#
Also Read
How To Set Up SSH Public and Private Key in Linux
How to Use LFTP in Linux
Before using the LFTP, you must connect to the FTP or SFTP server via a command-line interface.
The basic syntax to login into FTP or SFTP server is shown below:
lftp sftp://user@sftp-server-ip
Or
lftp ftp://user@ftp-server-ip
Let’s connect to the SFTP server with the following command:
lftp sftp://[email protected]
Once you are connected to the SFTP server, you will get the LFTP shell in the following output:
lftp [email protected]:~>
You can now use the ls command to list all files and directories:
lftp [email protected]:~> ls
You will get the following output:
drwx------ 4 root root 4096 Mar 16 02:46 . drwxr-xr-x 18 root root 4096 Mar 16 02:44 .. -rw-r--r-- 1 root root 570 Jan 31 2010 .bashrc drwxr-xr-x 3 root root 4096 Nov 22 2019 .local -rw-r--r-- 1 root root 161 Jul 9 2019 .profile drwx------ 2 root root 4096 Mar 16 02:46 .ssh
Upload and Download Files Using LFTP
You can use the pget command to download files from the SFTP server. For example, to download a file named file1.txt from the SFTP server to your local machine, run the following command inside the LFTP shell:
lftp [email protected]:~> pget file1.txt
You will get the following output:
104857600 bytes transferred in 58 seconds (1.73 MiB/s)
You can use the put command to upload files to the SFTP server. For example, to upload a file named file2.txt to the SFTP server from the local machine, run the following command:
put file2.txt
You will get the following output:
10485760 bytes transferred in 21 seconds (499.1 KiB/s)
You can use the mirror command to download the whole directory from the SFTP server.
mirror /etc /opt/
This command will download the /etc directory from the SFTP server to the local machine inside /opt directory.
If you want to upload a directory to the SFTP server from the local machine, use the mirros -R command:
mirror -R /var/www/html /mnt/
This will upload a directory named /var/www/html from the local machine to the SFTP server inside the /mnt directory.
Finally, run the exit command to exit from the LFTP shell.
exit
Conclusion
In this post, we explained how to use the LFTP command to download and upload a file and directory to and from the SFTP server. You can now try to use the LFTP command to transfer files and directories to the FTP server. Try it on dedicated hosting from Atlantic.Net!