The screen program, among other useful things, lets you keep an SSH session running even after you disconnect from SSH. Here’s how to use it.
SSH in.
ssh user@hostname.com
Once on the remote machine, set up a named screen:
screen -S myscreen
In another terminal, open another SSH connnection and start another screen:
screen -S mysecondscreen
You can see they are there by using, in either of the terminals,
screen -ls
This does NOT start screen, just lists the different screens.
You can now disconnect the SSH connections. When you reconnect, you can use
screen -r myscreen
or
screen -r mysecondscreen
to reconnect to the one you want.
Some easy ways to configure SSH to be a little more secure:
Edit /etc/ssh/sshd_config as root.
- Change the port (default is 22)
- Change “PermitRootLogin yes” to “PermitRootLogin no”
- AddUser username
- save and quit
- restart the ssh server: sudo /etc/init.d/ssh restart
More info here:
http://ubuntu-tutorials.com/2007/02/14/what-you-ought-to-know-about-securing-ssh/
Easy way to add public key of this machine to a remote machine:
ssh-copy-id -i ~/.ssh/id_dsa.pub root@fileserver01
More good info here:
http://ubuntu-tutorials.com/2007/02/05/unattended-ssh-login-public-key-ssh-authorization-ssh-automatic-login/
Step 1: SSH forwarding
First, forward the local port 3307 to 3306. That is, when you access the local port 3307, it will redirect it to port 3306 on the remote host.
ssh -fNg4 -L 3307:127.0.0.1:3306 user@hostname
-f sends SSH to the background
-g allows remote hosts to connect to local forwarded ports
-N don’t execute a remote command
-4 this was key! Forces IPv4. Kept getting “bind: Address already in use” errors because I didn’t have this.
-L the forwarding magic happens here . . . syntax is localport:localhost:remoteport
Step 2: Connect to mysql on port 3307
. . . which will redirect to port 3306 on remote host.
mysql -u root -h 127.0.0.1 -P 3307 -p
and you’re in!