{"id":479,"date":"2012-08-28T02:50:51","date_gmt":"2012-08-28T08:50:51","guid":{"rendered":"http:\/\/blog.the-erm.com\/?p=479"},"modified":"2012-08-28T02:57:41","modified_gmt":"2012-08-28T08:57:41","slug":"how-to-setup-an-sftpssh-server-with-denyhosts-on-ubuntu","status":"publish","type":"post","link":"https:\/\/blog.the-erm.com\/?p=479","title":{"rendered":"How to setup an sftp\/ssh server with denyhosts on ubuntu"},"content":{"rendered":"<p>Throughout this document I will have &lt;keywords&gt; encapsulated in brackets. Substitute what the keyword is for the appropriate value. For instance &lt;username&gt;@localhost would become erm@localhost, or whatever your username is.<\/p>\n<p>Cliffnotes version commands:<\/p>\n<p>Install open ssh:<br \/>\n<code>$ sudo apt-get install openssh-server<\/code><\/p>\n<p>Backup ssh config:<br \/>\n<code>$ sudo cp \/etc\/ssh\/sshd_config \/etc\/ssh\/sshd_config.`date +%Y-%m-%d`.bak <\/code><\/p>\n<p>Edit \/etc\/ssh\/sshd_config:<br \/>\n<code> $ sudo nano \/etc\/ssh\/sshd_config<\/code><\/p>\n<p>Enable sftp:<br \/>\n<code>Subsystem sftp \/usr\/lib\/openssh\/sftp-server<\/code><br \/>\n*save*<\/p>\n<p>Restart ssh:<br \/>\n<code>$ sudo \/etc\/init.d\/ssh restart<\/code><br \/>\n<b>OR<\/b><br \/>\n<code>$ sudo service ssh restart<\/code><\/p>\n<p>Test your server:<br \/>\n<code>$ ssh &lt;username&gt;@localhost<\/code><\/p>\n<p>Test your connection:<br \/>\n<code>$ echo $SSH_CLIENT<\/code><\/p>\n<p>Log out of ssh:<br \/>\n<code>$ &lt;ctrl+d&gt; <\/code><br \/>\n<strong>OR<\/strong><br \/>\n<code>$ exit <\/code><\/p>\n<p>Install denyhosts:<br \/>\n<code>$ sudo apt-get install denyhosts <\/code><\/p>\n<p>Backup denyhosts config:<br \/>\n<code>$ sudo cp \/etc\/denyhosts.conf \/etc\/denyhosts.conf.`date +%Y-%m-%d`.bak <\/code><\/p>\n<p>Edit denyhosts config:<br \/>\n<code>$ sudo nano \/etc\/denyhosts.conf <\/code><\/p>\n<p>Edit lines:<br \/>\n<code>BLOCK_SERVICE = ALL<\/code><br \/>\n<code>DENY_THRESHOLD_INVALID = 5<\/code><br \/>\n<code>DENY_THRESHOLD_RESTRICTED = 1<\/code><br \/>\n<code>RESET_ON_SUCCESS = yes<\/code><br \/>\n*save*<\/p>\n<p>Restart denyhosts:<br \/>\n<code>$ sudo \/etc\/init.d\/denyhosts restart<\/code><br \/>\n<strong>OR<\/strong><br \/>\n<code>$ sudo service denyhosts restart<\/code><\/p>\n<p>Get your remote ip:<br \/>\nVisit: <a href=\"http:\/\/ipchicken.com\">ipchicken.com<\/a> or <a href=\"hhttp:\/\/www.whatismyip.com\/\">whatismyip.com<\/a><\/p>\n<p>Get your lan\/local ip:<br \/>\n<code>$ ifconfig<\/code><\/p>\n<p>Login via remote computer from inside lan:<br \/>\n<code>$ ssh &lt;username&gt;@&lt;localip&gt;<\/code><\/p>\n<p>Login via remote computer from outside lan:<br \/>\n<code>$ ssh &lt;username&gt;@&lt;remoteip&gt;<\/code><\/p>\n<p><!--more--><\/p>\n<hr \/>\n<p>Here&#8217;s the long drawn out version:<\/p>\n<p>Install openssh.<br \/>\n<code>$ sudo apt-get install openssh-server<\/code><\/p>\n<p>All system wide ssh related files are located in <code>\/etc\/ssh\/<\/code>.<\/p>\n<p>Before you start editing your sshd_config file it&#8217;s always a good idea to backup. This will create a backup of your current config file. The end result will look like \/etc\/ssh\/sshd_config.YYYY-MM-DD.bak so you can easily sudo cp it back if you need to.<br \/>\n<code>$ sudo cp \/etc\/ssh\/sshd_config \/etc\/ssh\/sshd_config.`date +%Y-%m-%d`.bak <\/code><\/p>\n<p>When editing config files it&#8217;s always a good idea to comment out the original values, and then add a date. You could also add your initials if you think someone else might come along and change it. There&#8217;s also the advantage of telling your future self what it was, and why you changed it. It&#8217;s always better than coming along and asking yourself WTF was I thinking?!  Some server admins even go as far as to using a version control system.<br \/>\n<code><br \/>\n# Original value:<br \/>\n# VALUE = 0<br \/>\n#########<br \/>\n# Changed: 2012-08-27 ERM<br \/>\n# Notes on why you changed it. So someone else doesn't come<br \/>\n# along and undo your changes.<br \/>\n# ADD SOME CAPS IF YOU'RE REALLY SERIOUS ABOUT IT.<br \/>\n#########<br \/>\nVALUE = 1<br \/>\n<\/code><br \/>\n<code>$ sudo nano \/etc\/ssh\/sshd_config<\/code><\/p>\n<p>Make sure this line is present and doesn&#8217;t have a # in front of it.<br \/>\n<code>Subsystem sftp \/usr\/lib\/openssh\/sftp-server<\/code><br \/>\n*save*<\/p>\n<p>Restart ssh so the changes take affect:<br \/>\n<code>$ sudo \/etc\/init.d\/ssh restart<\/code><br \/>\n<b>OR<\/b><br \/>\n<code>$ sudo service ssh restart<\/code><\/p>\n<p>If you don&#8217;t know what your username is you can execute:<br \/>\n<code>$ echo $USER<\/code><\/p>\n<p>By default ssh is on port 22. Test the connection (this will ask you to enter your password.):<br \/>\n<code>$ ssh &lt;username&gt;@localhost<\/code><\/p>\n<p>If this works you will have a connection to your local host via ssh.<\/p>\n<p>To test if you are in-fact logged in via ssh you can execute the following:<br \/>\n<code>$ echo $SSH_CLIENT<\/code><\/p>\n<p>The output will look something like this:<br \/>\n<code>127.0.0.1 33493 22<\/code><br \/>\nThat means your connected from 127.0.0.1 (localhost).<\/p>\n<p>Quick Tip: You can press ctrl+d on an empty command line to quickly logout. This is the equivalent of typing:<br \/>\n<code>$ exit<\/code><\/p>\n<p><strong>You may not need denyhosts if you&#8217;re using ssh to connect from within your lan.<\/strong> However if you are opening up port 22 and forwarding it to a computer inside your lan you most definitely should. In fact if you don&#8217;t install denyhosts or fail2ban you&#8217;re pretty much doing the equivalent of putting a sign outside your house\/apartment that says &#8220;I&#8217;ll buy anything&#8221;.<\/p>\n<p>Install denyhosts.<br \/>\n<code>$ sudo apt-get install denyhosts<\/code><\/p>\n<p>It&#8217;s very important to note that denyhosts is a double edged sword. <strong>Too many failed login attempts and you lock yourself out.<\/strong> The good news is this also keeps pesky bots\/crackers from entering the wrong username\/password too many times. Which will save you bandwidth. Your desired configuration may vary, but I&#8217;ll go over my configuration. The configuration for denyhosts is located in <code>\/etc\/denyhosts.conf<\/code> <strong>read it.<\/strong> There are a lot of options, and I&#8217;m not going to go into them here. It&#8217;s your job to make sure your system is secure.<\/p>\n<p>Backup denyhosts.conf like we did before with sshd_config<br \/>\n<code>$ sudo cp \/etc\/denyhosts.conf \/etc\/denyhosts.conf.`date +%Y-%m-%d`.bak<\/code><\/p>\n<p>To edit the config file you can use vi, vim, nano, emacs or my favourite gedit. For the sake of ease of use I&#8217;ll use nano.<br \/>\n<code>$ sudo nano \/etc\/denyhosts.conf <\/code><\/p>\n<p>If you get a message stating &lt;editor&gt; isn&#8217;t installed you can install it with:<br \/>\n<code>$ sudo apt-get install &lt;editor&gt;<\/code><\/p>\n<p>I don&#8217;t like people trying to crack into my server. I think if they are willing to try and hack my ssh port with a dictionary attack, then they are going to do the same to try other attacks as well. So no http, no imap. Nothing. I&#8217;m not going to talk to that client any more. You might not be willing to take the risk, but I say it&#8217;s too much of a risk not to completely block them.<br \/>\n<code>BLOCK_SERVICE = ALL<\/code><\/p>\n<p>I have a pretty complex password, and I think 5 times is enough to get it right.<br \/>\n<code>DENY_THRESHOLD_INVALID = 5<\/code><\/p>\n<p><strong>NEVER<\/strong> log into your root account via ssh. Login as yourself, then run <code>$ sudo su -<\/code> to become root. For the most part I don&#8217;t do that unless I know I&#8217;m going to need it usually I run <code>$ sudo &lt;command&gt;<\/code> So if someone tries to log in as root. Instaband for even 1 failed attempt.<br \/>\n<code>DENY_THRESHOLD_RESTRICTED = 1<\/code><\/p>\n<p>Now this next setting I decided to compromise a little convenience for security. So if I get a successful login it resets the bad login count.<br \/>\n<code>RESET_ON_SUCCESS = yes<\/code><\/p>\n<p>As I stated before you should read over the config file. One nice thing about denyhosts is the creator added a community driven db. So any time you deny a host you can opt-in to send the ip address to the denyhosts community and share that info. You can also get the list of ips of recent attackers on other machince. This of course can come at a price, and people could report your ip then you&#8217;ll get locked out of your own system. I haven&#8217;t had a problem so I have those options enabled.<\/p>\n<p>Now that you&#8217;re done you need to restart the denyhosts to load the new settings.<br \/>\n<code>$ sudo \/etc\/init.d\/denyhosts restart<\/code><br \/>\n<strong>or<\/strong><br \/>\n<code>$ sudo service denyhosts restart<\/code><\/p>\n<p>Both of those commands do exactly the same thing, the difference is I like using <code>$ sudo \/etc\/init.d\/denyhosts restart<\/code> and I get a lot of warnings &#8230; &#8220;you no should do&#8221; &#8230; &#8220;you use service!&#8221; So in the spirit of doing things the &#8220;right&#8221; way I figured I better mention the <code>service<\/code> command in hopes that it future proofs this tutorial.<\/p>\n<p>Now that we have denyhosts setup you need to log into your router and forward all port 22 traffic to the machine you just installed ssh on. The easiest way to learn how to do that is go to google type in the make &amp; model of your router and the keywords &#8220;port forward&#8221;. Due to the diversity of routers in the world I&#8217;m not going to cover that here. As stated before, if you&#8217;re using ssh to connect to 2 machines inside the lan you can skip installing denyhosts and forwarding your ip.<\/p>\n<p>Before we continue I should probably tell you about ip addresses. The machine you&#8217;re currently on is always 127.0.0.1. This is called &#8220;localhost&#8221;. Lan ip ranges are as follows:<br \/>\n<code><br \/>\n10.0.0.0 - 10.255.255.255<br \/>\n172.16.0.0 - 172.31.255.255<br \/>\n192.168.0.0 - 192.168.255.255<br \/>\n<\/code><\/p>\n<p>I have never once ran into a router that used any ip ranges that didn&#8217;t start with <code>192.168<\/code>. Bottom line remember <code>192.168<\/code> it&#8217;s lan.<\/p>\n<p>The next step is to determine your ip address. To determine what your ip address is to the rest of the world visit <a href=\"http:\/\/ipchicken.com\/\">ipchicken.com<\/a> or <a href=\"http:\/\/www.whatismyip.com\/\">whatismyip.com<\/a><\/p>\n<p>To determine what your ip address is on the lan you can use the command <strong>ifconfig<\/strong>. To only display important lines I&#8217;ve added a grep. You should run ifconfig without the grep. <code>$ ifconfig<\/code> to get all your interfaces.<br \/>\n<code><br \/>\n$ ifconfig | grep 'eth\\|wlan\\|inet addr\\:\\|lo'<br \/>\n<strong>eth0<\/strong> Link encap:Ethernet HWaddr 00:23:8b:8b:7c:7b<br \/>\ninet addr:<strong>69.145.29.50<\/strong> Bcast:69.145.29.51 Mask:255.255.255.252<br \/>\n<strong>eth1<\/strong> Link encap:Ethernet HWaddr 68:7f:74:2b:60:29<br \/>\ninet addr:<strong>10.1.3.1<\/strong> Bcast:10.1.3.255 Mask:255.255.255.0<br \/>\n<strong>lo<\/strong> Link encap:Local Loopback<br \/>\ninet addr:<strong>127.0.0.1<\/strong> Mask:255.0.0.0<br \/>\n<strong>wlan0<\/strong> Link encap:Ethernet HWaddr 00:24:2b:bf:fc:5a<br \/>\n<\/code><\/p>\n<p>This ifconfig comes from my netbook. I have 4 different ethernet &#8220;nics&#8221; on\/in it. One is built in eth0 and the other one is a usb dongle eth1. lo is a loopback for localhost, and wlan0 is my wireless card. My wireless card is disabled so it doesn&#8217;t show an ip.  I use my netbook as a router\/server of sorts. So I can run <code>$ sudo iftop -i eth0<\/code> and <code>$ sudo iftop -i eth1<\/code> and a transparent proxy with dansguardian.<\/p>\n<p>Just looking at the ip addresses I can tell which is local, lan or net. <strong>69.145.29.50<\/strong> is what the rest of the world accesses my netbook, and it uses the interface <strong>eth0<\/strong>.<\/p>\n<p><strong>10.1.3.1<\/strong> is my eth1&#8217;s ip. This is the ip I use to connect from inside the lan.  In your case it&#8217;s most likely prefixed with 192.168.<br \/>\n<code>ssh &lt;username&gt;@&lt;localip&gt;<\/code><\/p>\n<p><strong>69.145.29.50<\/strong> is my eth-&#8216;s ip. This is the ip I use to connect from outside the lan.  If you don&#8217;t have a static ip address it can change at any time.  So you&#8217;ll need to setup up dynamic dns or visit <a href=\"http:\/\/whatismyip.com\">whatismyip.com<a \/>\/<\/a><a href=\"http:\/\/ipchicken.com\">ipchicken.com<\/a> to discover what your outside ip is.  You could pay your isp for a static ip.  Keep in mind setting up a dynamic dns may be in violation of your ISP&#8217;s TOS.<br \/>\n<code>ssh &lt;username&gt;@&lt;remoteip&gt;<\/code><\/p>\n<h2>Troubleshooting:<\/h2>\n<p>The first thing you should do is open a terminal window\/tab and run this:<br \/>\n<code>$ sudo tail -f \/var\/log\/syslog<\/code><br \/>\nThis will show you any login attempts, and any other errors.<\/p>\n<p>The next thing you should do is open up another tab\/terminal and restart your ssh server.<br \/>\n<code>$ sudo service ssh restart<\/code><br \/>\nYou should see it restart in the other window that you issued the tail -f command in.<\/p>\n<p>Check that the ethernet cord is connected.  No seriously get up, and make sure that ethernet cord is connected.  No I know you have it connected, but seriously check it.  You didn&#8217;t check it did you. Get up check it.  Then restart your server.<\/p>\n<p>Check ifconfig.  Is the ip you&#8217;re trying to connect to listed?  If not you need to restart your network <code>$ sudo service networking restart <\/code> or click &#8220;Auto Ethernet&#8221; from your trusty network applet.  Then restart the server.<\/p>\n<p>Check \/etc\/hosts.deny is the ip you&#8217;re trying to connect from listed?  If it&#8217;s listed you entered the wrong password too many times. <a href=\"http:\/\/denyhosts.sourceforge.net\/faq.html#3_19\">Here are instructions on how to remove them.<\/a><br \/>\n<code>$ sudo grep &lt;clientip&gt; \/etc\/hosts.deny<\/code><\/p>\n<p>Another issue I&#8217;ve ran into in the past is I have both a wireless and a wired connection.  This causes havoc with the routing.  Choose one or the other.  Usually I have wireless off.  Wired is so much more reliable.  I only enable wireless on devices that need it like a tablet, or a phone.  This will help keep wifi congestion down to a minimum.  To make sure ssh is listening on both interfaces you can:<br \/>\n<code>$ sudo nano \/etc\/ssh\/ssh_config<\/code><br \/>\nAdd\/Edit:<br \/>\n<code>Host *<\/code><br \/>\n*save*<br \/>\nThen restart your ssh server.<br \/>\n<code>$ sudo service ssh restart<\/code><br \/>\nEven with that enabled there is no guarantee you&#8217;ll be able to connect to the ssh server on both your wired &#038; wireless ips.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Throughout this document I will have &lt;keywords&gt; encapsulated in brackets. Substitute what the keyword is for the appropriate value. For instance &lt;username&gt;@localhost would become erm@localhost, or whatever your username is. Cliffnotes version commands: Install open ssh: $ sudo apt-get install openssh-server Backup ssh config: $ sudo cp \/etc\/ssh\/sshd_config \/etc\/ssh\/sshd_config.`date +%Y-%m-%d`.bak Edit \/etc\/ssh\/sshd_config: $ sudo nano [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,109],"tags":[],"class_list":["post-479","post","type-post","status-publish","format-standard","hentry","category-linux-2","category-tutorial"],"_links":{"self":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts\/479","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=479"}],"version-history":[{"count":8,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts\/479\/revisions"}],"predecessor-version":[{"id":487,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=\/wp\/v2\/posts\/479\/revisions\/487"}],"wp:attachment":[{"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.the-erm.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}