SSH: Best Practices

jbrown 17 February, 2008 17:04 Linux Permalink Trackbacks (0)

SSH: Best Practices

Introduction

Are you using SSH in the best way possible? Have you configured it to be as limited and secure as possible? The goal of this document is to kick in the new year with some best practices for SSH: why you should use them, how to set them up, and how to verify that they are in place.

All of the examples below assume that you are using EnGarde Secure Linux but any modern Linux distribution will do just fine since, as far as I know, everybody ships OpenSSH.

SSHv2 vs. SSHv1

There are numerous benefits to using the latest version of the SSH protocol, version 2, over it's older counterpart, version 1 and I'm not going into a lot of details on those benefits here - if you're interested, see the URL in the reference below or Google around. That being said if you don't have an explicit reason to use the older version 1, you should always be using version 2.

To use SSHv2 by default but permit SSHv1, locate the "Protocol" line in your sshd_config file and change it to:

Protocol 2,1

When doing 2,1 please note that the protocol selection is left up to the client. Most clients will default to v2 and "fall back" to v1, while legacy clients may continue to use v1. To force everybody to use SSHv2, change it to:

Protocol 2

When you make this change don't forget to generate the appropriate HostKey's as well! SSHv2 requires the following keys:

# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key

While SSHv1 requires:

# HostKey for protocol version 1
HostKey /etc/ssh/ssh_host_key

Once your changes are made, restart the SSH daemon:

# /etc/init.d/sshd restart

[ SUCCESSFUL ] Secure Shell Daemon
[ SUCCESSFUL ] Secure Shell Daemon

From another machine, try SSH'ing in. You can use the -v option to see which protocol is being used, and the '-oProtocol=' option to force one or the other - for example, "ssh -v -oProtocol=2 " would force protocol version 2.

Binding to a Specific Address or Non-Standard Port

If you're running SSH on an internal, firewalled, workstation then you can probably skip this section, but if you're running SSH on a firewall or on a machine with two network interfaces, this section is for you.

Out of the box OpenSSH will bind to every available network address; while convenient and suitable for most installations, this is far from optimal. If your machine has two or more interfaces then the odds are that one is "trusted" and the other is "untrusted." If this is the case, and you don't need nor want SSH access coming in on the untrusted interface, then you should configure OpenSSH to listen on a specific interface.

To have OpenSSH only bind to your internal interface, 192.168.0.1 in the example below, locate the following line in your sshd_config file:

ListenAddress 0.0.0.0

and change the 0.0.0.0 to 192.168.0.1:

ListenAddress 192.168.0.1

To verify that this change took, restart OpenSSH and look at netstat:

# /etc/init.d/sshd restart

[ SUCCESSFUL ] Secure Shell Daemon
[ SUCCESSFUL ] Secure Shell Daemon

# netstat -anp | grep sshd

tcp 0 0 192.168.0.1:22 0.0.0.0:* LISTEN 7868/sshd

As you can see, the sshd daemon is now only listening on 192.168.0.1. SSH requests coming in any other interface will be ignored.

Similarly, you may want to change the port that the SSH daemon binds to. Sometimes there is a functional need for this (ie, your employer blocks outbound 22/tcp) but there is also security-through-obscurity value in this as well. While not providing any real security benefit against a determined attacker, moving the SSH daemon off of port 22 protects you against automated attacks which assume that the daemon is running on port 22.

To have OpenSSH bind to a port other than port 22, 31337 in the example below, locate the following line in your sshd_config file:

Port 22

and change the 22 to 31337:

Port 31337

To verify that this change took, restart OpenSSH and, again, look at netstat:

# netstat -anp | grep sshd

tcp 0 0 192.168.0.1:31337 0.0.0.0:* LISTEN 330/sshd

Finally, to SSH into a host whose SSH daemon is listening on a non-standard port, use the -p option:

ssh -p 31337 user@192.168.0.1

Using TCP Wrappers

