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 && 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 && 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 >> </code><code>/etc/profile</code> <code>#增加10分鐘逾時退出</code>
<code>echo</code> <code>export</code> <code>HISTTIMEFORMAT=\'%F %T `</code><code>whoami</code><code>` \' >> </code><code>/etc/profile</code> <code>#記錄操作曆史記錄的時間</code>
<code>echo</code> <code>export</code> <code>HISTFILESIZE=10000 >> </code><code>/etc/profile</code>
<code>echo</code> <code>export</code> <code>HISTSIZE=10000 >> </code><code>/etc/profile</code>
<code>source</code> <code>/etc/profile</code>
注:history指令曆史可以有效的将使用者的行為記錄下來,一方面可以友善查找,也可以檢視使用者在什麼時間做了哪些操作。
本文轉自 ljohnmail 51CTO部落格,原文連結:http://blog.51cto.com/ljohn/1907249,如需轉載請自行聯系原作者