SSH

September 16, 2018 Linux, Application 1 minute, 4 seconds

Basic usage with custom port and custom key

ssh -i ~/.ssh/id_rsa -p PORT USER@HOST

Local port forwarding

ssh -L BINDADDRESS:PORT:DSTHOST:DSTPORT USER@HOST

For examlpe:

ssh -L localhost:9000:127.0.0.1:80 admin@rootknecht.net

When you now visit localhost:9000 in your browser you will be tunneled to your machine. There you get the return of the url 127.0.0.1:80 without actually loging into your machine.

Remote port forwarding

ssh -R BINDADDRESS:PORT:LOCALHOST:LOCALPORT USER@HOST

Essentially the other way of local port forwarding for example:

ssh -R 9000:localhost:80 admin@rootknect.net

Now if you visit your remote server on port 900 it is directed to your local machine port 80

In sshd_config make sure to add the line GatewayPorts yes.

Run command on host

ssh -i ~/.ssh/id_rsa USER@HOST "sudo apt-get update && sudo apt-get upgrade"

SSH related files

File Purpose
~/.ssh/ User specific ssh settings
~/.ssh/authorized_keys Public keys which are allowed to login as this user
~/.ssh/config User specific ssh config file
~/.ssh/id_* Common prefix for public/private ssh keys
~/.ssh/known_hosts Public host keys known to user
/etc/ssh/ssh_config Global ssh client config
/etc/ssh/sshd_config Global ssh server config

Add authorized key

cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
#  remotley
ssh-copy-id -i ~/.ssh/id_rsa USER@HOST

Copy files

From host

scp USER@HOST:/path/to/file.txt /tmp/file.txt

To host

scp /tmp/file.txt USER@HOST:/tmp/file.txt

Options

  • -r → copy recursive
  • -P → custom port (note capital P in contrast to ssh)