External OpenSSL Command-line FAQ

SSH config tweaks

Most default SSH installations need a little tweaking for speed or extra security. There are two config files to worry about. One is on the server you are trying to connect to and the other is on your localhost.

/etc/ssh/sshd_config

These are changes I always make to /etc/ssh/sshd_config.
See fail2ban for protecting against bots doing dictionary attacks.

# this speeds up logins.
UseDNS no
# Don't make this 1. Public keys count as 1st try. If that fails the SSHd will not try password.
MaxAuthTries 2
# bots often check these accounts for weak passwords:
DenyUsers root test admin guest nobody
# for even extra security, limit access to only these users:
# Uncomment the line below and add the usernames you want to allow.
#AllowUsers user1 user2 user3

Use the following to support SSH1. I no longer use this.

# this is required if you want to support SSH1
Protocol 2,1
# this is required if you want to support SSH1
PasswordAuthentication yes

/etc/ssh/ssh_config or ~/.ssh/config

Edit the client SSH config if you are getting slow logins. This is sometimes caused by GSSAPI Authentication. I also like to turn on KeepAlive.

Host *
# This sometimes fixes slow logins on Ubuntu.
# Don't use this if you use Kerberos or GSSAPI.
GSSAPIAuthentication no
# This helps prevent timeout disconnects.
TCPKeepAlive yes
ServerAliveInterval 60

simple port forwarding (SSH tunnel)

This example creates a tunnel for HTTP. This will forward port 80 of your localhost to port 80 of www.example.com.

ssh -f -N -q -L 80:localhost:80 username@www.example.com

This example creates a tunnel for IMAP. Here we forward port 1143 on localhost to 143 (IMAP) on imap.example.com.

ssh -f -N -q -L 1143:localhost:143 username@imap.example.com

-f tells ssh to go into the background (daemonize).

-N tells ssh that you don't want to run a remote command. That is, you only want to forward ports.

-q tells ssh to be quiet

-L specifies the port forwarding

port forwarding through an intermediary

You can have the remote machine forward ports to a third machine. This is useful where your have your local machine outside a firewall; a visible machine on the DMZ; and a third machine invisible to the outside.

This creates a tunnel from your localhost port 81 to 192.168.1.69 port 80 through dmz.example.com. This lets you see the web server from outside a LAN.

ssh -f -N -q -L 81:192.168.1.69:80 username@dmz.example.com

This example creates a tunnel for IMAP. Here we forward port 1143 on localhost to 143 (IMAP) on 192.168.1.100 through dmz.example.com.

ssh -f -N -q -L 1143:192.168.1.100:143 username@dmz.example.com

Using scp through a DMZ gateway to a machine behind a firewall

First you setup port forwarding through an intermediary. This forwards your localhost port 2222 to port 22 on 192.168.1.100. Remember, that 192.168.1.100 is not on your local network. The machine at 192.168.1.100 is on the LAN network visible to the machine 208.77.188.166.

SOCKS5 with Firefox

Simple and secure web browsing. You can setup a tunnel as described above or you can use the following technique. This starts SSH on your localhost acting as a SOCKS proxy. Once you start SSH this way you can point any application that supports a SOCKS5 interface to this port. But these instructions will show what you need to do to get Firefox to proxy through SOCKS. Firefox supports SOCKS with no extra add-ons.

Start ssh an connection to a host that you want to proxy through. Use the -D option to specify a SOCKS5 port on your localhost. The port doesn't really matter. You just need to use the same port in your SOCKS client application. :

This tool can also help in switching the proxy settings on and off:
SwitchProxy Tool

Remote Server Security Enhancement with SSH Keys

You can make port forwarding even more secure by limiting what a privileged account can do. When you add a key to authorized_key you may pass parameters to fine tune the connection. This can be used to restrict what the client is allowed to do. On the remote server, add the following to ~username/.ssh/authorized_keys:

reverse port forwarding

Sometimes I need to make an internal LAN machine expose a service to the outside WAN.
For example, I have a database server that will only accept connections from a
specific development box. That dev box is inside the firewall. I want to connect to
the database from outside the firewall.