TCP Wrappers are used to limit access to TCP services on your machine. If you haven't heard of TCP Wrappers you've probably heard of /etc/hosts.allow and /etc/hosts.deny: these are the two configuration files for TCP Wrappers. In the context of SSH, TCP Wrappers allow you to decide what specific addresses or networks have access to the SSH service.

To use TCP Wrappers with SSH you need to make sure that OpenSSH was built with the -with-tcp-wrappers. This is the case on any modern distribution.

As I indicated earlier, TCP Wrappers are configured by editing the /etc/hosts.deny and /etc/hosts.allow files. Typically you tell hosts.deny to deny everything, then add entries to hosts.allow to permit specific hosts access to specific services.

An example:

#
# hosts.deny This file describes the names of the hosts which are
# *not* allowed to use the local INET services, as decided
# by the '/usr/sbin/tcpd' server.
#
ALL: ALL
#
# hosts.allow This file describes the names of the hosts which are
# allowed to use the local INET services, as decided
# by the '/usr/sbin/tcpd' server.
#
sshd: 207.46.236. 198.133.219.25

In the example above, access to SSH is limited to the network 207.46.236.0/24 and the address 198.133.219.25. Requests to any other service from any other address are denied by the "ALL: ALL" in hosts.deny. If you try to SSH into a machine and TCP Wrappers denies your access, you'll see something like this:

ssh_exchange_identification: Connection closed by remote host

This simple configuration change significantly hardens your installation since, with it in place, packets from hostile clients are dropped very early in the TCP session -- and before they can do any real damage to a potentially vulnerable daemon.

Public Key Authentication

The last item I will cover is public key authentication. One of the best things you can do to tighten the security of your SSH installation is to disable password authentication and to use public key authentication instead. Password authentication is suboptimal for many reasons, but mostly because people choose bad passwords and attackers routinely try to brute-force passwords. If the systems administrator has chosen a bad password and he's permitting root logins... game over.

Public key authentication is no silver bullet - similarly, people generate passphrase-less keys or leave ssh-agents running when they shouldn't - but, in my opinion, it's a much better bet.

Just about every distribution ships with public key authentication enabled, but begin by making sure it is:

RSAAuthentication yes
PubkeyAuthentication yes

Both of these options default to "yes" and the "RSAAuthentication" option is for SSHv1 and the "PubkeyAuthentication" option is for SSHv2. If you plan on using this authentication method exclusively, while you're there, you may want to disable password authentication:

PasswordAuthentication no

Before you proceed, make sure you have a terminal open on your target machine. Once you restart the SSH daemon you will no longer be able to log in without a key... which we haven't generated yet!

Once you're sure, restart the SSH daemon:

# /etc/init.d/sshd restart

[ SUCCESSFUL ] Secure Shell Daemon
[ SUCCESSFUL ] Secure Shell Daemon

Now, from your desktop, try to SSH in to your target machine:

$ ssh rwm@brainy

Permission denied (publickey,keyboard-interactive).

We're locked out! This is a good thing. The next step, on your desktop, is to generate a key:

$ ssh-keygen -t dsa -C "Ryan's SSHv2 DSA Key (Jan 2008)"

Generating public/private dsa key pair.
Enter file in which to save the key (/home/rwm/.ssh/id_dsa):
Enter passphrase (empty for no passphrase): **********
Enter same passphrase again: **********
Your identification has been saved in /home/rwm/.ssh/id_dsa.
Your public key has been saved in /home/rwm/.ssh/id_dsa.pub.
The key fingerprint is:
98:4d:50:ba:ee:8b:79:be:b3:36:75:8a:c2:4a:44:4b Ryan's SSHv2 DSA Key (Jan 2008)

A few notes on this:

  • You can generate a DSA (-t dsa), RSA (-t rsa), or SSHv1 (-t rsa1) key. In the example above I'm using dsa.
  • I like to put the date I generated the key in the comment (-C) field, that way I can change it out every so often.
  • You're entering a passphrase, not a password. Use a long string with spaces and punctuation. The longer and more complicated the better!

The command you just ran generated two files - id_dsa, your private key and id_dsa.pub, your public key. It is critical that you keep your private key private, but you can distribute your public key to any machines you would like to access.

