How to transfer files and folders among clients (i.e., a macOS M1) and severs (i.e., Ubuntu 18.04 LTS) using SSH Protocol?
SSH Connection
The standard port of SSH to connect to server is 22.
ssh -p 22 user@IP-Address
If the sever uses a non-standard port, you should specify this instead of 22
.
ssh -v -p 22 user@IP-Address
-v
is the verbose mode, it is helpful in diagnosing SSH connectivity issues as it outputs all steps of the connection process.
Besides using the password, can connect using an SSH key,
ssh -i keyfile user@IP-Address
To disconnect
exit
File Transfer using SCP
From a local client to a remote server,
scp -P 22 ~/starting-folder/file-name user@IP-Address:~/destination-folder
From a remote server to a local client,
scp -P 22 user@IP-Address:~/starting-folder/file-name ~/destination-folder
Folder Transfer using SCP
Use the recursive option -r
to secure copy a folder.
From a local client to a remote server,
scp -r -P 22 ~/starting-folder user@IP-Address:~/destination-folder
Result in the remote server,
user@IP-Address:~/destination-folder/starting-folder
From a remote server to a local client,
scp -r -P 22 user@IP-Address:~/starting-folder ~/destination-folder
Result in the local client,
~/destination-folder/starting-folder
Thus, should be careful of trailing slashes.
Folder Transfer using RSYNC
Advantages: rsync
can resume transfers if the connection breaks, and intelligently transfers only the differences between files.
rsync -avz -e 'ssh' ~/starting-folder user@IP-Address:~/destination-folder
Result in the remote sever,
user@IP-Address:~/destination-folder/starting-folder