Remote Operations


SSH

ssh myUser@myTargetHost
Open a secure interactive session to the targeted host.

ssh myUser@myTargetHost myCommand
Run a command in the targeted host.

ssh myUser@myTargetHost -N -L localhost:1212:myUnreachableHost:1212
Create a local port forward.
Allows local machine to access a port on a machine that is in another network.
Scenario where local machine doesn't have access to myUnreachableHostp while myTargetHost does.

ssh myUser@myTargetHost -N -R 0.0.0.0:8080:localhost:80
Create a remote port forward.
Allows target machine to access a local machine port.
Scenario where remote machine is not allowed to open connections to local machine, but the contrary is allowed.


SSH Flags

-p
Specify the port to connect to on the target.

-i myPrivateKeyFile
Connect using private key (need that target trust public key beforehand).

-t
Force pseudo-terminal allocation (e.g. allows connecting to a user that do not have a default shell).

-L myLocalBindAddr:myLocalPort:myUnreachableHost:myUnreachableHostPort
Local port forward.

-R myTargetHostAddr:myTargetHostPort:localhost:myLocalPort
Remote port forward.

-D myBindAddr:myLocalPort
Dynamic port forward via specified port (ssh will act as a SOCKS proxy server).

-N
Do not execute a remote command (useful for just forwarding ports).

-f
Run in background (useful for just forwarding ports).


SCP

scp /path/to/myLocalFile.txt myUser@myTargetHost:/destination/path/
Copy a local file to a remote host.

scp -r /path/to/myFolder myUser@myTargetHost:/destination/path/
Copy a folder and its content to a remote host.

*scp -r /path/to/myFolder/ myUser@myTargetHost:/destination/path/myFolder/**
Copy the content of a folder to a remote host.


SFTP

sftp myUser@myTargetHost
Create an SFTP session.

help
Display help.

pwd
Returns remote current folder.

lpwd
Returns local current folder.

ls
Lists folders and files in remote current folder.

lls
Lists folders and files in local current folder.

cd
Changes remote directory.

lcd
Changes local directory

get
Downloads a file from server to client (-r for a directory).

put
Uploads a file from client to server (-r for a directory).

mkdir
Creates a folder in remote.

lmkdir
Creates a folder in local.

rmdir
Deletes a folder.

rm
Deletes a file.

exit
Exit SFTP session.

Key Management


Generate

ssh-keygen -t rsa
Generate a OpenSSH key pair (public and private keys).


Trust Keys

ssh-copy-id myUser@myHost
Copy public key to target host (allows connecting with private key -i).