SSH

From ArchWiki

Jump to: navigation, search


Contents

[edit] Introduction

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; it can transfer files 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, Linux, Solaris and OpenVMS. Proprietary, freeware and open source versions of various levels of complexity and completeness exist.

[edit] 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.

[edit] Installing OpenSSH

pacman -Sy openssh


[edit] Configuring the SSH server

To configure you must edit the configuration file:

su -c 'nano /etc/ssh/sshd_config'

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.

[edit] Adjust the configuration

The configuration file can be found at /etc/ssh/ssh_config and the basic version looks like this:

#	$OpenBSD: sshd_config,v 1.75 2007/03/19 01:01:29 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 various options

# Host *
#   ForwardAgent no
#   ForwardX11 no
#   RhostsRSAAuthentication no
#   RSAAuthentication yes
#   PasswordAuthentication yes
#   HostbasedAuthentication 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-cbc,3des-cbc,blowfish-cbc,cast128-cbc,arcfour,aes192-cbc,aes256-cbc
#   EscapeChar ~


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.
Of course there is also a configuration file for the SSH daemon. It's called /etc/ssh/sshd_config and looks like this:

#	$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/ssh''host''key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh''host''rsa_key
#HostKey /etc/ssh/ssh''host''dsa_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/ssh''known''hosts
#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.

[edit] 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: DENY


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


To start using the new configuration, restart the daemon:

su -c '/etc/rc.d/sshd restart'

[edit] Managing SSHD Daemon

Just add sshd to the "DAEMONS" section of your /etc/rc.conf:

DAEMONS=(... ... ... ... ... sshd ... ... ...)


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

/etc/rc.d/sshd {start|stop|restart}



[edit] Connecting to the server

To connect to a server, run:

ssh -p port user@server-address

[edit] Links & References

A Cure for the Common SSH Login Attack
Using SSH Keys
Defending against brute force ssh attacks


[edit] Tips and Tricks

[edit] 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.

[edit] 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 wich 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 :)

[edit] Step 2: Configure your Browser

The above step is completely useless if you don't configure your web browser to use this newly created socks tunnel.

For Firefox it's quite easy: go to the Edit menu, and choose Preferences. Go to Advanced -> Network -> Connection -> Settings. 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).

Enjoy your secure tunnel!

Personal tools