Do you have a file on your Linux PC that needs to be transferred to your Linux server and you are not sure how to do this? This article teaches you how to copy files via SSH to your remote Linux server. It presents two methods for achieving this file transfer in a secure way. One based on the
scp
program and one based on the rsync
program.- Ssh Download File From Server To Local Machine Windows
- Download File Ssh To Local
- Free Ssh Download
- Ssh Download File To Local Machine
Background
Once you have your own Linux server up and running, you typically access it through SSH. SSH stands for Secure Socket Shell. SSH enables you to securely log in and access your Linux server over an unsecured network. Through SSH you can install, configure and update software on your Linux server, to name just a few common Linux server administration tasks.
While administering you Linux server, sooner or later you run into a situation where you have a file on your own Linux PC and you need to transfer this file to your Linux server. So you SSH-ed into your server and you are staring at your terminal screen, wondering how to go about this task. Unfortunately, you cannot directly transfer a file from your own PC to your remote Linux server through this active SSH terminal session. Luckily though, several methods exist that enable you to copy files via SSH. This article presents you with two of these methods. Namely, by using the
scp
and rsync
programs.On Mac or Linux, run the touch command while in the boot directory to create a blank ssh file: touch shh. Safely remove or eject the card from the computer and insert it again in your Raspberry Pi. Boot up Raspberry Pi. Every time you turn on the Pi board, the device looks for the ssh file. When the device finds the file, then SSH is enabled automatically. I am using the Terminal window to ssh into a unix server. I am not sure how to copy a file from my mac onto the unix server. What command do I enter and how do I type the file I want to upload Example. Say my file is named Test1.doc and it is on the usr/me/test/working/ directory and I want to upload into to the Unix directory listed as me/test2/ what command do I need to enter? When I tried that it says: mv: accessing 'Desktop/hostfile.txt': Not a directory Could it possibly be trying to download it to itself (the remote machine)? With a few other commands it was listing file paths on the remote machine (the server, not my computer). Next, open a Terminal window on your Mac and navigate using the cd (change directory) command to the folder containing the private key file (.pem) created during the EC2 configuration process. This is usually in the Downloads folder unless you moved the file to another folder.
System setup
A typical system setup consists of your Linux desktop PC, connected to your local network router, and a remote Linux server somewhere in the cloud. Instead of setting up a cloud server somewhere for this article (think Digital Ocean or Linode for example) , I decided on running a Linux server as a virtual machine (VM) on my laptop. Below you can find an illustration of the system setup:
My trusty Lenovo Thinkpad T450s serves as the Desktop PC. I run Debian 10 on this PC and its hostname is set to
tinka
. The Linux server VM also runs Debian 10 and its hostname is set to debianvm
. I configured the same username on both the PC and the server. It is set to pragmalin
. Refer to this article in case you would like to setup a similar Debian server as a virtual machine with VirtualBox.Connecting to your server via SSH
While explaining the steps for copying files to the Debian server via SSH, I’ll occasionally SSH into the Debian server to verify that the files actually got transferred. Here follows a quick refresher that explains how you can log into your server via SSH.
The command from a Linux terminal on your PC to connect to your server is:
ssh <username>@ip-address
or ssh <username>@hostname
. In my case the hostname of the Debian server VM is debianvm
. My username on this server is set to pragmalin
. This means that I can log into this server via SSH with the command:ssh pragmalin@debianvm
To close the SSH connection, simply type the
exit
command:SCP versus RSYNC
Before diving into the actual file copying via SSH, we should discuss the two commonly used programs for this, namely
scp
and rsync
.The SCP program
The
scp
program is a secure copy program. So basically a secure and remote version of the cp
program that you locally use for copying files. Pretty much all Linux server distributions install the scp
program by default, including Debian. Now, if the already installed scp
program does all we need then why would we ever need another program for the same task? Read on and you’ll see that rsync
does offer some benefits.The RSYNC program
The
rsync
program is labeled as a fast, versatile and remote file-copying tool. But it is not just a plain file-copying tool. The rsync
program features build-in synchronization functionality. This means that it only copies a file to the remote server if it is not already present. In contrast, the scp
program blatantly overwrites the file. Furthermore, rsync
can compress the files during the transfer. In other words, rsync
is faster and uses less network bandwidth.By default
rsync
does not communicate in a secure way. Luckily an easy fix exists for this. You can force rsync
to use the SSH protocol by specifying the -e 'ssh'
option when calling the program. Another minor disadvantage is that rsync
is not installed by default on all Linux server distributions. Of course this is merely a one time inconvenience. You can simply install it with sudo apt install rsync
. Just keep in mind that the rsync
program needs to be installed on both sides. So both on your PC and your server.When should you use
scp
and when rsync
? They both work, so it partially comes down to personal preference. Personally, I use scp
for small quick file transfers as its syntax strikes me as more intuitive. For large file transfers, I opt for rsync
, because it is faster and uses less network bandwidth. For example when I need to restore a complete backup to one of my servers.WordPress archive
For file copy via SSH testing purposes, this article uses the latest WordPress archive. WordPress is a hugely popular website content management system and runs on millions of websites, including the PragmaticLinux blog. We are not actually going to install WordPress, but just use the WordPress files for file copy example purposes.
Go ahead and download the latest WordPress archive from https://wordpress.org/latest.tar.gz. On my PC the file
wordpress-5.4.2.tar.gz
is now present in directory /home/pragmalin/Downloads/
Copy a single file
Let’s start out with copying just a single file to the server via SSH. Open your terminal and go to the directory that holds to previously downloaded WordPress archive. Next, run either one of the following commands to copy the file to your remote server. Just replace the
/home/pragmalin
directory name with the name of your home directory on the server and replace the pragmalin@debianvm
part with your username on the server and the hostname of the server, respectively:scp wordpress-5.4.2.tar.gz pragmalin@debianvm:/home/pragmalin
rsync -e 'ssh' -avz wordpress-5.4.2.tar.gz pragmalin@debianvm:/home/pragmalin
If you now SSH into your server, you can verify the presence of the
wordpress-5.4.2.tar.gz
file in your user’s home directory. Both the scp
and rsync
commands have a similar structure. It is:[COMMAND] [OPTIONAL ARGUMENTS] [SOURCE] [DESTINATION]
As you can see in this example, the
scp
program does not require any arguments. However, the rsync
program does: -e 'ssh' -avz
. For detailed information on the command options, you can refer to the program’s man-page. Alternatively, you can make use of the excellent explainshell.com website. Here are the links for an explanation of the previous two commands: scp and rsync.Permissions
Note that you can only copy files to a directory where the username you specified has write permissions. That is the reason why I specified the home directory in this example. If you need to store the file in a directory where your user does not have write permissions, then you would have to connect to the server via SSH afterwards and move the file with the help of
sudo mv
.Reverse transfer direction
You can copy the files via SSH in the other direction too. So from the server to your PC. You just need to swap the
[SOURCE]
and [DESTINATION]
in the command. For example:scp pragmalin@debianvm:/home/pragmalin/wordpress-5.4.2.tar.gz /home/pragmalin/Downloads
rsync -e 'ssh' -avz pragmalin@debianvm:/home/pragmalin/wordpress-5.4.2.tar.gz /home/pragmalin/Downloads
Copy all files in a directory
Another common operation is to copy all the files in a specific directory via SSH. We need a few files to try this out. Since we already downloaded the WordPress archive, we might all well extract its contents to get a bunch of files for testing purposes:
tar -xvf wordpress-5.4.2.tar.gz
The newly created
wordpress
subdirectory now holds the archive contents. To copy all the files in this directory to your remote server, run either one of the following commands. Just replace the /home/pragmalin
directory name with the name of your home directory on the server and replace the pragmalin@debianvm
part with your username on the server and the hostname of the server, respectively:scp * pragmalin@debianvm:/home/pragmalin
rsync -e 'ssh' -avz --no-recursive * pragmalin@debianvm:/home/pragmalin
If you now SSH into your server, you can verify the presence of the files such as
index.php
, wp-config-sample.php
, etc. in your user’s home directory.Copy all files in a directory recursively
In the previous section just the files in a specific directory were copied. This did not include subdirectories. If you want to copy everything, so files and subdirectories, run either one of the following commands. Just replace the
/home/pragmalin
directory name with the name of your home directory on the server and replace the pragmalin@debianvm
part with your username on the server and the hostname of the server, respectively:scp -r * pragmalin@debianvm:/home/pragmalin
rsync -e 'ssh' -avz * pragmalin@debianvm:/home/pragmalin
The output of the command is a bit too long for a screenshot. However the following screenshot from the directory contents listing on the server show proof that the copy operation worked. You can verify the presence of the files such as
index.php
and wp-config-sample.php
, but also all the directories such as wp-admin
, wp-contents
, etc. in your user’s home directory:Wrap up
After working through this article, you now know about two programs (
scp
and rsync
) that enable you to copy files via SSH. Both commands get the job done. The syntax of the rsync
command is a bit more complicated so you might prefer scp
. Keep in mind though that rsync
uses less network bandwidth. As a result rsync
is faster especially when transferring a large amount of data.The syntax for both commands is not hard to understand. Nevertheless, it is complex enough that you probably won’t memorize them, unless used frequently. For this reason I recommend bookmarking this article. That way you can quickly reference this information when needed.
Whether you are developer, devops guy or a sys admin in charge of multiple servers, you'll always find yourself wanting to move files from one machine to another. It's not an easy feat for first-timers, but it gets easy with a bit of experience.
There are several ways of moving files and directories such as a newly developed website or backup from your local machine to a remote server and vise versa. Different methods use ssh, ftp or http protocol to achieve the same. I prefer SSH method since there's increased level of security while moving your data.
scp
This is probably my favorite when moving files from one box to another. scp using ssh behind the scenes which means you get added encryption whilst moving files.
The general syntax is;
scp from-source to-destination e.g
scp from-source to-destination e.g
scp [email protected]:/home/dave/file.txt /home/dave/docs
If you are copying directory, don't forget to add the -r option.
If you wish to specify ssh port of the remote server, then add the -P option (notice it's uppercase). So if I were copying a directory from remote box whose ssh port is 1234 to my local machine, then that would be;
If you wish to specify ssh port of the remote server, then add the -P option (notice it's uppercase). So if I were copying a directory from remote box whose ssh port is 1234 to my local machine, then that would be;
scp -rP 1234 [email protected]:/home/dave/file.txt /home/dave/docs
You'll be prompted for ssh password eachtime you make the transfer. However, if you setup passwordless ssh, then you won't be prompted everytime.
rsync
rsync is probably my second favorite method of copying files between two computers. It would have been my favorite if it wasn't for some potentially dangerous gotcha newbie might fall for I'll discuss later.
The advantage of rsync over the other options is that is copies only the files/directories that have changed. It doesn't copy the entire directory eachtime you initiate a transfer. Therefore it's best for incremental backups and pushing website or application code changes to production box. It's sort like git but without version control. It's really cool.
Now if you are copying files within the same machine or from an external hard drive to your machine, you could use rsync plainly the way you use cp. e.g
rsync from/source/file to/destination/dir
.But if you wish to copy files from remote pc whose ssh port is 1234, then you cause ssh together with rync to achieve this like this;
rsync -have 'ssh -p 1234' [email protected]:/home/dave/remote-file.txt /home/dave/docs
You can read more about rsync options here but generally the 'have' option means;
-h: output numbers in a human-readable format.
-a: It is a quick way of saying you want recursion. and want to preserve almost everything.
-v: give you information about what files are being transferred and a brief summary at the end.
-e: shell option.
-a: It is a quick way of saying you want recursion. and want to preserve almost everything.
-v: give you information about what files are being transferred and a brief summary at the end.
-e: shell option.
If you are syncing remote directory with local one, then use the same syntax except add a leading slash at the end of directory path of the source directory like this;
rsync -have 'ssh -p 1234' [email protected]:/home/dave/remote-dir/ /home/dave/local-dir
Now the gotcha is if /home/dave/remote-dir/ is empty (for instance you mistakenly use a different empty dir such as /home/dave/remote-empty-dir) and you rsync, it'll empty the destination dir /home/dave/local-dir. So very careful about the ordering of the directories.
ftp
My good-old-friend ftp is still very much used today as it was in the beginning of the internet days. FTP uses plain-text authentication by default, however, today there are ftp implementation that use SSL protocol such as ftps or sftp to add an added layer of protection. I encourage you to use either of them.
Web developers and web hosting companies majorly like to use ftp for file transfer. Devs default to using a multitude of ftp clients to copy files. My favorite ftp client is Filezilla which is pretty awesome, but there are alternatives in the wild such as;
- Coreftp,
- WinSCP
- Transmit (Mac OS X)
- FireFTP (All Platforms with Firefox)
- Cyberduck (Mac OS X).
When you want to copy files from one remote server to another remote one, then you might not have the option of using GUI ftp apps unless you first download to your local machine then re-upload to the destination box. To save time and bandwidth, I use ftp commandline client.
So let me share some ftp commands thanks to awesome guys at cyberciti.biz;
$ ftp my-remote-box.com
ftp> ls
ftp> cd dirName
Task: Download / Copy file
To copy one file at a time from the remote ftp server to the local system use get command:
ftp> ls
ftp> cd dirName
Task: Download / Copy file
To copy one file at a time from the remote ftp server to the local system use get command:
get fileName
get fileName newFileName
get fileName newFileName
To change directory on your local system, enter:
ftp> lcd /path/to/new/dir
ftp> lcd /path/to/new/dir
Print local directory:
ftp> lpwd
ftp> lpwd
Task: Download Multiple Files
To download all files, enter:
ftp> mget *
ftp> mget *
To download all perl files (ending with .pl extension), enter:
ftp> mget *.pl
ftp> mget *.pl
Task: Upload One File
To copy one file at a time from the local systems to the remote ftp server, enter:
ftp> put fileName
To copy one file at a time from the local systems to the remote ftp server, enter:
ftp> put fileName
Task: Upload Multiple Files
Upload all files from the current system:
ftp> mput *
ftp> mput *.pl
ftp> mput *
ftp> mput *.pl
Task: Create a Directory
To make a new directory, enter:
ftp> mkdir dirName
To make a new directory, enter:
ftp> mkdir dirName
Task: Delete a Directory
To remove or delete a directory, enter:
ftp> rmdir dirName
To remove or delete a directory, enter:
ftp> rmdir dirName
Note: recursive copy doesn’t work. Use wget instead like this;
wget -m --user='dave' --password='my-secure-password' ftp://ftp.my-remote-box.com/remote-dir
.That will copy remote ftp directory from my-remote-box.com using ftp username dave and password my-secure-password. This really is a pro-tip since unfortunately ftp commandline won't recursively copy directories.
wget
The amazing wget is the download manager that rules them all. Nothing beats wget when you want to copy files from remote box to localhost. The only challenge is that you can't move from localhost to remote though. It's a one-way trip, but an awesome trip nonetheless.
Downloading a file from remote server is as simple as giving wget a link to download.
wget http://my-remote-box/remote-file.txt
To recursively download a directory using wget enter;
wget -r --no-parent http://my-remote-box/remote-dir
.The Parameters mean:
-r //recursive Download
and
--no-parent // Don´t download something from the parent directory
-r //recursive Download
and
--no-parent // Don´t download something from the parent directory
//To download a whole website with wget.
wget --random-wait -r -p -e robots=off -U mozilla www.example.com
.You can visit explainshell.com to get a break down of this long command. You can visit labnol.org for more examples on how to use the magic of wget or even gnu.org.
Samba/smb
Finally we visit our very good-old-friends. Almost as old as FTP and the internet itself. Samba uses the smb/cifs protocol majorly to share files between windows computers and Linux machines. There's a long dirty history there between Microsoft and FOSS community but now things seem to be working out pretty well now that M$ changed strategy in recent years.
Nonetheless, samba is probably your best option of sharing files between a linux server and windows machines withing the same LAN network. It does NOT work over the internet.
After you have setup a samba server which is beyond the scope of this post, you can access remote share in the following ways;
On the windows machine type this at run:
ip-of-the-samba-pcshared
.Take note of the forward slashes.
On Linux/ubuntu machine open file browser and type this at the location bar;
smb://ip-of-the-samba-pc/shared
Take note of the back slashes.
Ssh Download File From Server To Local Machine Windows
You may also be able to access files by directly specifying their names in the form
HOSTSHAREPATHFILE
, where HOST is the computer’s NetBIOS name, SHARE is the name of the share, and PATHFILE is the path to the file relative to the share’s root.Take note that while accessing from windows you must use the samba ip or netbios name rather than the samba server domain name self!NFS
Network File System or NFS is my favorite method for sharing files between two Linux machines on the same LAN. I discovered Samba shares can’t write file name with special characters. Specifically I noticed this while working with email files such as 1451825736.V68I5391eb4M572897.example.com:2,.
Download File Ssh To Local
NFS works much like Unix File system while Samba works more like Windows.
Usually I mount a remote NFS share on local machine after setting us the NFS server. Once the remote share is mounted locally, it behaves like any other directory on your local machine making copying files really easy.
Git
If you are developer, you probably know about version control and more specifically git. You can use git not just for keeping track of changes to your code, but also to push files to remote server.
Free Ssh Download
When you are done committing changes to your code, you simply git push to an online git-compliant service such as Github, Gitlab, Bitbucket or even your own remote repository. Then on the server where you need the file, you then git pull the code from the remote repository.
netcat and python
You can create a simple webserver using python command for all files in the current directory (and sub-directories) and make them available to anyone on your network. You might have to open firewall rules for particular port you are using but by default the command defaults to port 8000. You can ofcourse restrict access to only particular IPs. I use this trick a lot when a client requests their website or mail files from a server.
python -m SimpleHTTPServer 9000
(for port 9000 and Python 2) and python -m http.server
9000 (for port 9000 and Python 3).I find using python command really simple and easy to remember, but you can use netcat as well.
Ssh Download File To Local Machine
//transfer files using netcat
At server-side
At server-side
nc server.ip 9999 > remote-file.txt or cat hugefile.ext | nc -l -p 9999
At client-sise
nc -l -p 9999 < remote-file.txt
So there you have it; 7 ways to transfer files between two remote machines. The options are dependent on whether the machines are on the same network or are connected via the internet, whether they are both of the same OS(Linux) or Windows/Mac and Linux, if security is paramount or not, whether I need a quick dirty solution or if am looking at long-time remote file sharing.
**Quick one folks: For the last 7 months, I have been working on this cool project that automates website uptime monitoring, checks SSL certificate for errors and Expiry, monitors domain expiry and cron jobs. You probably want to keep tabs on these if you are running a website or app in product. So Please Sign up to Site Monki today for free. **
Image: gcn.com