天天看點

如何在Linux中限制SSH遠端會話及會話時間逾時

在Linux環境中如果你需要限制使用者輸入幾次錯誤密碼後鎖定他的賬号使其不能登陸,

如同控制密碼健壯性,我們需要調用PAM子產品來完成任務。

這個子產品叫:pam_tally2.so

本子產品可以了解為一個嘗試登陸的計數器,并可以以此計數為憑證,拒絕達到你設定的計數數值的使用者登陸。此計數器可以手動重置。

與cracklib.so一樣,我們需要同時為/etc/pam.d/system-auth以及/etc/pam.d/password-auth來插入該子產品,以達到同時為各種登入方案都起作用的效果

1、會話設定

(1)這一條登入多少次後就提示并自動結束會話:非常重要,在Linux中,如下:

# cat /etc/ssh/sshd_config  | grep MaxAuth

MaxAuthTries 1 //遠端使用者通過ssh連接配接登入2次失敗後自動結束會話

The server has disconnected with an error.  Server message reads:

A protocol error occurred. Too many authentication failures for root

(2)root隻能從tty1和vc登陸。建議僅允許root從一個tty或vc登陸,如果需要更多裝置登陸,使用su指令轉換為root。

openssh應該禁止使用協定1,禁止root直接登入

/etc/ssh/sshd_config

Protocol 2

MaxAuthTries 1

PermitRootLogin no  //不允許root使用者使用ssh登入

StrictModes yes

PermitEmptyPasswords no //不允許使用空密碼登入

PrintLastLog yes

root不登入,其他使用者登入時,添加密碼,和使用者時的具體操作方法:

useradd jackxiang

忘記了密碼,得有root去修改它即可:

[email protected]:/root# su root

密碼:

[email protected]:~# passwd jackxiang

更改使用者 jackxiang 的密碼 。

新的 密碼:

service sshd restart

禁止某些使用者使用ssh遠端登入:http://www.myhack58.com/Article/48/66/2011/30887.htm

vim /etc/pam.d/sshd

 在第一行加入 auth       required     pam_listfile.so item=user sense=deny file=/etc/sshdusers onerr=succeed,注意一定要在第一行,因為pam中執行順序是上面優先

vim /etc/sshdusers

 在檔案中加入root    wp ,root 和wp是兩個本地使用者

在另一個終端測試

[root@clone2 ~]# ssh clone1.rhel.com

[email protected]'s password: 

Permission denied, please try again.

Permission denied (publickey,gssapi-with-mic,password).

[wp@clone2 ~]$ ssh clone1.rhel.com

[email protected]'s password: 

兩個使用者都無法使用ssh了。呵呵,目的達到了

(3)[email protected]:~#  cat /etc/login.defs | grep PASS | grep -v ^#

PASS_MAX_DAYS   99999

PASS_MIN_DAYS   0

PASS_MIN_LEN    5

PASS_WARN_AGE   7

PASS_MAX_DAYS   90 //密碼最大使用日期90天

PASS_MIN_DAYS   0 //若設定為2,則設定密碼2天後才可以再次更改密碼,即密碼至少要保留的天數

PASS_MIN_LEN    8 //密碼最小長度8位

PASS_WARN_AGE   7 //密碼過期前7天警告

(4)

umask至少為027,最好是077

[root@station60 ~]# grep umask /etc/bashrc 

        umask 077

[root@station60 ~]# . /etc/bashrc

[root@station60 ~]# umask 

0077

[root@station60 ~]# touch 3.txt

[root@station60 ~]# ll 3.txt 

-rw------- 1 root root 0 Apr  8 00:11 3.txt

(5)

檢查系統是否最小化安裝,啟動的運作級别為3

檢視/etc/pam.d/su是否包含以下兩行

auth            sufficient      pam_rootok.so

# Uncomment the following line to implicitly trust users in the "wheel" group.

#auth           sufficient      pam_wheel.so trust use_uid

# Uncomment the following line to require a user to be in the "wheel" group.

auth            required        pam_wheel.so use_uid //上面的注釋已經說的很清楚了,沒有注釋下面這一行,那麼要su到别的使用者就必須在wheel組中

(6)關閉telnet服務,redhat預設是關閉telnet服務的

[root@station90 ssh]# netstat -tnlp | grep :23

[root@station90 ssh]# cd /etc/xinetd.d/

[root@station90 xinetd.d]# ls telnet*

ls: telnet*: 沒有那個檔案或目錄

如果有telnet服務,則把該目錄下的telnet檔案改為disable=yes

[root@station90 xinetd.d]# tail -n 2 /etc/xinetd.d/krb5-telnet | head -n 1 

        disable         = yes

[root@station90 xinetd.d]# service xinetd restart

停止 xinetd:                                              [确定]

啟動 xinetd:                                              [确定]

=============================================================

上面是我從Url:http://lhf0616.blog.51cto.com/2920914/538453 總結的,我貼上原文:

