News | About | Get Frugalware | Packages | Documentation | Discussion Forums | Bug Tracker | Wiki | Community | Development

SSH

From FrugalWiki

Jump to: navigation, search



Secure Shell or SSH is a network protocol that allows data to be exchanged over a secure channel between two computers. Encryption provides confidentiality and integrity of data. SSH uses public-key cryptography to authenticate the remote computer and allow the remote computer to authenticate the user, if necessary.

SSH is typically used to log into a remote machine and execute commands, but it also supports tunneling, forwarding arbitrary TCP ports and X11 connections; file transfer can be accomplished using the associated SFTP or SCP protocols.

An SSH server, by default, listens on the standard TCP port 22. An SSH client program is typically used for establishing connections to an sshd daemon accepting remote connections. Both are commonly present on most modern operating systems, including Mac OS X, GNU/Linux, Solaris and OpenVMS. Proprietary, freeware and open source versions of various levels of complexity and completeness exist.

(Source: Wikipedia:Secure Shell)

Contents

OpenSSH

OpenSSH (OpenBSD Secure Shell) is a set of computer programs providing encrypted communication sessions over a computer network using the ssh protocol. It was created as an open source alternative to the proprietary Secure Shell software suite offered by SSH Communications Security. OpenSSH is developed as part of the OpenBSD project, which is led by Theo de Raadt.

OpenSSH is occasionally confused with the similarly-named OpenSSL; however, the projects have different purposes and are developed by different teams, the similar name is drawn only from similar goals.

Installing OpenSSH

Root terminal 48px.png
# pacman -S openssh


Configuring SSH

Client

The SSH client configuration file can be found and edited in /etc/ssh/ssh_config.

An example configuration:

File: /etc/ssh/ssh_config
#       $OpenBSD: ssh_config,v 1.25 2009/02/17 01:28:32 djm Exp $

# This is the ssh client system-wide configuration file.  See
# ssh_config(5) for more information.  This file provides defaults for
# users, and the values can be changed in per-user configuration files
# or on the command line.

# Configuration data is parsed as follows:
#  1. command line options
#  2. user-specific file
#  3. system-wide file
# Any configuration value is only changed the first time it is set.
# Thus, host-specific definitions should be at the beginning of the
# configuration file, and defaults at the end.

# Site-wide defaults for some commonly used options.  For a comprehensive
# list of available options, their meanings and defaults, please see the
# ssh_config(5) man page.

Host *
#   ForwardAgent no
#   ForwardX11 no
#   RhostsRSAAuthentication no
#   RSAAuthentication yes
#   PasswordAuthentication yes
#   HostbasedAuthentication no
#   GSSAPIAuthentication no
#   GSSAPIDelegateCredentials no
#   BatchMode no
#   CheckHostIP yes
#   AddressFamily any
#   ConnectTimeout 0
#   StrictHostKeyChecking ask
#   IdentityFile ~/.ssh/identity
#   IdentityFile ~/.ssh/id_rsa
#   IdentityFile ~/.ssh/id_dsa
#   Port 22
#   Protocol 2,1
#   Cipher 3des
#   Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc
#   MACs hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160
#   EscapeChar ~
#   Tunnel no
#   TunnelDevice any:any
#   PermitLocalCommand no
#   VisualHostKey no
HashKnownHosts yes
StrictHostKeyChecking ask

It is recommended to change the Protocol line into this:

Protocol 2

That means that only Protocol 2 will be used, since Protocol 1 is considered somewhat insecure.

Daemon

The SSH daemon configuration file can be found and edited in /etc/ssh/sshd_config.

An example configuration:

File: /etc/ssh/sshd_config
#    $OpenBSD: sshd_config,v 1.75 2007/03/19 01:01:29 djm Exp $

# This is the sshd server system-wide configuration file.  See
# sshd_config(5) for more information.

# This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin

# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented.  Uncommented options change a
# default value.

