启动或禁止SSH用户登录

http://blog.csdn.net/linghe301/article/details/8211305

一般情况下,在使用Linux操作系统都不会去机房来操作机器,都是使用一些第三方的工具来操作。

ssh

比如使用SSH Secure File Transfer Client工具来传输文件,利用Putty来操作,利用Xmanger综合操作等,那么最常见的连接类型包括telnet、SSH、Raw等

下面就针对SSH方面讨论一下,如果有人特别关注Linux环境的安全性,第一就从login方面来进行讨论,也就是文章标题

1:Linux启动或禁止SSH root用户的登录

2:Linux限制SSH用户

其实这些东西就是修改一个系统的配置文件

  1. [root@rhsde ~]# vi /etc/ssh/sshd_config

我们可以查看

  1. #PermitRootLogin yes

把前面的#号去掉,yes修改为no即可

yes 就是可以使用SSH方式的root登录

no就是禁止使用SSH方式的root登录

  1. login as: root
  2. root@192.168.220.165’s password:
  3. Access denied
  4. root@192.168.220.165’s password:
 

另外如果需要限制SSH方式的用户登录 修改如下参数

  1. AllowUsers arcsde

arcsde是我操作系统的用户,如果没有用户可以手动添加AllowUsers

这样的话,只能arcsde登录了

其他用户登录不了了(oracle)

  1. login as: oracle
  2. oracle@192.168.220.165’s password:
  3. Access denied
  4. oracle@192.168.220.165’s password:

以上修改完配置文件,必须重新启动SSH服务才能生效

  1. [root@rhsde ~]# /etc/init.d/sshd restart
  2. Stopping sshd:                                             [  OK  ]
  3. Starting sshd:                                             [  OK  ]
 

可能会有人会问到如果我设置了,每次都是Access denied,有没有一些可以进行信息的提示,这肯定可以啊

我们可以修改如下文件

  1. [root@rhsde ~]# vi /etc/issue.net

然后添加如下信息

  1. ###############################################################
  2. #                    Welcome to redhatserver                                    #
  3. #          All connections are monitored and recorded                            #
  4. #  Disconnect IMMEDIATELY if you are not an authorized user!                    #
  5. #                       Please tel 400-819-2881
  6. ###############################################################

我们仍然需要修改sshd_config里面的参数

  1. Banner /etc/issue.net

后面对应的就是相关文件的路径,重启服务即可

然后我们测试一下

  1. login as: root
  2. Red Hat Enterprise Linux Server release 5.5 (Tikanga)
  3. Kernel \r on an \m
  4. ###############################################################
  5. #                    Welcome to redhatserver                                    #
  6. #          All connections are monitored and recorded
  7.  #
  8. #  Disconnect IMMEDIATELY if you are not an authorized user!                    #
  9. #Please tel 400-819-2881
  10. ###############################################################
  11. root@192.168.220.165’s password:

当我们输入root用户,系统就自动提示了。另外也可以在输入密码的时候提示,如果是这样的话,我们修改如下文件即可

  1. vi /etc/motd
vi /etc/motd

 

启动或禁止用户IP登录

除了可以禁止某个用户登录,我们还可以针对固定的IP进行禁止登录,这里面其实就是修改了配置文件

查看 /etc/hosts.allow配置文件,设置允许登录的IP

  1. [root@rhsde ~]# more /etc/hosts.allow
  2. #
  3. # hosts.allow   This file describes the names of the hosts which are
  4. #               allowed to use the local INET services, as decided
  5. #               by the ‘/usr/sbin/tcpd’ server.
  6. #
  7. sshd:192.168.220.164:allow

 

查看/etc/hosts.deny文件,设置sshd:ALL

  1. [root@rhsde ~]# more /etc/hosts.deny
  2. #
  3. # hosts.deny    This file describes the names of the hosts which are
  4. #               *not* allowed to use the local INET services, as decided
  5. #               by the ‘/usr/sbin/tcpd’ server.
  6. #
  7. # The portmap line is redundant, but it is left to remind you that
  8. # the new secure portmap uses hosts.deny and hosts.allow.  In particular
  9. # you should know that NFS uses portmap!
  10. sshd:ALL

也就是说,我们禁止所有IP,但是允许相关IP登录。

另外,如果对sshd_config文件中的配置参数感兴趣可以参考:http://doc.licess.org/openssh/sshd_config.html

 

SSH访问控制,多次失败登录即封掉IP,防止暴力破解

http://www.linuxidc.com/Linux/2014-09/107370.htm

一、系统:CentOS 6.3 64位

二、方法:读取/var/log/secure,查找关键字 Failed,例如(注:文中的IP地址特意做了删减):

Sep 17 09:08:09 localhost sshd[29087]: Failed password for root from 13.7.3.6 port 44367 ssh2
Sep 17 09:08:20 localhost sshd[29087]: Failed password for root from 13.7.3.6 port 44367 ssh2
Sep 17 09:10:02 localhost sshd[29223]: Failed password for root from 13.7.3.6 port 56482 ssh2
Sep 17 09:10:14 localhost sshd[29223]: Failed password for root from 13.7.3.6 port 56482 ssh2

从这些行中提取IP地址,如果次数达到5次则将该IP写到 /etc/hosts.deny中。

三、步骤:

1、先把始终允许的IP填入 /etc/hosts.allow ,这很重要!比如:
sshd:19.16.18.1:allow
sshd:19.16.18.2:allow

2、脚本 /root/secure_ssh.sh

#! /bin/bash
cat /var/log/secure|awk ‘/Failed/{print $(NF-3)}’|sort|uniq -c|awk ‘{print $2″=”$1;}’ > /root/black.txt
DEFINE=”5″
for i in `cat  /root/black.txt`
do
IP=`echo $i |awk -F= ‘{print $1}’`
NUM=`echo $i|awk -F= ‘{print $2}’`
if [ $NUM -gt $DEFINE ];then
grep $IP /etc/hosts.deny > /dev/null
if [ $? -gt 0 ];then
echo “sshd:$IP:deny” >> /etc/hosts.deny
fi
fi
done

3、将secure_ssh.sh脚本放入cron计划任务,每1分钟执行一次。
# crontab -e
*/1 * * * *  sh /root/secure_ssh.sh

四、测试:

1、开两个终端窗口,一个ssh连上服务器,另一个用错误的密码连接服务器几次。

很快,服务器上黑名单文件里已经有记录了:
[root@ ~]# $ cat /root/black.txt
13.26.21.27=3

再看看服务器上的hosts.deny
[root@ ~]# cat /etc/hosts.deny
sshd:13.7.3.6:deny
sshd:92.4.0.4:deny
sshd:94.10.4.2:deny
sshd:94.4.1.6:deny
sshd:11.64.11.5:deny

2、从另一个终端窗口继续“暴力”连接服务器。

看看服务器上的黑名单文件:
[root@ ~]# cat black.txt
13.26.21.27=6

再看看服务器上的hosts.deny
[root@ ~]# cat /etc/hosts.deny
sshd:13.7.3.6:deny
sshd:92.4.0.4:deny
sshd:94.10.4.2:deny
sshd:94.4.1.6:deny
sshd:11.64.11.5:deny
sshd:13.26.21.27:deny

IP 已经被加入到服务器的hosts.deny,再用正确的密码连接服务器,被拒绝:
$ ssh root@myserver.mydomain.com -p 2333
ssh_exchange_identification: Connection closed by remote host

分类: Linux基本操作

0 条评论

发表评论