Secure Shell

From ArchWiki
Revision as of 16:56, 23 February 2011 by Sandstorm (Talk | contribs) (X11 Forwarding)

Jump to: navigation, search

This template has only maintenance purposes. For linking to local translations please use interlanguage links, see Help:i18n#Interlanguage links.

Local languages: Català – Dansk – English – Español – Esperanto – Hrvatski – Indonesia – Italiano – Lietuviškai – Magyar – Nederlands – Norsk Bokmål – Polski – Português – Slovenský – Česky – Ελληνικά – Български – Русский – Српски – Українська – עברית – العربية – ไทย – 日本語 – 正體中文 – 简体中文 – 한국어

External languages (all articles in these languages should be moved to the external wiki): Deutsch – Français – Română – Suomi – Svenska – Tiếng Việt – Türkçe – فارسی

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)


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

# pacman -S openssh

Configuring SSH


The SSH client configuration file can be found and edited in Template:Filename.

An example configuration:


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.


The SSH daemon configuration file can be found and edited in Template:Filename.

An example configuration:


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 Template:Filename 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).


Allowing others in

Template:Box Note

To let other people ssh to your machine you need to adjust Template:Filename, add the following:

# let everyone connect to you
sshd: ALL

# OR you can restrict it to a certain ip

# OR restrict for an IP range

# OR restrict for an IP match
sshd: 192.168.1.

Now you should check your Template:Filename for the following line and make sure it looks like this:


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):

# /etc/rc.d/sshd restart

Managing SSHD Daemon

Just add sshd to the "DAEMONS" section of your Template:Filename:

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

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

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

Connecting to the server

To connect to a server, run:

$ 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 Template:Codeline is your username at the SSH server running at the Template:Codeline. It will ask for your password, and then you're connected! The Template:Codeline flag disables the interactive prompt, and the Template:Codeline 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 Template:Filename file as following:

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

It's nice to add the verbose Template:Codeline flag, because then you can verify that it's actually connected from that output. Now you just have to execute the Template:Codeline 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.

  • 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).
Make sure you select SOCKS4 as the protocol to use. This procedure will not work for SOCKS5.

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:

# pacman -S xorg-xauth

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:

# xclock

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

$ xhost +

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

$ 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

$ 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 Template:Filename:

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 Template:Codeline flag, like this:

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

To use them permanently, add this line under the proper host in Template:Filename:

Ciphers arcfour,blowfish-cbc

Another option to improve speed is to enable compression with the Template:Codeline flag. A permanent solution is to add this line under the proper host in Template:Filename:

Compression yes

Login time can be shorten by using the Template:Codeline flag, which bypasses IPv6 lookup. This can be made permanent by adding this line under the proper host in Template:Filename:

AddressFamily inet

Another way of making these changes permanent is to create an alias in Template:Filename:

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
server$ telnet localhost 6010
localhost/6010: lookup failure: Temporary failure in name resolution   

can be fixed by adding localhost to Template:Filename.

Mounting a Remote Filesystem with SSHFS

Install sshfs

# pacman -S sshfs

Load the Fuse module

# modprobe fuse

Add fuse to the modules array in Template:Filename to load it on each system boot.

Mount the remote folder using sshfs

# mkdir ~/remote_folder
# 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:

# fusermount -u ~/remote_folder

If we work on this folder on a daily basis, it is wise to add it to the Template:Filename table. This way is can be automatically mounted upon system boot or mounted manually (if Template:Codeline 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 Template:Filename or to Template:Filename 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 Template:Filename 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 Template:Filename file as shown in the following example:


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

$ 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.


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:

 /etc/rc.d/iptables stop


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

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