#Port 22
#Protocol 2,1
ListenAddress 0.0.0.0
#ListenAddress ::

# HostKey for protocol version 1
#HostKey /etc/ssh/sshhostkey
# HostKeys for protocol version 2
#HostKey /etc/ssh/sshhostrsa_key
#HostKey /etc/ssh/sshhostdsa_key

# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
#ServerKeyBits 768

# Logging
#obsoletes ~QuietMode and ~FascistLogging
#SyslogFacility AUTH
#LogLevel INFO

# Authentication:

#LoginGraceTime 2m
#PermitRootLogin yes
#StrictModes yes
#MaxAuthTries 6

#RSAAuthentication yes
#PubkeyAuthentication yes
#AuthorizedKeysFile     .ssh/authorized_keys

# For this to work you will also need host keys in /etc/ssh/sshknownhosts
#RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no

# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes

# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ~ChallengeResponseAuthentication mechanism.
# Depending on your PAM configuration, this may bypass the setting of
# PasswordAuthentication, ~PermitEmptyPasswords, and
# "PermitRootLogin without-password". If you just want the PAM account and
# session checks to run without PAM authentication, then enable this but set
# ChallengeResponseAuthentication=no
#UsePAM no

#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression yes
#ClientAliveInterval 0
#ClientAliveCountMax 3
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10

# no default banner path
#Banner /some/path

# override default of no subsystems
Subsystem       sftp    /usr/lib/ssh/sftp-server


To allow access only for some users add this line:

AllowUsers    user1 user2

You might want to change some lines so that they look as following:

Protocol 2
.
.
.
LoginGraceTime 120
.
.
.
PermitRootLogin no # (put yes here if you want root login)

You could also uncomment the BANNER option and edit /etc/issue for a nice welcome message.

Tip: You may want to change the default port from 22 to any higher port (see security through obscurity).

Even though the port ssh is running on could be detected by using a port-scanner like nmap, changing it will reduce the number of log entries caused by automated authentication attempts.

Tip: Disabling password logins entirely may also increase security, since each user with access to the server will need to create ssh keys. (see Using SSH Keys).
File: /etc/ssh/sshd_config
PasswordAuthentication no
ChallengeResponseAuthentication no

Allowing others in

Note: You have to adjust this file to remotely connect to your machine since the file is empty by default

To let other people ssh to your machine you need to adjust /etc/hosts.allow, add the following:

# let everyone connect to you
sshd: ALL

# OR you can restrict it to a certain ip
sshd: 192.168.0.1

# OR restrict for an IP range
sshd: 10.0.0.0/255.255.255.0

# OR restrict for an IP match
sshd: 192.168.1.

Now you should check your /etc/hosts.deny for the following line and make sure it looks like this:

ALL: ALL

That's it. You can SSH out and others should be able to SSH in :).

To start using the new configuration, restart the daemon (as root):

Root terminal 48px.png
# service sshd restart


Managing SSHD Daemon

To start/restart/stop the daemon, use the following:

Root terminal 48px.png
# service sshd start


Connecting to the server

To connect to a server, run:

User terminal 48px.png
$ ssh -p port user@server-address

Tips and Tricks

Encrypted Socks Tunnel

This is highly useful for laptop users connected to various unsafe wireless connections. The only thing you need is an SSH server running at a somewhat secure location, like your home or at work. It might be useful to use a dynamic DNS service like DynDNS so you don't have to remember your IP-address.

Step 1: Start the Connection

You only have to execute this single command in your favorite terminal to start the connection:

$ ssh -ND 4711 user@host

where "user" is your username at the SSH server running at the "host". It will ask for your password, and then you're connected! The "N" flag disables the interactive prompt, and the "D" flag specifies the local port on which to listen on (you can choose any port number if you want).

One way to make this easier is to put an alias line in your ~/.bashrc file as following:

alias sshtunnel="ssh -ND 4711 -v user@host"