Now that you have generated your keys we need to get the public key into the ~/.ssh/authorized_keys file on the target machine. The best way to do this is to copy-and-paste it - begin by concatenating the public key file:

$ cat .ssh/id_dsa.pub

ssh-dss AAAAB3NzaC1kc3MAAACBAL7p6bsg5kK4ES9BWLPCNABl20iQQB3R0ymaPMHK...
... ds= Ryan's SSHv2 DSA Key (Jan 2008)

This is a very long string. Make sure you copy all of it and that you do NOT copy the newline character at the end. In other words, copy from the "ssh" to the "2008)", but not past that.

The next step is to append this key to the end of the ~/.ssh/authorized_keys file on your target machine. Remember that terminal I told you to keep open a few steps ago? Type the following command into it, pasting the key you've just copied into the area noted KEY:

echo "KEY" >> ~/.ssh/authorized_keys

For example:

echo "ssh-dss AAAA5kS9BWLPCN...s= Ryan's SSHv2 DSA Key (Jan 2008)" >> ~/.ssh/authorized_keys

Now, try to SSH in again. If you did this procedure correctly then instead of being denied access, you'll be prompted for your passphrase:

$ ssh rwm@brainy

Enter passphrase for key '/home/rwm/.ssh/id_dsa':
Last login: Thu Jan 10 14:37:14 2008 from papa.engardelinux.org
[rwm@brainy ~]$


