Copy file ssh and scp is one of the most common ways to manage *nix servers, and to connect to the server. Quite often, you’ll need to upload or unload a file to a remote server, and there are no other options than using an SSH connection. Fortunately, copying files over a secure connection is one of the Protocol’s usual functions. On Mac (and Linux) computers, you can use the scp command to do it.
Copy file ssh and scp: The SCP command
scp [modifier] [source] [target]
Execute the following instructions if the source or target directory is on a remote server:
Using the following command, you can copy the local file /home/user/file.doc to the root user home directory of the remote server 192.168.1.80:
scp /home/user/file.doc email@example.com:/root
To copy the same file from a remote server:
scp firstname.lastname@example.org:/root/file.doc /home/user
You can copy many files at the same time. To do so, you’ll need to add them as a source. Put a space between them – the last parameter will be used as the target directory. To upload file1 files.doc and file2.doc from a local directory to a distant server, for example, use the following command:
scp file1.doc file2.doc email@example.com:/root
You’ll need to use the –r command modifier to copy the directory. Copy the /home/user/dir directory to the remote server:
scp-r /home/user/dir firstname.lastname@example.org:/root
The -P option is required when the SSH server is running on a port other than the default. If you need to use port 10000, follow this step:
scp-P 10000 /home/user/file.doc email@example.com:/root
Simply run scp without parameters and read the instructions to see what other modifications are available.
Using the SSH connection to run commands on a remote server
On a remote server, the SSH protocol allows you to perform interactive sessions as well as normal commands and scripts.
ssh [user]@[server] '[command]'
Enter the specified user’s password and, if applicable, the command output in the SSH console.
For example, we can learn about the distant server’s operating system by looking at the following:
ssh firstname.lastname@example.org 'uname -a'
You can use the “;” character as a separator to perform several commands from a single connection. Check the distant server’s network settings and active network connections:
ssh email@example.com 'ifconfig; netstat -anp tcp'
Run the command interpreter if you need to run a local script file on a remote server (in the script execution mode). It can be run with the-s key, and the script file can be sent to it as standard input. Take a look at the following example:
ssh firstname.lastname@example.org 'bash -s' < /home/user/myscript.sh
On a remote server, the local file /home/user/myscript.sh will be run.
When you run the SSH command without any parameters, you’ll see a quick syntax reference and a list of extra modifiers that let you enhance the command’s capabilities.
This is how to copy fille ssh and scp. Check out how to Remove ssh key Mac, Unix and Linux.