APLawrence.com -  Resources for Unix and Linux Systems, Bloggers and the self-employed

Security Paranoia - restricting ssh access

I had email from someone today whose system was hacked, apparently by a dictionary attack over ssh. They overwrote most of his hard drive with "DIE SCO DIE". He said there were "over 8000 root login attempts and another 6000 for various other names".

Several things come to mind immediately. First, you shouldn't allow root logins over ssh anyway. The

#PermitRootLogin yes

in /etc/ssh/sshd_config should be uncommented and changed to no. Second, you should have an AllowUsers line, and if possible, limit that to names that aren't obvious. For example, I wouldn't use "tony" or "apl". I might use "mghtrey" (though I don't), because that's a completely made up name. With that line, only the listed users are allowed to login with ssh at all. That includes root even if PermitRootLogin isn't turned off, but I set both anyway. Exclusionary duplication never hurts.

Next, don't have passwords susceptible to dictionary attack. Letters, numbers, and punctuation are a must. Do NOT use the same passwords on multiple servers and do not allow user equivalency or ssh_agent unless you absolutely have to have it.

Finally, don't let someone have 8,000 chances at guessing your password. I have two limitations set. One is the

MaxStartups 10:30:60

which (from the man page):

             Specifies the maximum number of concurrent unauthenticated con-
             nections to the sshd daemon.  Additional connections will be
             dropped until authentication succeeds or the LoginGraceTime
             expires for a connection.  The default is 10.

             Alternatively, random early drop can be enabled by specifying the
             three colon separated values ``start:rate:full'' (e.g.,
             "10:30:60").  sshd will refuse connection attempts with a proba-
             bility of ``rate/100'' (30%) if there are currently ``start''
             (10) unauthenticated connections.  The probability increases lin-
             early and all connection attempts are refused if the number of
             unauthenticated connections reaches ``full'' (60).

But I don't stop there on my most critical servers. I also have

auth required /lib/security/$ISA/pam_tally.so no_magic_root onerr=fail
account    required      /lib/security/$ISA/pam_tally.so onerr=fail
file=/var/log/faillog deny=1 no_magic_root even_deny_root_account

in my /etc/pam.d/system-auth file. That kills login if you type a bad password just twice. I reset it with a cron job every hour during the day when I'm working in case I screw up twice (" /sbin/pam_tally --reset") but not at night.

So, for someone to break in with ssh, they first have to know the one account that allows ssh at all and they have exactly two chances to guess the password - miss those two and pam locks them out. Keep on trying, and ssh itself will also lock you out eventually.

You really cannot be too careful now. If you don't need ssh outside of working hours, turn it off with a cron job. The "bad guys" are constantly hammering at our servers; there's no reason to make it easy for them.

Got something to add? Send me email.

(OLDER)    <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Security Paranoia - restricting sshaccess


Increase ad revenue 50-250% with Ezoic

More Articles by

Find me on Google+

© Tony Lawrence

---October 26, 2004

Thanks for the last part of the article. I had already done the first few things, but locking them out after two bad password attempts is a great idea. My logs are filled with ssh hacking on a daily basis now. Mostly, someone is trying to login as root, which I do not allow anyway, and now I see attempts for other usernames. I just recently changed my regular username password to something much harder than I had before. Also, the IP addresses from the host that is doing all this hacking, is never resolvable with the 'host' command. I still blacklist their IP in my firewall, but I am wondering if there is some IP spoofing going on as well.

Thanks again for the good ideas, Tony!

- Bruce Garlock

---October 26, 2004

I did forget to mention the final step - blacklist 'em after you see them in the logs. That step can even be automated, though between spoofing and constantly changing addresses, I don't do that until I've seen them more than once.


---October 26, 2004

I seem to have the same jerk, using the same login names, from different IP addresses. What is the easiest way to play bad guy, and spoof your IP address, so I can test my defenses? Is there a way I can spoof my IP, and try to crack my own machine to make sure everything is working? I guess I need some direction on being the bad guy, to make sure the defenses I am putting up, are working. What about running ssh through xinetd, and using tcpwrappers, and sending a banner with a warning? Can that be done?

- Bruce Garlock

---October 26, 2004

Looks like I found the answer to my question about tcp wrappers here:


But, how would one go about being a bad guy, and spoofing my IP address to connect to the ssh port on my machine?

- Bruce Garlock

---October 26, 2004

See http://www.iss.net/security_center/advice/Underground/Hacking/Methods/Technical/Spoofing/default.htm


---October 26, 2004

If sshd is compiled with LIBWRAP, you don't need tcpd - it will check /etc/hosts.allow itself



---October 27, 2004

I have a feeling in my searching for how to hack my own system by spoofing my IP address, that I will be brought to places I never knew or thought would exist. Although I have always taken the preventative stance when it comes to security, and done a little hacking to make sure things are safe, I never fully used the tools of the crackers. I am sure there are some c files out there to help with the process, and I will probably be inventing a few of my own. Usually running nmap on a new host, and setting passwords, and access permissions properly would be my first line of defense, but I have never taken on the role of the bad guy. In high school, I always found the holes into our schools system, and reported them. Although I think my server is as good as I can secure it, it will never be secure enough. I always keep binaries patched, and deploy patches as fast as I can after a security announcment is made, but I have never tried to brute force crack on any of my services. Since this is apparantly what the clown who is trying to break into my system is doing, I figured I would look at my system as he does, to see what he can see. I honestly doubt I will get anywhere, since I use special characters, numbers, and letters in my password, and that is the only account allowed to log in remotely. This should be interesting :-)