comments

  1. no teletrack payday loans one hour payday loans loan applications no credit check payday loans payday loans Jacksonville online cash advance no fax payday loans quick faxless payday loans pay day one loan faxing ace cash advance locations payday loans work check cashing usa miami cash advance credit card rates payday loan no faxing no credit check personal loan companies payday advance loans locations nix check cashing locations in inglewood Please, send your abuse here!!! send.your.abuse.here@gmail.com

    Posted by daxdsnv — 01 Dec 2009, 03:39

  2. christmas carols sheet music flute cheap personalized christmas ornaments in bulk free funny christmas cards to print out free printable christmas coloring pages free printable santa claus letters christmas letter childrens music cds free santa claus letters christmas lights for sale canada printable christmas word searches puzzles santa baby 2 wiki christmas songs and lyrics cheap artificial xmas trees german christmas crafts for children christmas songs lyrics music email xmas cards free martha stewart weddings fall 2004 issue christmas nativity cliparts clean funny christmas cartoons cartoon christmas tree pictures music feature audio wham last christmas lyrics song lyrics

    Posted by xwlsfjn — 01 Dec 2009, 16:50

  3. buy cheap artificial christmas trees asheville nc christmas tree farms free 800x600 holiday wallpaper free photo christmas cards printable disney christmas screensavers and wallpapers christmas ornaments to make for kids christmas jokes for kids holiday free trivia questions online funny christmas cards online free ecards funny xmas songs lyrics christmas gift ideas for men boyfriend frosty the snowman lyrics unique custom christmas photo cards clean funny christmas jokes clean humor extra large outdoor christmas lights homemade christmas gift ideas for kids free christmas carols for kids free download christmas bingo for kids free preschool christmas printables free country decorations catalogs

    Posted by ejnekwx — 03 Dec 2009, 16:27

  4. buy cheap artificial christmas trees asheville nc christmas tree farms free 800x600 holiday wallpaper free photo christmas cards printable disney christmas screensavers and wallpapers christmas ornaments to make for kids christmas jokes for kids holiday free trivia questions online funny christmas cards online free ecards funny xmas songs lyrics christmas gift ideas for men boyfriend frosty the snowman lyrics unique custom christmas photo cards clean funny christmas jokes clean humor extra large outdoor christmas lights homemade christmas gift ideas for kids free christmas carols for kids free download christmas bingo for kids free preschool christmas printables free country decorations catalogs

    Posted by ripdpdg — 03 Dec 2009, 16:38

  5. vancouver 2010 olympics dates winter olympics 2010 canada hockey clark county washington gis property portland oregon newspaper classifieds pets selling vancouver 2010 tickets ctv vancouver 2010 medals jp canada adidas vancouver marathon results 2005 vancouver 2010 mascots unveiled winter olympics vancouver bc north vancouver canada hotels vancouver 2010 video game wiki vancouver 2010 winter olympics vancouver 2010 mascots coloring pages winter olympics 2010 vancouver washington real estate agent vancouver washington lake home vancouver 2010 tickets city of battle ground washington vancouver 2010 mascots miga

    Posted by lvfaltq — 05 Dec 2009, 16:42

  6. zBAxEo rmmwxportlpk, [url=http://rofvnkvonkzk.com/]rofvnkvonkzk[/url], [link=http://dgdpoqsjiiwe.com/]dgdpoqsjiiwe[/link], http://dinbuddjmtbx.com/

    Posted by xbfhmd — 06 Dec 2009, 09:19

  7. 9KnlhG fuuccfvcvzoi, [url=http://csdhrlduugdo.com/]csdhrlduugdo[/url], [link=http://exmoicyvokhw.com/]exmoicyvokhw[/link], http://jitxvixpscss.com/

    Posted by htbjsqsgycm — 06 Dec 2009, 09:33

  8. vancouver 2010 winter olympics committee west seattle herald obituaries 2007 vancouver sun run portland oregon newspaper classifieds clark county washington records search winter olympics vancouver 2010 dates vancouver island real estate listings oregon map vancouver games 2010 jobs vancouver 2010 logo restaurants vancouver british columbia canada winter olympics vancouver 2010 city of vancouver washington jobs vancouver 2010 olympics logo winter olympics vancouver bc 2010 road map vancouver island winter olympics 2010 packages clark county washington home vancouver 2010 winter olympics committee hbc vancouver 2010 apparel

    Posted by tembxqi — 06 Dec 2009, 16:29

  9. city of ridgefield washington jobs buy winter olympics 2010 tickets north vancouver island map vancouver 2010 olympics volunteers vanoc craigslist vancouver canada clark county map washington state vancouver island map canada vancouver 2010 mascots mukmuk winter olympics vancouver bc 2010 portland me newspaper real estate winter olympics 2010 figure skating schedule 2010 winter olympics vancouver bc craigslist vancouver bc metro vancouver newspaper canada winter olympics 2010 ticket sales winter olympics vancouver canada jp canada vancouver washington real estate vancouver sun run training vancouver 2010 tickets for sale

    Posted by vcyrpyv — 07 Dec 2009, 16:28

  10. winter olympics vancouver opening ceremony vancouver washington jobs 2008 winter olympics vancouver 2010 state of washington courts records vancouver marathon 2007 canada northern california southern oregon map craigslist vancouver bc canada apartments las vegas clark county map vancouver 2010 olympics volunteers vanoc vancouver 2010 mascots sumi map vancouver island bc the weather network vancouver bc canada vancouver 2010 olympics logo winter olympics 2010 ticket sales winter olympics vancouver date winter olympics 2010 packages vancouver 2010 logo inukshuk vancouver bc weather vancouver 2010 apparel the bay vancouver 2010 winter olympics tickets

    Posted by egjbqpr — 08 Dec 2009, 20:34

  11. winter olympics vancouver opening ceremony vancouver washington jobs 2008 winter olympics vancouver 2010 state of washington courts records vancouver marathon 2007 canada northern california southern oregon map craigslist vancouver bc canada apartments las vegas clark county map vancouver 2010 olympics volunteers vanoc vancouver 2010 mascots sumi map vancouver island bc the weather network vancouver bc canada vancouver 2010 olympics logo winter olympics 2010 ticket sales winter olympics vancouver date winter olympics 2010 packages vancouver 2010 logo inukshuk vancouver bc weather vancouver 2010 apparel the bay vancouver 2010 winter olympics tickets

    Posted by gkkwdii — 08 Dec 2009, 21:05

  12. vancouver washington hotels heathman portland oregon weather craigslist seattle wa apartments seattle tacoma vancouver 2010 logo inukshuk vancouver 2010 jobs map of vancouver bc british columbia vancouver 2010 mascots miga oregon weather winter olympics vancouver 2010 tickets canada winter olympics vancouver 2010 road map vancouver island bc state of washington courts records vancouver 2010 tickets buy vancouver 2010 apparel the bay vancouver washington lake home the vancouver sun newspaper classifieds vancouver winter olympics 2010 volunteer city of camas washington winter olympics 2010 opening ceremony date vancouver winter olympics 2010 accommodation

    Posted by jhbeeyk — 09 Dec 2009, 21:17

  13. vancouver 2010 olympics hotels vancouver winter olympics 2010 security nursing jobs in vancouver canada employers winter olympics 2010 opening ceremonies date vancouver 2010 medals british columbia map canada weather vancouver british columbia canada craigslist vancouver washington jobs vancouver 2010 mascots games clark county washington assessors office vancouver 2010 mascots coloring pages vancouver 2010 olympics tickets vancouver 2010 logo city of battleground washington jobs 2008 winter olympics vancouver 2010 2010 winter olympics vancouver 2010 olympics vancouver 2010 jobs vancouver washington real estate city of vancouver washington jobs vancouver 2010 olympics dates

    Posted by lcogjsc — 10 Dec 2009, 23:03

  14. vancouver 2010 olympics hotels vancouver winter olympics 2010 security nursing jobs in vancouver canada employers winter olympics 2010 opening ceremonies date vancouver 2010 medals british columbia map canada weather vancouver british columbia canada craigslist vancouver washington jobs vancouver 2010 mascots games clark county washington assessors office vancouver 2010 mascots coloring pages vancouver 2010 olympics tickets vancouver 2010 logo city of battleground washington jobs 2008 winter olympics vancouver 2010 2010 winter olympics vancouver 2010 olympics vancouver 2010 jobs vancouver washington real estate city of vancouver washington jobs vancouver 2010 olympics dates

    Posted by zhydmug — 10 Dec 2009, 23:19

  15. vancouver 2010 apparel hbc what are the names of the vancouver 2010 mascots vancouver games 2010 jobs harris park london ontario canada map white pages vancouver bc canada vancouver sun run registration 2008 winter olympics vancouver 2010 2008 winter olympics vancouver seattle washington weather buy winter olympics 2010 tickets vancouver bc canada zip code list detailed southern oregon map ctv vancouver 2010 medals vancouver island map vancouver 2010 clothing the oregonian vancouver 2010 logo inukshuk craigslist vancouver bc vancouver 2010 mascots coloring pages vancouver 2010 olympics logo

    Posted by sjhyanp — 11 Dec 2009, 22:01

  16. vancouver 2010 apparel hbc what are the names of the vancouver 2010 mascots vancouver games 2010 jobs harris park london ontario canada map white pages vancouver bc canada vancouver sun run registration 2008 winter olympics vancouver 2010 2008 winter olympics vancouver seattle washington weather buy winter olympics 2010 tickets vancouver bc canada zip code list detailed southern oregon map ctv vancouver 2010 medals vancouver island map vancouver 2010 clothing the oregonian vancouver 2010 logo inukshuk craigslist vancouver bc vancouver 2010 mascots coloring pages vancouver 2010 olympics logo

    Posted by tkphqhe — 11 Dec 2009, 22:35

  17. about valium 8-DDD buy xanax skf

    Posted by Tabellengitternetz — 20 Dec 2009, 16:54

  18. tramadol 9612 prednisone roh nexium 086 accutane femz

    Posted by custom — 13 Jan 2010, 11:40

  19. levitra 8-PPP nexium >:-)) accutane 03756 accutane :))) xanax %-O

    Posted by Standaardtabel — 13 Jan 2010, 13:13

  20. tramadol 64694 prednisone 32710 accutane tjezpb xanax %-DDD

    Posted by verdanasuper — 13 Jan 2010, 16:00


Add comment

Add comment

Powered by LifeType
© 2006 - Design by Omar Romero (all rights reserved)