How to transfer files between devices
I use multiple devices and I need to copy files between them. I mainly transfer the files with ssh/scp/sftp, rsync, samba/cifs, web and ftp servers.
SSH
In general, I setup an openssh server on my computers and I use scp/sftp to transfer files. I setup an ssh key on the machines to avoid entering my password each time I transfer a file. On my iphone, I use the FTPManager app which has sftp to transfer files (my photos and videos) to my computers.
Here are the commands for setting up ssh:
apt-get install openssh-server
# Generate an ssh key (in the client)
# sshKey is the secret key filename and sshKey.pub is the public key
ssh-keygen -t rsa -f sshKey -b 4096 -N 'passphrase' -C name@example.com
# Copy the public key to the server
scp sshKey.pub name@example.com:.ssh/authorized_keys
# To test the setup, connect to the server
ssh -i sshKey name@example.com
# Copy the secret key to ~/.ssh in the client
chmod 600 sshKey
mv sshKey ~/.ssh/
# Create the ssh config file:
vi ~/.ssh/config
# keep connection alive (for NAT routers)
Host *
ServerAliveInterval 290
ServerAliveCountMax 2
Host example.com
HostName example.com
User name
IdentityFile ~/.ssh/sshKey
:wq
# In the client, add the key to ssh-agent to type the passphrase only once
eval `ssh-agent`
ssh-add ~/.ssh/sshKey
# To copy a file or a tree, run
scp file example.com:Downloads/
scp -r directory example.com:Downloads/
When I want to browse the file system and transfer files, I use midnight commander:
apt-get install mc
mc
# then F9 > Right > SFTP link > example.com
With SSH, it is possible to use a middle machine to transfer files:
- Machine A has the files
- Machine B is in the middle
- Machine C downloads from machine A
For this, I use tar:
# From C
# the data is compressed with bzip2 on the network:
ssh B 'ssh A "/bin/tar cj file1 dir2 file3"' | tar xj
Sharing drives
I share drives through the local network with samba/cifs. I have my media on a server and the server disk is shared with my other machines.
I wrote about my samba setup in how to share files with samba.
In the past, I also used NFS. I stopped using it because the clients were crashing when there was a problem with the server.
Rsync
I use rsync because sometimes scp/sftp fail (large transfer and going offline,...) and with rsync it is possible to resume a transfer. Unison is command that synchronizes directory between machines, I haven't used it in along time..
# Copy a directory
rsync -az directory/ example.com:Dowloads/
# If the ssh key is not configured
rsync -az -e 'ssh -i ~/.ssh/sshKey' directory/ example.com:Dowloads/
# To resume a transfer
rsync -az --partial directory/ example.com:Dowloads/
# with unison
unison directory/ ssh://example.com/Downloads/
Web server
I usually setup the lighttpd web server, then copy files to the web root '/var/www/' and download the files with a web browser.
Setup:
apt-get install lighttpd
vi /etc/lighttpd/lighttpd.conf
server.document-root = "/var/www"
at the end:
server.dir-listing = "enable"
/etc/init.d/lighttpd restart
FTP
I install pure-ftpd server, there is no configuration and it works directly after installation:
apt-get install pure-ftpd
The ftp clients I use are ncftp and mc:
apt-get install ncftp mc
# ncftp usage
# open connection
open -u user serverAddress
# copy directory from client machine to server
put -R directory
# mc usage
# To open connection: Choose FTP link...
ftp://user@serverAddress
# on older mc, it is:
user@serverAddress
Other alternatives
- Syncthing is a background program that copies or deletes files in specified directories between computer.
- Nextcloud uses the same principle but there is a server storing all the data. Nextcloud has apps for iOS and Android, so it is convenient for copying files to/from phones.
I don't use these systems because they take resources while not using them and I don't transfer files everyday.
- Sshfs allows sharing a drive on a remote machine through an ssh connection, it is similar to samba and it needs a mount point on the local machine.
Tags: #ssh #rsync #samba #cifs #ftp