{"id":559,"date":"2012-07-10T11:02:46","date_gmt":"2012-07-10T03:02:46","guid":{"rendered":"http:\/\/rmohan.com\/?p=559"},"modified":"2012-07-10T11:02:46","modified_gmt":"2012-07-10T03:02:46","slug":"securing-ssh","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=559","title":{"rendered":"Securing SSH"},"content":{"rendered":"<h2>Securing SSH<\/h2>\n<p>&nbsp;<\/p>\n<p>SH is how most administrators connect to their servers. It is also one of the most commonly attacked ports on a Linux Server. If you followed my previous tutorial about how to install <a href=\"http:\/\/www.syntaxtechnology.com\/?p=240\">fail2ban<\/a>, you\u2019ve probably noticed that you receive many emails about failed attacks. In this tutorial, I\u2019ll show a few more steps that can be taken to lock down the SSH daemon and your server even further.<br \/>\nBefore we begin, I\u2019d like to show you a few stats about your server. The following commands will show you some interesting information about the brute force attacks you\u2019ve been noticing on your server.<\/p>\n<p>First \u2013 Show the 5 most recently attacked user accounts on your system. In this list you may notice user accounts that don\u2019t even exist on your system. That is because someone is trying automated attacks against you:<\/p>\n<div>\n<div id=\"highlighter_339879\">\n<div><\/div>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>lastb | awk '{print $1}' | sort | uniq -c | sort -rn | head -5<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Next \u2013 Show the 5 most attacked accounts. Again, user accounts that don\u2019t exist may be in this list.<\/p>\n<div>\n<div id=\"highlighter_202038\">\n<div><\/div>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>awk 'gsub(\".*sshd.*Failed password for (invalid user )?\", \"\") {print $1}' \/var\/log\/secure* | sort | uniq -c | sort -rn | head -5<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Finally \u2013 Show the 5 most frequent attacker IP addresses. These are addresses that attempt to connect to your server.<\/p>\n<div>\n<div id=\"highlighter_157772\">\n<div><\/div>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>awk 'gsub(\".*sshd.*Failed password for (invalid user )?\", \"\") {print $3}' \/var\/log\/secure* | sort | uniq -c | sort -rn | head -5<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p><strong>Securing SSH<\/strong><\/p>\n<p>Now that you can see what\u2019s coming at your server, what can you do about it? Below are a few steps you can take to secure SSH.<\/p>\n<div>\n<div id=\"highlighter_744834\">\n<div><\/div>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>vi \/etc\/ssh\/sshd_config<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>This is the main configuration file for SSH. All of our changes will be in here.<\/p>\n<p>The first setting we are looking for is <strong>Protocol<\/strong>. We want this changed to a <strong>2<\/strong>. Most modern Linux Distributions already have this by default, but some may still allow the first version of the protocol to connect. We don\u2019t want this.<\/p>\n<div>\n<div id=\"highlighter_491578\">\n<div><\/div>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>Protocol 2<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Next, we are going to deny <strong>root<\/strong> the ability to log in via SSH. Root doesn\u2019t need direct access, because we have already set up <a href=\"http:\/\/www.syntaxtechnology.com\/?p=31\">sudo<\/a>. Find the <strong>PermitRootLogin<\/strong> setting and change it to <strong>no<\/strong>.<\/p>\n<div>\n<div id=\"highlighter_302385\">\n<div><\/div>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>PermitRootLogin no<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>The next step is to limit the amont of time an unauthenticated session can hold open a connection. By default this is two minutes. This is way to long. Find the <strong>GraceLoginTime<\/strong> setting and change it to a more reasonable time. The value listed here is in seconds. The example below allows 30 seconds for a user to enter their password before the connection is closed.<\/p>\n<div>\n<div id=\"highlighter_156889\">\n<div><\/div>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>LoginGraceTime 30<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>The next one is to change the SSH port. It should be noted that this step brings no additional security to your system at all. It will, however, reduce the number of random, automated attacks that hit your server. Again, it will NOT bring additional security to your system. Find the <strong>Port<\/strong> setting and change it to another port. Common practice is to raise this above 1024, as everything below that is reserved for other programs.<\/p>\n<div>\n<div id=\"highlighter_170300\">\n<div><\/div>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>Port 22222<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Now when you connect to your server, you will need to modify your connection port to use 22222.<\/p>\n<p>Next, we can set up SSH to only allow whitelisted users or groups. The following will only allow users \u2018mary\u2019, \u2018john\u2019 and any user that starts with \u2018joe\u2019 to conenct. This line gets placed at the end of the file:<\/p>\n<div>\n<div id=\"highlighter_419408\">\n<div><\/div>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>AllowUsers john mary joe*<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>This setting, alternatively, will allow all users from the \u2018sshusers\u2019 group to login<\/p>\n<div>\n<div id=\"highlighter_470704\">\n<div><\/div>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>AllowGroups sshusers<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Finally, we can only allow users to log in using public\/private key pairs. How to set this up is beyond the scope of this tutorial, so if you don\u2019t know how to do so, do not change this setting:<\/p>\n<div>\n<div id=\"highlighter_205928\">\n<div><\/div>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>PasswordAuthentication no<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>Save and exit this file. Now we restart <strong>sshd<\/strong> and you are good to go.<\/p>\n<p>Note: Do not log out of your active SSH session after running this command until you have tested that you can connect. If you do and something does not work, you will be locked out of your server.<\/p>\n<div>\n<div id=\"highlighter_404168\">\n<div><\/div>\n<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n<tbody>\n<tr>\n<td>\n<div>\n<div><code>service sshd restart<\/code><\/div>\n<\/div>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/div>\n<\/div>\n<p>If you are able to log in using another Putty or SSH session, your changes have worked. Remember when you log in, if you changed your Port, you need to specify the new port.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Securing SSH <\/p>\n<p>&nbsp;<\/p>\n<p>SH is how most administrators connect to their servers. It is also one of the most commonly attacked ports on a Linux Server. If you followed my previous tutorial about how to install fail2ban, you\u2019ve probably noticed that you receive many emails about failed attacks. In this tutorial, I\u2019ll show a few [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/559"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=559"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/559\/revisions"}],"predecessor-version":[{"id":560,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/559\/revisions\/560"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}