[root@station90 桌面]# awk  -F : '($2=="") {print $1}' /etc/shadow //檢查空密碼帳号

zhang3

[root@station90 桌面]# tail -n 1 /etc/shadow | head -n 1  //-F :是以冒号作為分隔符,($2==""表示第1個和第2個冒号之間是空的,即空密碼帳号,{print $1}列印出使用者名

zhang3::15071:0:99999:7:::

檢查帳号

[root@station90 桌面]# pwck

使用者 adm:目錄 /var/adm 不存在

使用者 news:目錄 /etc/news 不存在

使用者 uucp:目錄 /var/spool/uucp 不存在

使用者 gopher:目錄 /var/gopher 不存在

使用者 pcap:目錄 /var/arpwatch 不存在

使用者 avahi-autoipd:目錄 /var/lib/avahi-autoipd 不存在

使用者 oprofile:目錄 /home/oprofile 不存在

pwck:無改變

密碼複雜度及登入失敗政策

應啟用登入失敗處理功能,可采取結束會話,限制非法登入次數和自動退出措施,密碼應有複雜度要求并定期更換

要求強制記住3個密碼曆史

密碼至少包含1個數字,字母和其他特殊字元(如:#,@,!,$等);

5次遠端登入失敗自動結束會話

[root@station90 桌面]# cat /etc/login.defs | grep PASS | grep -v ^#

[root@station90 pam.d]# cat /etc/pam.d/system-auth | tail -n 2 && grep ^#password /etc/pam.d/system-auth

password    required      pam_cracklib.so difok=3 minlen=8 dcredit=-1,lcredit=-1 ocredit=-1 maxrepeat=3

password    required      pam_unix.so use_authtok nullok md5

#password    requisite     pam_cracklib.so try_first_pass retry=3 //注釋這一行後,無法修改密碼

[root@station90 pam.d]# passwd

Changing password for user root.

passwd: Authentication information cannot be recovered

修改登入失敗政策

[root@station90 ssh]# cat /etc/ssh/sshd_config  | grep MaxAuth

[root@station90 ssh]# ssh 192.168.0.90

[email protected]'s password: 

Received disconnect from 192.168.0.90: 2: Too many authentication failures for root

關閉telnet服務,redhat預設是關閉telnet服務的

[root@station90 xinetd.d]# chkconfig xinetd on

[root@station60 init.d]# pwd

/etc/rc.d/init.d

[root@station60 init.d]# chmod -R 750 ./ 也可以直接chmod -R /etc/init.d/*

[root@station60 init.d]# ll | head -n 2

total 644

-rwxr-x--- 1 root root  1566 Jun  8  2009 acpid

[root@station60 pam.d]# gpasswd  -a oracle wheel

Adding user oracle to group wheel

[root@station60 pam.d]# id oracle

uid=500(oracle) gid=500(oracle) groups=500(oracle),0(root),10(wheel) context=system_u:system_r:unconfined_t

[root@station60 pam.d]# id zhang3

uid=501(zhang3) gid=501(zhang3) groups=501(zhang3) context=system_u:system_r:unconfined_t

[root@station60 pam.d]# su - zhang3

[zhang3@station60 ~]$ su - root //以下密碼輸入都是正确的

Password: 

su: incorrect password

[zhang3@station60 ~]$ su - oracle

[zhang3@station60 ~]$ 

[oracle@station60 ~]$  su - oracle

[oracle@station60 ~]$ su - root

[root@station60 ~]# 

2 操作指南  

2.1.本地登入使用者參考配置操作

#cd /etc/profile.d

執行

#vi autologout.sh

加入如下内容:

TMOUT=600

readonly TMOUT

export TMOUT

儲存退出,系統将在使用者閑置10分鐘後自動登出。

2.2.遠端登入使用者參考配置操作

#vi /etc/ssh/sshd_config

将以下内容設定為:

ClientAliveInterval 600

ClientAliveCountMax 0

以上表示10分鐘閑置後,自動登出并結束會話。

檢測方法    1、判定條件

檢視帳号逾時是否自動登出;

2、檢測操作

cat  /etc/ssh/sshd_config

檢查其中兩個參數設定:

(2)執行:awk -F: '($3 == 0) { print $1 }' /etc/passwd

傳回值包括“root”以外的條目,說明有其他超級使用者,低于安全要求。

執行:awk -F: '($3 == 0) { print $1 }' /etc/passwd

傳回值包括“root”以外的條目,說明有其他超級使用者;

/etc/securetty 檔案設定root登陸的tty和vc(虛拟控制台)裝置。/etc/securetty 檔案被login程式讀 (通常 /bin/login)。它的格式是允許的tty和vc清單,注釋掉或不出現的裝置,不允許root登陸。

本文轉自xiaocao1314051CTO部落格,原文連結:http://blog.51cto.com/xiaocao13140/1971149 ,如需轉載請自行聯系原作者