How to block IP in ssh












1















A very insistent individual is trying continuously to gain (unauthorized) access to my system for the last few days... There are a gazillion of entries, like in /var/log/auth.log:



Jun 21 03:55:15 cloudy sshd[32487]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=116.31.116.20  user=root
Jun 21 03:55:16 cloudy sshd[32487]: Failed password for root from 116.31.116.20 port 30629 ssh2
Jun 21 03:55:19 cloudy sshd[32487]: Failed password for root from 116.31.116.20 port 30629 ssh2
Jun 21 03:55:21 cloudy sshd[32487]: Failed password for root from 116.31.116.20 port 30629 ssh2
Jun 21 03:55:21 cloudy sshd[32487]: Received disconnect from 116.31.116.20: 11: [preauth]
Jun 21 03:55:21 cloudy sshd[32487]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=116.31.116.20 user=root


I would like to ask for some advice on how to block this individual from consuming resources from the computer, is there a possibility to "hard block" the IP from even opening an ssh connection? Please note, I use Debian 8 as OS.










share|improve this question



























    1















    A very insistent individual is trying continuously to gain (unauthorized) access to my system for the last few days... There are a gazillion of entries, like in /var/log/auth.log:



    Jun 21 03:55:15 cloudy sshd[32487]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=116.31.116.20  user=root
    Jun 21 03:55:16 cloudy sshd[32487]: Failed password for root from 116.31.116.20 port 30629 ssh2
    Jun 21 03:55:19 cloudy sshd[32487]: Failed password for root from 116.31.116.20 port 30629 ssh2
    Jun 21 03:55:21 cloudy sshd[32487]: Failed password for root from 116.31.116.20 port 30629 ssh2
    Jun 21 03:55:21 cloudy sshd[32487]: Received disconnect from 116.31.116.20: 11: [preauth]
    Jun 21 03:55:21 cloudy sshd[32487]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=116.31.116.20 user=root


    I would like to ask for some advice on how to block this individual from consuming resources from the computer, is there a possibility to "hard block" the IP from even opening an ssh connection? Please note, I use Debian 8 as OS.










    share|improve this question

























      1












      1








      1








      A very insistent individual is trying continuously to gain (unauthorized) access to my system for the last few days... There are a gazillion of entries, like in /var/log/auth.log:



      Jun 21 03:55:15 cloudy sshd[32487]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=116.31.116.20  user=root
      Jun 21 03:55:16 cloudy sshd[32487]: Failed password for root from 116.31.116.20 port 30629 ssh2
      Jun 21 03:55:19 cloudy sshd[32487]: Failed password for root from 116.31.116.20 port 30629 ssh2
      Jun 21 03:55:21 cloudy sshd[32487]: Failed password for root from 116.31.116.20 port 30629 ssh2
      Jun 21 03:55:21 cloudy sshd[32487]: Received disconnect from 116.31.116.20: 11: [preauth]
      Jun 21 03:55:21 cloudy sshd[32487]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=116.31.116.20 user=root


      I would like to ask for some advice on how to block this individual from consuming resources from the computer, is there a possibility to "hard block" the IP from even opening an ssh connection? Please note, I use Debian 8 as OS.










      share|improve this question














      A very insistent individual is trying continuously to gain (unauthorized) access to my system for the last few days... There are a gazillion of entries, like in /var/log/auth.log:



      Jun 21 03:55:15 cloudy sshd[32487]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=116.31.116.20  user=root
      Jun 21 03:55:16 cloudy sshd[32487]: Failed password for root from 116.31.116.20 port 30629 ssh2
      Jun 21 03:55:19 cloudy sshd[32487]: Failed password for root from 116.31.116.20 port 30629 ssh2
      Jun 21 03:55:21 cloudy sshd[32487]: Failed password for root from 116.31.116.20 port 30629 ssh2
      Jun 21 03:55:21 cloudy sshd[32487]: Received disconnect from 116.31.116.20: 11: [preauth]
      Jun 21 03:55:21 cloudy sshd[32487]: PAM 2 more authentication failures; logname= uid=0 euid=0 tty=ssh ruser= rhost=116.31.116.20 user=root


      I would like to ask for some advice on how to block this individual from consuming resources from the computer, is there a possibility to "hard block" the IP from even opening an ssh connection? Please note, I use Debian 8 as OS.







      linux networking ssh






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jun 21 '16 at 8:57









      Ferenc DeakFerenc Deak

      2541519




      2541519






















          5 Answers
          5






          active

          oldest

          votes


















          3














          From http://www.linuxquestions.org/questions/linux-newbie-8/how-to-configure-ssh-to-allow-or-deny-specify-host-ip-address-610246/



          Add sshd: 116.31.116.20 to /etc/hosts.deny






          share|improve this answer



















          • 3





            This won't work for everyone. hosts.deny is part of an external library called TCP Wrappers. The SSH server will only honor this file if tcp wrapper support was included by whoever compiled it. Current versions of OpenSSH no longer contain TCP Wrapper support, although Linux distro maintainers may still be adding it back in to the OpenSSH packages that they distribute.

            – Kenster
            Jun 21 '16 at 14:01



















          1














          You can also use iptables



          Create a file with ip addresses you want to block as blocked.ips.txt



          Now create and run a script containing the following:



          blocked=$(egrep -v -E "^#|^$" ./blocked.ips.txt)
          for ip in $blocked
          do
          iptables -I INPUT -s $ip -p tcp --dport ssh -j DROP
          done


          Running iptables -L should give output of dropped packets






          share|improve this answer































            1














            iptables approach is the most direct way. Erik Handriks proposed it as a script.



            Two but-s:
            - the script would have to be started after every restart, (if tables not backed up internally with iptables).
            - calling the script as is, twice or more will duplicate entries.



            Try simply nailing the bad guy with:




            iptables -I INPUT -s BAD_IP -p tcp --dport ssh -j DROP




            In most distributions, iptables has own rules-save-restore mechanism.
            See if '/var/lib/iptables/rules-save' is present. For your distro, it might sit somewhere else (gentoo here).



            After you add every single BAD IP, try:




            iptables-save > /var/lib/iptables/rules-save




            After restart, check with 'iptables -L' what's out there...






            share|improve this answer

































              0














              You may want to use Fail2Ban to monitor logs and block IP automagically. It is possible to specify time period and fail count to block IP address, inform administrator, send complain to IP owner etc.



              See for more info: https://www.upcloud.com/support/installing-fail2ban-on-debian-8-0/






              share|improve this answer































                0














                On my server (Ubuntu 16.04) I use UFW (UncomplicatedFireWall), a simplification layer over iptables) which seems to be pretty much standard issue on Ubuntu servers (but should be available for other distros). For these cases I use:



                ufw insert 1 deny from nnn.nnn.nnn.nnn


                UFW also allows you to limit the number of connections



                ufw limit ssh/tcp


                Note that this also applies to successful connections, so if you use scripts that use multiple ssh command, you have to disable this limit for your IP by issuing:



                ufw insert 1 allow in from nnn.nnn.nnn.nnn





                share|improve this answer























                  Your Answer








                  StackExchange.ready(function() {
                  var channelOptions = {
                  tags: "".split(" "),
                  id: "3"
                  };
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function() {
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled) {
                  StackExchange.using("snippets", function() {
                  createEditor();
                  });
                  }
                  else {
                  createEditor();
                  }
                  });

                  function createEditor() {
                  StackExchange.prepareEditor({
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader: {
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  },
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  });


                  }
                  });














                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function () {
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1091698%2fhow-to-block-ip-in-ssh%23new-answer', 'question_page');
                  }
                  );

                  Post as a guest















                  Required, but never shown

























                  5 Answers
                  5






                  active

                  oldest

                  votes








                  5 Answers
                  5






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  3














                  From http://www.linuxquestions.org/questions/linux-newbie-8/how-to-configure-ssh-to-allow-or-deny-specify-host-ip-address-610246/



                  Add sshd: 116.31.116.20 to /etc/hosts.deny






                  share|improve this answer



















                  • 3





                    This won't work for everyone. hosts.deny is part of an external library called TCP Wrappers. The SSH server will only honor this file if tcp wrapper support was included by whoever compiled it. Current versions of OpenSSH no longer contain TCP Wrapper support, although Linux distro maintainers may still be adding it back in to the OpenSSH packages that they distribute.

                    – Kenster
                    Jun 21 '16 at 14:01
















                  3














                  From http://www.linuxquestions.org/questions/linux-newbie-8/how-to-configure-ssh-to-allow-or-deny-specify-host-ip-address-610246/



                  Add sshd: 116.31.116.20 to /etc/hosts.deny






                  share|improve this answer



















                  • 3





                    This won't work for everyone. hosts.deny is part of an external library called TCP Wrappers. The SSH server will only honor this file if tcp wrapper support was included by whoever compiled it. Current versions of OpenSSH no longer contain TCP Wrapper support, although Linux distro maintainers may still be adding it back in to the OpenSSH packages that they distribute.

                    – Kenster
                    Jun 21 '16 at 14:01














                  3












                  3








                  3







                  From http://www.linuxquestions.org/questions/linux-newbie-8/how-to-configure-ssh-to-allow-or-deny-specify-host-ip-address-610246/



                  Add sshd: 116.31.116.20 to /etc/hosts.deny






                  share|improve this answer













                  From http://www.linuxquestions.org/questions/linux-newbie-8/how-to-configure-ssh-to-allow-or-deny-specify-host-ip-address-610246/



                  Add sshd: 116.31.116.20 to /etc/hosts.deny







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jun 21 '16 at 9:18









                  CameronCameron

                  1463




                  1463








                  • 3





                    This won't work for everyone. hosts.deny is part of an external library called TCP Wrappers. The SSH server will only honor this file if tcp wrapper support was included by whoever compiled it. Current versions of OpenSSH no longer contain TCP Wrapper support, although Linux distro maintainers may still be adding it back in to the OpenSSH packages that they distribute.

                    – Kenster
                    Jun 21 '16 at 14:01














                  • 3





                    This won't work for everyone. hosts.deny is part of an external library called TCP Wrappers. The SSH server will only honor this file if tcp wrapper support was included by whoever compiled it. Current versions of OpenSSH no longer contain TCP Wrapper support, although Linux distro maintainers may still be adding it back in to the OpenSSH packages that they distribute.

                    – Kenster
                    Jun 21 '16 at 14:01








                  3




                  3





                  This won't work for everyone. hosts.deny is part of an external library called TCP Wrappers. The SSH server will only honor this file if tcp wrapper support was included by whoever compiled it. Current versions of OpenSSH no longer contain TCP Wrapper support, although Linux distro maintainers may still be adding it back in to the OpenSSH packages that they distribute.

                  – Kenster
                  Jun 21 '16 at 14:01





                  This won't work for everyone. hosts.deny is part of an external library called TCP Wrappers. The SSH server will only honor this file if tcp wrapper support was included by whoever compiled it. Current versions of OpenSSH no longer contain TCP Wrapper support, although Linux distro maintainers may still be adding it back in to the OpenSSH packages that they distribute.

                  – Kenster
                  Jun 21 '16 at 14:01













                  1














                  You can also use iptables



                  Create a file with ip addresses you want to block as blocked.ips.txt



                  Now create and run a script containing the following:



                  blocked=$(egrep -v -E "^#|^$" ./blocked.ips.txt)
                  for ip in $blocked
                  do
                  iptables -I INPUT -s $ip -p tcp --dport ssh -j DROP
                  done


                  Running iptables -L should give output of dropped packets






                  share|improve this answer




























                    1














                    You can also use iptables



                    Create a file with ip addresses you want to block as blocked.ips.txt



                    Now create and run a script containing the following:



                    blocked=$(egrep -v -E "^#|^$" ./blocked.ips.txt)
                    for ip in $blocked
                    do
                    iptables -I INPUT -s $ip -p tcp --dport ssh -j DROP
                    done


                    Running iptables -L should give output of dropped packets






                    share|improve this answer


























                      1












                      1








                      1







                      You can also use iptables



                      Create a file with ip addresses you want to block as blocked.ips.txt



                      Now create and run a script containing the following:



                      blocked=$(egrep -v -E "^#|^$" ./blocked.ips.txt)
                      for ip in $blocked
                      do
                      iptables -I INPUT -s $ip -p tcp --dport ssh -j DROP
                      done


                      Running iptables -L should give output of dropped packets






                      share|improve this answer













                      You can also use iptables



                      Create a file with ip addresses you want to block as blocked.ips.txt



                      Now create and run a script containing the following:



                      blocked=$(egrep -v -E "^#|^$" ./blocked.ips.txt)
                      for ip in $blocked
                      do
                      iptables -I INPUT -s $ip -p tcp --dport ssh -j DROP
                      done


                      Running iptables -L should give output of dropped packets







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Feb 5 at 22:18









                      Erik HendriksErik Hendriks

                      111




                      111























                          1














                          iptables approach is the most direct way. Erik Handriks proposed it as a script.



                          Two but-s:
                          - the script would have to be started after every restart, (if tables not backed up internally with iptables).
                          - calling the script as is, twice or more will duplicate entries.



                          Try simply nailing the bad guy with:




                          iptables -I INPUT -s BAD_IP -p tcp --dport ssh -j DROP




                          In most distributions, iptables has own rules-save-restore mechanism.
                          See if '/var/lib/iptables/rules-save' is present. For your distro, it might sit somewhere else (gentoo here).



                          After you add every single BAD IP, try:




                          iptables-save > /var/lib/iptables/rules-save




                          After restart, check with 'iptables -L' what's out there...






                          share|improve this answer






























                            1














                            iptables approach is the most direct way. Erik Handriks proposed it as a script.



                            Two but-s:
                            - the script would have to be started after every restart, (if tables not backed up internally with iptables).
                            - calling the script as is, twice or more will duplicate entries.



                            Try simply nailing the bad guy with:




                            iptables -I INPUT -s BAD_IP -p tcp --dport ssh -j DROP




                            In most distributions, iptables has own rules-save-restore mechanism.
                            See if '/var/lib/iptables/rules-save' is present. For your distro, it might sit somewhere else (gentoo here).



                            After you add every single BAD IP, try:




                            iptables-save > /var/lib/iptables/rules-save




                            After restart, check with 'iptables -L' what's out there...






                            share|improve this answer




























                              1












                              1








                              1







                              iptables approach is the most direct way. Erik Handriks proposed it as a script.



                              Two but-s:
                              - the script would have to be started after every restart, (if tables not backed up internally with iptables).
                              - calling the script as is, twice or more will duplicate entries.



                              Try simply nailing the bad guy with:




                              iptables -I INPUT -s BAD_IP -p tcp --dport ssh -j DROP




                              In most distributions, iptables has own rules-save-restore mechanism.
                              See if '/var/lib/iptables/rules-save' is present. For your distro, it might sit somewhere else (gentoo here).



                              After you add every single BAD IP, try:




                              iptables-save > /var/lib/iptables/rules-save




                              After restart, check with 'iptables -L' what's out there...






                              share|improve this answer















                              iptables approach is the most direct way. Erik Handriks proposed it as a script.



                              Two but-s:
                              - the script would have to be started after every restart, (if tables not backed up internally with iptables).
                              - calling the script as is, twice or more will duplicate entries.



                              Try simply nailing the bad guy with:




                              iptables -I INPUT -s BAD_IP -p tcp --dport ssh -j DROP




                              In most distributions, iptables has own rules-save-restore mechanism.
                              See if '/var/lib/iptables/rules-save' is present. For your distro, it might sit somewhere else (gentoo here).



                              After you add every single BAD IP, try:




                              iptables-save > /var/lib/iptables/rules-save




                              After restart, check with 'iptables -L' what's out there...







                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Feb 6 at 14:08

























                              answered Feb 5 at 22:47









                              Pawel TateraPawel Tatera

                              292




                              292























                                  0














                                  You may want to use Fail2Ban to monitor logs and block IP automagically. It is possible to specify time period and fail count to block IP address, inform administrator, send complain to IP owner etc.



                                  See for more info: https://www.upcloud.com/support/installing-fail2ban-on-debian-8-0/






                                  share|improve this answer




























                                    0














                                    You may want to use Fail2Ban to monitor logs and block IP automagically. It is possible to specify time period and fail count to block IP address, inform administrator, send complain to IP owner etc.



                                    See for more info: https://www.upcloud.com/support/installing-fail2ban-on-debian-8-0/






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      You may want to use Fail2Ban to monitor logs and block IP automagically. It is possible to specify time period and fail count to block IP address, inform administrator, send complain to IP owner etc.



                                      See for more info: https://www.upcloud.com/support/installing-fail2ban-on-debian-8-0/






                                      share|improve this answer













                                      You may want to use Fail2Ban to monitor logs and block IP automagically. It is possible to specify time period and fail count to block IP address, inform administrator, send complain to IP owner etc.



                                      See for more info: https://www.upcloud.com/support/installing-fail2ban-on-debian-8-0/







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Jul 13 '17 at 16:50









                                      Milan KerslagerMilan Kerslager

                                      514




                                      514























                                          0














                                          On my server (Ubuntu 16.04) I use UFW (UncomplicatedFireWall), a simplification layer over iptables) which seems to be pretty much standard issue on Ubuntu servers (but should be available for other distros). For these cases I use:



                                          ufw insert 1 deny from nnn.nnn.nnn.nnn


                                          UFW also allows you to limit the number of connections



                                          ufw limit ssh/tcp


                                          Note that this also applies to successful connections, so if you use scripts that use multiple ssh command, you have to disable this limit for your IP by issuing:



                                          ufw insert 1 allow in from nnn.nnn.nnn.nnn





                                          share|improve this answer




























                                            0














                                            On my server (Ubuntu 16.04) I use UFW (UncomplicatedFireWall), a simplification layer over iptables) which seems to be pretty much standard issue on Ubuntu servers (but should be available for other distros). For these cases I use:



                                            ufw insert 1 deny from nnn.nnn.nnn.nnn


                                            UFW also allows you to limit the number of connections



                                            ufw limit ssh/tcp


                                            Note that this also applies to successful connections, so if you use scripts that use multiple ssh command, you have to disable this limit for your IP by issuing:



                                            ufw insert 1 allow in from nnn.nnn.nnn.nnn





                                            share|improve this answer


























                                              0












                                              0








                                              0







                                              On my server (Ubuntu 16.04) I use UFW (UncomplicatedFireWall), a simplification layer over iptables) which seems to be pretty much standard issue on Ubuntu servers (but should be available for other distros). For these cases I use:



                                              ufw insert 1 deny from nnn.nnn.nnn.nnn


                                              UFW also allows you to limit the number of connections



                                              ufw limit ssh/tcp


                                              Note that this also applies to successful connections, so if you use scripts that use multiple ssh command, you have to disable this limit for your IP by issuing:



                                              ufw insert 1 allow in from nnn.nnn.nnn.nnn





                                              share|improve this answer













                                              On my server (Ubuntu 16.04) I use UFW (UncomplicatedFireWall), a simplification layer over iptables) which seems to be pretty much standard issue on Ubuntu servers (but should be available for other distros). For these cases I use:



                                              ufw insert 1 deny from nnn.nnn.nnn.nnn


                                              UFW also allows you to limit the number of connections



                                              ufw limit ssh/tcp


                                              Note that this also applies to successful connections, so if you use scripts that use multiple ssh command, you have to disable this limit for your IP by issuing:



                                              ufw insert 1 allow in from nnn.nnn.nnn.nnn






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Jul 13 '17 at 19:32









                                              xenoidxenoid

                                              3,7533719




                                              3,7533719






























                                                  draft saved

                                                  draft discarded




















































                                                  Thanks for contributing an answer to Super User!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid



                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.


                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function () {
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1091698%2fhow-to-block-ip-in-ssh%23new-answer', 'question_page');
                                                  }
                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  Index of /

                                                  Tribalistas

                                                  Listed building