scp
Updated: September 28, 2024
Secure Copy is used to copy files over a network.
Table of Contents
SYNTAX
scp [-346BCpqrTv] [-c cipher] [-F ssh_config] [-i identity_file]
[-J destination] [-l limit] [-o ssh_option] [-P port] [-S program]
source ... target
ARGUMENTS
-3 # Copies between 2 remote hosts through the local host.
-4 # IPv4
-6 # IPv6
-B # prevents asking for passwords
-C # Enable compression
-c # Selects cipher to be used to encrypt data on transfer
-F # Specify an alternative config file for ssh (per user)
-i # Selects file used for public key identity for authentication
-J # ProxyJump - connect to a target host by first connecting to a jump host
-l # Limits used bandwidth
-o # Pass in parameters to ssh (same as setup for .ssh/config file)
-P # Specify port to connect to on remote host
-p # Preserve mtime, atime etc.
-q # Quiet mode (no progress bar)
-r # Recursive (follows symbolic links)
-S # Name of program to use for encrypted connection (must understand ssh(1))
-T # Disable strict filename checking
-v # Verbose
MOVE SSH KEY TO SERVER
cat ~/.ssh/id_ed25519.pub | ssh user@remotehost 'cat >> ~/.ssh/authorized_keys' # appends key
scp -v ~/.ssh/id_rsa.pub pi@10.0.18.18:.ssh/authorized_keys # tends to overwrite keys
sudo chmod 600 authorized_keys # change mod u=rw
SCP USES SAME CONNECTION USED IN SSH
The big difference is that ssh use a lowercase p and scp uses uppercase P. If you change the default port from 22 to something else you will need to specify it anytime you ssh or scp.
# move from remote to local
scp -v -r -P1234 user@10.0.1.14:/home/pictures/vacation ~/Desktop/vacation
# move from local to remote
scp -v -r -P1234 ~/Desktop/ user@10.0.1.14:/home/pictures/vaction
USING SCP WITH SSH CONFIG IN PLACE
It is advocated that an SSH Config file be made. This will help setup different users with different keys easier to maintain. It eases accessing many different servers as you can referrence them by name. However, the use with scp changes along with ssh. It is not harder, just different. Given the file in the above link this is how to scp using it.
scp drax@pi3:test.txt ~/Documents # the hostname of machine is in config so its setting are invoked
This shows just how although we added complexity to our ssh configuration it still simplified the commands to use it!
EXAMPLES
Examples without using an ssh config file:
scp user@10.0.1.14:~/some_script.sh ~/Desktop/
scp -r user@10.0.1.14:/var/www ~/Desktop/Pi
scp -r user@10.0.1.14:~/Desktop/website/ /var/www/www.example.com