Sunday, August 14, 2016

SCP (Secure Copy) Examples


SCP (Secure Copy) Examples

Copy the file "foo.txt" from a remote host to the local host

$ scp /local/dir

Copy the file "foo.txt" from the local host to a remote host

$ scp foo.txt

Copy the directory "foo" from the local host to a remote host's directory "bar"

$ scp -r foo

Copy the file "foo.txt" from remote host "" to remote host ""

$ scp

Copying the files "foo.txt" and "bar.txt" from the local host to your home directory on the remote host

$ scp foo.txt bar.txt

Copy the file "foo.txt" from the local host to a remote host using port 2264

$ scp -P 2264 foo.txt

Copy multiple files from the remote host to your current directory on the local host

$ scp\{a,b,c\} .
$ scp\{foo.txt,bar.txt\} .

SSH Cheatsheet


SSH Notes

Configure aliases for hosts

For example, to set up named aliases for dev and qa, create a file at ~/.ssh/config:

Host dev
    User dev_username

Host qa
    User qa_usermname


$ ssh dev

Passwordless auth with ssh-copy-id

Use ssh-copy-id to copy your public key to the remotehost. After you've logged in once, you get passwordless authentication thereafter.

$ ssh-copy-id -i ~/.ssh/ remotehost

This will not work if you are connecting to a Windows remotehost.

Executing Multiple Commands with ssh

the here document syntax

The here document syntax lets you include everything in between << EOF and EOF as a list of commands. Like so:

$ ssh remotehost << EOF
  cd some_folder
  git pull

a script with commands only

Use a script of just the commands.


#!/usr/bin/env bash
cd some_project


$ ssh user@remote 'bash -s' < /path/to/

Transfer files with SFTP

You can use sftp in the same way ssh is used, to get an old-school ftp cli interface.

Compare files with diff

$ ssh user@host cat /path/to/remotefile | diff /path/to/localfile –

Port forwarding

Connects to a remote port, forward it to local.

$ ssh -L 4444:localhost:4000 remotehost

After that, from localhost, requests to http://localhost:4444 will get a response from http://remotehost:4000