It's nice to add the verbose "-v" flag, because then you can verify that it's actually connected from that output. Now you just have to execute the "sshtunnel" command :)

Step 2: Configure your Browser (or other programs)

The above step is completely useless if you don't configure your web browser (or other programs) to use this newly created socks tunnel. Since the current version of SSH supports both SOCKS4 and SOCKS5, you can use either of them.

  • For Firefox: Edit → Preferences → Advanced → Network → Connection → Setting:
Check the "Manual proxy configuration" radio button, and enter "localhost" in the "SOCKS host" text field, and then enter your port number in the next text field (I used 4711 above).

Firefox does not automatically make DNS requests through the socks tunnel. This potential privacy concern can be mitigated by the following steps:

  1. Type about:config into the Firefox location bar.
  2. Search for network.proxy.socks_remote_dns
  3. Set the value to true.
  4. Restart the browser.
  • For Chromium: You can set the SOCKS settings as enviroment variables or as command line options. I recommend to add one of the following functions to your .bashrc:
function secure_chromium {
    port=4711
    export SOCKS_SERVER=localhost:$port
    export SOCKS_VERSION=5
    chromium &
    exit
}

OR

function secure_chromium {
    port=4711
    chromium --proxy-server="socks://localhost:$port" &
    exit
}

Now open a terminal and just do:

User terminal 48px.png
$ secure_chromium

Enjoy your secure tunnel!

X11 Forwarding

To run graphical programs through a SSH connection you can enable X11 forwarding. An option needs to be set in the configuration files on the server and client (here "client" means your (desktop) machine your X11 Server runs on, and you will run X applications on the "server").

Install xorg-xauth on the server:

Root terminal 48px.png
# pacman -S xauth


  • Enable the AllowTcpForwarding option in sshd_config on the server.
  • Enable the X11Forwarding option in sshd_config on the server.
  • Set the X11DisplayOffset option in sshd_config on the server to 10.
  • Enable the X11UseLocalhost option in sshd_config on the server.


  • Enable the ForwardX11 option in ssh_config on the client.

To use the forwarding, log on to your server through ssh:

# ssh -X -p port user@server-address

If you receive errors trying to run graphical applications try trusted forwarding instead:

# ssh -Y -p port user@server-address

You can now start any X program on the remote server, the output will be forwarded to your local session:

Root terminal 48px.png
# xclock


If you get "Cannot open display" errors try the following command as the non root user:

User terminal 48px.png
$ xhost +

the above command will allow anybody to forward X11 applications. To restrict forwarding to a particular host type:

User terminal 48px.png
$ xhost +hostname

where hostname is the name of the particular host you want to forward to. Type "man xhost" for more details.

Be careful with some applications as they check for a running instance on the local machine. Firefox is an example. Either close running Firefox or use the following start parameter to start a remote instance on the local machine

User terminal 48px.png
$ firefox -no-remote

Speed up SSH

You can make all sessions to the same host use a single connection, which will greatly speed up subsequent logins, by adding those line under the proper host in /etc/ssh/ssh_config:

ControlMaster auto
ControlPath ~/.ssh/socket-%r@%h:%p

Changing the ciphers used by SSH to less cpu-demanding ones can improve speed. In this aspect, the best choices are arcfour and blowfish-cbc. Please do not do this unless you know what you are doing; arcfour has a number of known weaknesses. To use them, run SSH with the "c" flag, like this:

# ssh -c arcfour,blowfish-cbc user@server-address

To use them permanently, add this line under the proper host in /etc/ssh/ssh_config:

Ciphers arcfour,blowfish-cbc

Another option to improve speed is to enable compression with the "C" flag. A permanent solution is to add this line under the proper host in /etc/ssh/ssh_config:

Compression yes

Login time can be shorten by using the "4" flag, which bypasses IPv6 lookup. This can be made permanent by adding this line under the proper host in /etc/ssh/ssh_config:

AddressFamily inet

