Thursday, February 27, 2014

ssh

Linux can be easily administered remotely and securely by connecting to another machine's command line over an SSH connection.

Install

Install an ssh server to connect to:
#Ubuntu/Debian:
sudo apt-get install ssh
#Fedora/RPM:
sudo yum install ssh

Usage

Connect to a linux box:
ssh username@127.0.0.1
Add -X if you want to forward graphical programs through the connection. This means you would be able to open a program window like GIMP or gedit from the computer you are connecting to. This can also be used from a Mac to load a linux program remotely, so long as you have X11 installed.
ssh -X username@host.com
If you don't specify a user name, it will assume the user name of your local machine.
ssh host.com
If it takes a few seconds to connect, you can try editing /etc/nsswitch.conf. Find the line that reads:
hosts:  files mdns4_minimal [NOTFOUND=return] dns mdns4
and remove the "mdns4".

Copy files over a secure (ssh) connection:

scp local_file.mp3 username@remote.host.com:/remote/folder
scp username@remote.host.com:/remote/file.txt /local/directory/
#Use -r to get directories recursively:
scp -r

Port Forwarding

You can set your browser to use a proxy server that can tunnel your connection through ssh. This can be used to encrypt between your box and a server at home, as well as get around firewall restrictions on webpages. Ignoring HTTPS, the connection is only encr
#SOCKS proxy server (for whole web connection):
ssh userName@serverIp -D portOnLocalhost
#Other options:
ssh -D portToForwardLocally userName@serverIp -p portToConnectByRemotely
ssh -D 8080 userName@serverIp -p 2222
ssh inPort:userName@serverIp:outPort

Key Management

Generate a key:
ssh-keygen -t rsa
Update the password:
ssh-keygen -p
Add your ssh key to a host:
ssh-copy-id user@remotehost.com
#specify your public key:
ssh-copy-id -i .ssh/id_rsa.pub username:password@remotehost.com
Add your ssh key to your local authentication agent (so your system knows about it):
ssh-add

Additional Information

Remember you can always use the instruction manual for each program by:
man ssh
man ssh-keygen
man ssh-add
man programName