- Bruce Garlock

---October 27, 2004

Why not disable password authentication completely? It's the weakest form of authentication that OpenSSH supports.

You disable password authetication, set the allowed hosts if you want, and depend on a PubkeyAuthentication method that is supported by version 2 of the SSH protocol.

That way even if a person figured out your password they would still not be able to login thru ssh. You simply cary around your private half of the keypair on a usb keychain device and use the passphrase option. That way even if they get your private key, they still need to get your passphrase, which can be different from your normal login password.


---January 13, 2005

Thanks! This article has been extremely helpful. I get hack attempts nightly and put all your tips in place and it works like a charm.
Jason Russo

---January 27, 2005

Very useful, just what I was hunting for. Also an option for some folks that may be helpful (although with creatures like nmap, not against anyone with more than trivial knowledge) is changing the port number with the 'Port ##' line option. Thanks for the useful info!
Landis Vinchattle

Thu Feb 24 11:14:37 2005: 63   TonyLawrence

One thing I forgot to mention:

When the user gets locked out like this, attempts to login with the wrong password still get logged. If they subsequently give the RIGHT password, that doesn't get logged , but they still don't get in.

Wed Jun 8 20:52:59 2005: 634   anonymous

DenyHosts is a nice little script that reads through your authentication logs and adds attackers to your /etc/hosts.deny file. Very nice, simple, effective.

Sat Jul 22 08:13:36 2006: 2276   JimKeller

If you are the only one (or just about the only one) logging into your servers via SSH, it might be good to make MaxStartups even more restrictive than the default. MaxStartups does not limit the number of SSH connections you can have open, just the number of "concurrent unauthenticated connections". As such, if you expect very few simultaneous SSH connections, you might want to do something like MaxStartups 3:70:20 instead. Essentially you can set the first number to be the number of people you expect to ever be trying to connect at once. The only downside of making this directive more restrictive is that valid login attempts will be temporarily rejected during an attack if there are "limit" (the last number) connections pending. However, since MaxStartups only applies to unauthenticated connections, they will close very quickly and you should be able to get in without much of a wait.

Sat Jul 22 09:28:07 2006: 2277   TonyLawrence

Good tip, Jim, thanks!

Thu Aug 17 21:36:53 2006: 2407   anonymous

as mentioned use public key authentication. the absolute worst thing you can do is allow someone to DoS you out of your own box, its fine if its a home system; if its colocated you will cry.

Kerio Connect Mailserver

Kerio Samepage

Kerio Control Firewall

Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.

Contact us

A common and not necessarily apocryphal example portrays a solo practitioner starved for business in a small town. A second lawyer then arrives, and they both prosper. (Deborah L. Rhode)

Anyone who puts a small gloss on a fundamental technology, calls it proprietary, and then tries to keep others from building on it, is a thief. (Tim O'Reilly)

This post tagged: