天天看点

Linux 系统安全加固

1,变更默认的ssh服务端口,禁止root用户远程连接

<code>[root@ljohn ~]</code><code># cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak</code>

<code>[root@ljohn ~]</code><code># vim /etc/ssh/sshd_config</code>

<code>Port 10022    </code><code>#ssh连接默认的端口</code>

<code>PermitRootLogin no   </code><code>#root用户黑客都知道,禁止它远程登录</code>

<code>[root@ljohn ~]</code><code># /etc/init.d/sshd reload    #从新加载配置</code>

<code>[root@ljohn ~]</code><code># netstat -lnt     #查看端口信息</code>

<code>[root@ljohn ~]</code><code># lsof -i tcp:10022</code>

或者直接修改用如下命令:

<code>cp</code> <code>/etc/ssh/sshd_config</code> <code>/etc/ssh/sshd_config</code><code>.bak</code>

<code>sed</code> <code>-i </code><code>"s/#PermitRootLogin yes/PermitRootLogin no/"</code> <code>/etc/ssh/sshd_config</code>

<code>sed</code> <code>-i </code><code>"s/#Port 22/Port 10022/"</code> <code>/etc/ssh/sshd_config</code>

注:这样做的目的是防止root用户暴力破解,22端口为ssh默认的端口,建议使用其他端口

/etc/init.d/sshd reload

service sshd restart &amp;&amp; history -c  #重启sshd服务

演示:

# ssh [email protected] 10022  这样就显示服务器拒绝了root用户登录。

2,添加普通用户并进行sudo授权管理

<code>[root@ljohn ~]</code><code># useradd cljj</code>

<code>[root@ljohn ~]</code><code># echo "123456" | passwd --stdin cljj &amp;&amp; history –c ##这条命令历史记录要清除</code>

<code>[root@ljohn ~]</code><code># visudo</code>

<code>在root    ALL=(ALL)    ALL此行下,添加如下内容</code>

<code>cljj    ALL=(ALL)    ALL</code>

<code>[cljj@ljohn home]$ </code><code>sudo</code> <code>cat</code> <code>-n </code><code>/etc/issue</code>

<code>[</code><code>sudo</code><code>] password </code><code>for</code> <code>cljj:   </code><code>#这里输入当前用户的密码,临时授予root用户权限</code>

<code>cljj is not </code><code>in</code> <code>the sudoers </code><code>file</code><code>.  This incident will be reported.</code>

<code>[</code><code>sudo</code><code>] password </code><code>for</code> <code>cljj: </code>

<code>     </code><code>1CentOS release 6.8 (Final)</code>

<code>     </code><code>2Kernel \r on an \m</code>

<code>     </code><code>3</code>

注:此方式可以限制用于的权限,防止篡改系统配置文件导致系统瘫痪

3,当普通用户登录时,密码输入错误三次,系统马上把该用户锁定10分钟,root用户锁定20分钟

编辑/etc/pam.d/sshd (ssh登录)

/etc/pam.d/login (终端)

<code>cp</code> <code>/etc/pam</code><code>.d</code><code>/sshd</code>  <code>/etc/pam</code><code>.d</code><code>/sshd</code><code>.bak   </code><code>#在文件末添加如下行:</code>

<code>auth    required    pam_tally2.so    deny=3    unlock_time=600 even_deny_root root_unlock_time=1200</code>

各参数解释:

even_deny_root    也限制root用户;  

deny           设置普通用户和root用户连续错误登陆的最大次数,超过最大次数,则锁定该用户 

unlock_time        设定普通用户锁定后,多少时间后解锁,单位是秒; 

root_unlock_time      设定root用户锁定后,多少时间后解锁,单位是秒; 

查看登陆次数:

查看某一用户错误登陆次数:

pam_tally –-user 用户

例如,查看cljj 用户的错误登陆次数:

pam_tally –-user cljj

清空某一用户错误登陆次数:

pam_tally –-user  用户 –-reset

例如,清空 cljj 用户的错误登陆次数,

pam_tally –-user cljj –-reset 

注:此方式也可以防御暴力破解用户账号

4,锁定关键文件系统

<code>[root@ljohn ~]</code><code># chattr +i /etc/passwd</code>

<code>[root@ljohn ~]</code><code># chattr +i /etc/inittab</code>

<code>[root@ljohn ~]</code><code># chattr +i /etc/group</code>

<code>[root@ljohn ~]</code><code># chattr +i /etc/shadow</code>

<code>[root@ljohn ~]</code><code># chattr +i /etc/gshadow</code>

使用chattr命令后,为了安全我们需要将其改名

[root@ljohn ~]# /bin/mv /usr/bin/chattr /usr/bin/任意名称

注:将有关用户账号密码的配置文件限制权限,也也可以有效的防止恶意篡改。

5,登出时间限制600登出时间,及HISTSIZE =10000

<code>cp</code>  <code>/etc/profile</code> <code>/etc/profile</code><code>.bak</code>

<code>echo</code> <code>export</code> <code>TMOUT=600 &gt;&gt; </code><code>/etc/profile</code>  <code>#增加10分钟超时退出</code>

<code>echo</code> <code>export</code> <code>HISTTIMEFORMAT=\'%F %T  `</code><code>whoami</code><code>` \'  &gt;&gt; </code><code>/etc/profile</code>    <code>#记录操作历史记录的时间</code>

<code>echo</code> <code>export</code> <code>HISTFILESIZE=10000 &gt;&gt; </code><code>/etc/profile</code>

<code>echo</code> <code>export</code> <code>HISTSIZE=10000 &gt;&gt; </code><code>/etc/profile</code>

<code>source</code> <code>/etc/profile</code>

注:history命令历史可以有效的将用户的行为记录下来,一方面可以方便查找,也可以查看用户在什么时间做了哪些操作。

本文转自 ljohnmail 51CTO博客,原文链接:http://blog.51cto.com/ljohn/1907249,如需转载请自行联系原作者

继续阅读