Another way of making these changes permanent is to create an alias in ~/.bashrc:

alias ssh='ssh -C4c arcfour,blowfish-cbc'

Trouble Shooting

make sure your DISPLAY string is resolveable on the remote end:

ssh -X user@server-address
server$ echo $DISPLAY
localhost:10.0
server$ telnet localhost 6010
localhost/6010: lookup failure: Temporary failure in name resolution   

can be fixed by adding localhost to /etc/hosts.

Mounting a Remote Filesystem with SSHFS

Install sshfs

Root terminal 48px.png
# pacman-g2 -S sshfs


Load the Fuse module

Root terminal 48px.png
# modprobe fuse

Add fuse to the modules array in /etc/rc.conf to load it on each system boot.

Mount the remote folder using sshfs

Root terminal 48px.png
# mkdir ~/remote_folder
Root terminal 48px.png
# sshfs USER@remote_server:/tmp ~/remote_folder


The command above will cause the folder /tmp on the remote server to be mounted as ~/remote_folder on the local machine. Copying any file to this folder will result in transparent copying over the network using SFTP. Same concerns direct file editing, creating or removing.

When we’re done working with the remote filesystem, we can unmount the remote folder by issuing:

Root terminal 48px.png
# fusermount -u ~/remote_folder


If we work on this folder on a daily basis, it is wise to add it to the /etc/fstab table. This way is can be automatically mounted upon system boot or mounted manually (if noauto option is chosen) without the need to specify the remote location each time. Here is a sample entry in the table:

sshfs#USER@remote_server:/tmp /full/path/to/directory fuse    defaults,auto,allow_other    0 0

Keep Alive

Your ssh session will automatically log out if it is idle. To keep the connection active (alive) add this to ~/.ssh/config or to /etc/ssh/ssh_config on the client.

ServerAliveInterval 120

This will send a "keep alive" signal to the server every 120 seconds.

Conversely, to keep incoming connections alive, you can set

ClientAliveInterval 120

(or some other number greater than 0) in /etc/ssh/sshd_config on the server.

Save connection data in .ssh/config

Whenever you want to connect to a server, you usually have to type at least its address and your username. To save that typing work for servers you regularly connect to, you can use the $HOME/.ssh/config file as shown in the following example:

File: $HOME/.ssh/config
Host myserver
    HostName 123.123.123.123
    Port 12345
    User bob
Host other_server
    HostName test.something.org
    User alice
    CheckHostIP no
    Cipher blowfish

Now you can simply connect to the server by using the name you specified:

User terminal 48px.png
$ ssh myserver

To see a complete list of the possible options, check out ssh_config's manpage on your system or the ssh_config documentation on the official website.

Troubleshooting

Connection Refused Problem

Is SSH running and listening?

 netstat -tnlp | grep ssh

If the above command doesn't display anything, then SSH is NOT running. Check /var/log/messages for errors etc.

Are there firewall rules blocking the connection?

Flush your iptables rules to make sure they are not interfering:

Root terminal 48px.png
# service firewall stop


or:

 iptables -P INPUT ACCEPT
 iptables -P OUTPUT ACCEPT
 iptables -F INPUT
 iptables -F OUTPUT

Allow SSH through the firewall permanently

Edit /etc/sysconfig/firewall and uncomment (i.e. remove the preceding hash symbol) the line that reads:

#-A INPUT -p tcp -m tcp --dport 22 -j ACCEPT

Have you allowed SSH in hosts.allow?

Double check you have done this section correctly.

Is the traffic even getting to your computer?

Start a traffic dump on the computer you're having problems with:

 tcpdump -lnn -i any port ssh and tcp-syn

This should show some basic information, then wait for any matching traffic to happen before displaying it. Try your connection now. If you don't see any output when you attempt to connect, then something outside of your computer is blocking the traffic (eg, hardware firewall, NAT router etc)

See Also

Links & References

Personal tools
Namespaces
Variants
Actions