Home   Notes   Contact Me

ssh

Local


Work Computer, remote desktop from home without vpn

# on work machine # have work computer send its remote desktop port to home: ssh -R 9999:localhost:3389 HomeUsername1@HomeAddress1 # you can now access remote desktop on home machine in remote desktop at this address: localhost:9999 # if you need a different home machine to be able to see the remote desktop # and the different machine cannot be ssh'd into, do this to bring the port # to the other home machine. # enter this on the other home machine you are bringing the forwarded port to: ssh -L 9999:localhost:9999 HomeUsername1@HomeAddress1 # the same remote desktop address should work find now.

Connect without having to confirm new machines ip address (good for automated testing scripts)

Use StrictHostKeyChecking=no you can put this is the ssh_config file (on cygwin it is at: /etc/ssh_config ), or you can do it on the command line with as shown below:

ssh -o StrictHostKeyChecking=no myusername@targetcomputer

Port Forwarding simplified

Bring a remote machines port to me

ssh-bring-remote-port-to-me.png

ssh -L 5901:localhost:5901 mylogin@192.168.1.102

Bring a remote reachable port to me

ssh-bring-remote-reachable-port-to-me.png

ssh -L 5901:192.168.1.103:5901 mylogin@192.168.1.102

Have remote machine SEND a port to me

ssh-remote-send-port-to-me.png

ssh -R 5901:localhost:5901 mylogin@192.168.1.102

Port Forwarding Cookbook

#send a port from a computer to another computer ssh -R 3391:localhost:3390 username@computername ssh -R {port#onSentTo}:localhost:{port#onMachineSshCommandIsTypedInOn} username@computername #================================================== #to grab a port from another computer ssh -L 3391:localhost:3390 username@computername ssh -L {port#onLocalMachine}:localhost:{port#onComputerGettingItFrom} username@computername

Installing ssh

Ubuntu

Ubuntu installing ssh

Configuring the User settings


Port Forwarding to sshd System

situation list


Port Forwarding to sshd Systems Neighbor

situation list


From B, Let C use Port on A

situation list

ssh -L 22:addressOfA:3737 sshUsername@addressOfC

From Work To firewalled Home Via accessable Home

situation list


Port Forwarding to sshd Systems Neighbor as if local to Neighbor

situation list


Port Forwarding

Reference Network Map

System A --Firewall-- internet --Firewall-- System B -- System C

Notes:
An sshd System is a system that is running sshd which listens to ssh connections. It is the System ssh can connect to.
A closed firewall is one that does not allow connections to be initiated from the outside.
A sshd firewall is one that allows connections from outside ssh clients to sshd running on a System behind it.
For these examples assume the internet address of the Systems is just the word System followed by the letter used in the table, with no spaces. Examples: SystemA SystemB

CaseScenarioFromTo
To sshd SystemSystem B -> System C
[Program appears to be running local on C]
System behind
closed firewall
System behind
sshd firewall
To sshd Systems NeighborSystem B -> System C -> System D
[Program appears to be conecting from C]
System behind
closed firewall
System unreachable
through System reachable
from sshd firewall
To sshd Systems Neighbor
as if you were running
your program locally
On the Neighbor
System B -> System C -> System D
[B thinks Program is running locally]
[D thinks Program is running locally]
System behind
closed firewall
System unreachable
through System reachable
from sshd firewall
From Work
To firewalled Home
Via accessable Home
System B -> System C -> System D
[B thinks Program is running locally]
[D thinks Program is running locally]
System behind
closed firewall
System unreachable
through System reachable
from sshd firewall
From B, Let C use Port on A
Email Me if you want me to document another case

ssh Command Syntax


ssh -N -L p1:rra:p2 [sshUsername@]sshdAddr 
ParameterDescription
-NTells ssh not to open a command shell on the ssh server machine (the default _is_ to open one)
-LTells it you are linking a port
p1 (local port)Port that is created on the local machine, this is where things connect to enter the ssh tunnel
rra
(remote reachable addr)
This is where the other end of the tunnel will be located at. Note that it need not be on the ssh server machine, but it can be on any machine that machine can reach. So if the ssh server has a local addr like '192.168.1.100' you, can use this parameter to reach other machines local to it like '192.168.1.101'. Also note that the value 'localhost' is a valid choice
p2
(on remote reachable machine)
Port that is openned on the machine specified by 'remote reachable addr'
sshUsername
sshdAddr

Reverse ssh

How to get access to remote machine that cannot be ssh'd into

MachineNetwork AddressIP addressUsername
Work work.company.com 128.16.0.146 mylastname
Home home.home.org 66.66.66.66 myhandle
  1. On work machine type:
    ssh -R 10003:128.16.0.146:22 -f -N 66.66.66.66 -l myhandle
  2. On home machine type:
    ssh mylastname@localhost -p 10003

Details of the commands

On work machine, setting up an ssh tunnel:

ssh -R {tunnel port #}:{work machines ip}:{port to access} -f -N {home machines ip} -l {home username}
Parameter Purpose
-R Reverse Tunnel
tunnel port # The port number that will be used on the home machine to connect to the tunnel
work machines ip The ip address the work machine knows itself as.
This can be a local to the work network address, and potentially you can use 'localhost' instead of an ip address.
This also can be the ip address of another machine in the set of ip addresses at work - if you want to make the tunnel connect to a machine other than where the tunnel is being created at.
port to access Which port the tunnel provides access to (like 22 for ssh, see a list of well known ports for others)
home machines ip This is whatever string is needed to ssh into the home machine, unless you have your own domain, it will be an ip address.
home username Your user name on the home machine
-f Runs ssh in the background
-N ?? Is this so with -R?? Tells ssh not to open a command shell on the ssh server machine (the default _is_ to open one)

On home machine, using the tunnel (for ssh-ing back into work)


ssh {work username}@localhost -p {tunnel port #}
Parameter Purpose
work username Your user name on the work machine
tunnel port # The tunnels port number, as described by the command on the work machine that set up the tunnel

Making sure a tunnel is there (on the home machine)

netstat -na | grep -i listen

Passwordless Login



on client make .ssh dir
go into .ssh dir

you only need do the following the first time, then use the same key on all target systems
generate a public key:

ssh-keygen -t rsa
ssh-keygen -d


give it a name like:

id_rsa

no passphrase

copy the .pub file made to something with the name of the system you are on (so you can keep track of which system it is for)
for example id_dsa-sirius.pub where sirius is the name of the machine.

copy the id_rsa.pub file to the target system you want to log into without a password
(where username is the name of the account you don't want to need a password to ssh to)

scp id_rsa.pub <username>@targetmachine:identity.pub

log into the target machine (in the account 'username')

cause the equivelent of the following to happen

mkdir .ssh
cat identity.pub >> .ssh/authorized_keys
chmod go-rwx .ssh/authorized_keys
rm identity.pub

logout, then try ssh-ing in, it should work with no password.

!! NOTE !!

ssh without password will fail if server side directories are to insecure, use the following
settings to allow it to work:

chmod 755 ~ ~/.ssh
chmod 644 ~/.ssh/authorized_keys

!! NOTE 2 !!

Make sure you spell authorized_keys correctly.