In this tutorial I want to show you how to copy files from one computer to another on a network containing both Linux and Windows hosts, using the SSH protocol. Linux-to-Linux, Linux-to-Windows, and Windows-to-Linux file transfer can be accomplished with an SSH daemon running on one or more Linux hosts and/or a copy of Cygwin running on each Windows host. (For Windows-to-Windows file transfer, it’s much easier to just transfer files over SMB using Windows HomeGroup.) Here are the steps needed to make seamless file transfer over a heterogeneous network a reality:
Step 1: Install OpenSSH if it isn’t already installed
OpenSSH comes preinstalled on most Linux distros, but if it isn’t, you can install it by running the package manager for your distro on the
openssh-server package. For example:
$ sudo apt install openssh-server
Step 2: Generate an SSH host key file
Once OpenSSH is installed, you want to generate a host key for authenticating between the client and the server. You do this using the
$ sudo ssh-keygen -A -t ecdsa
In this command, the
-A option tells
ssh-keygen to create a host key file in /etc/ssh, as opposed to just a file in your home directory. The SSH daemon will not work unless you use a file in the host key format. The
-t option specifies what public key cipher is to be used. I’m using ECDSA, which is a digital signature algorithm based on elliptic curve cryptography. You can also use RSA or Diffie-Hellman, but ECDSA seems to be the de facto standard for SSH servers.
Step 3: Start the SSH server
Once you’ve created an SSH host key, you can start the OpenSSH server using the following command:
$ sudo /usr/bin/sshd
This starts up the OpenSSH server. (A lot of Linux systems will complain if you don’t provide the full file path for
sshd. I don’t actually know why.) Now you can log into your Linux system remotely from an SSH client.
Step 4: Install Cygwin on Windows
Cygwin is a program for Windows that emulates the POSIX API and command line interface, allowing you to run Unix/Linux software on Windows. It is the method I use to run an SSH client from a Windows computer. You can get the latest version of Cygwin from the Cygwin project’s website. Download the installer and then follow the installation instructions. Cygwin comes with an SSH client installed by default, so you won’t have to do anything special here.
Linux-to-Linux file transfer using scp
scp command transfers files to a remote host via the SSH protocol. The name is an acronym that stands for “Secure Copy” and it is an encrypted version of the earlier “Remote Copy” program
rcp. To do a remote copy using
scp, an SSH server must be running on the target machine.
To copy a file from one Linux machine to another using
scp, make sure an SSH server is set up on the destination host, then from the source host, type a command like this:
$ scp file firstname.lastname@example.org:~
Let’s dissect this command now… The first argument is the file or files you want to transfer. The second argument is a specification for the destination which has the following structure:
<username> @ <hostname-or-ip-address> : <directory>
When I’m transferring files through
scp, my destination username will typically be the main non-root user on the destination machine and my destination directory will typically be that user’s home directory. This is because I often don’t have the proper permissions for other directories on the destination machine such as the OpenMediaVault volume on my Raspberry Pi NAS server. After I do this I go to the destination host (or do a direct SSH login) and move the file to the desired location.
If you want to transfer an entire directory, you add the
-r option to
scp like so:
$ scp -r dir email@example.com:~
Windows-to-Linux and Linux-to-Windows file transfer
To copy a file from a Windows computer to a Linux computer via SSH, fire up the Cygwin terminal and
cd to the directory containing the file you want to transfer. The proper file path to use consists of the full Windows file path with all backslashes replaced with forward slashes, the colon after the drive letter removed, and the entire path preceded with
/cygdrive/. For example:
Windows file path:
Cygwin file path:
Once you are in the right directory, use the same
scp command you would use for a Linux-to-Linux file transfer.
In addition to uploading files to an SSH server, you can also download files by simply reversing the order of the arguments:
$ scp firstname.lastname@example.org:~/file .
In this command, you use the user@host:path specification for the file on the remote host you want to download, followed by the directory on the local host to copy it to.
Note that in all of these file transfer methods, the
scp command is run from the SSH client, not the SSH server.
So that’s basically how you transfer files over SSH on a heterogeneous network. I hope you found this tutorial valuable. If you did, please consider sharing my content on social media, and also checking out some of the other Linux-related articles on my site. For now, farewell and happy command line hacking. 🙂