如何用 PAM 模塊加強(qiáng) Linux 密碼復(fù)雜度?一文搞定!
在現(xiàn)代企業(yè)的生產(chǎn)環(huán)境中,信息安全是重中之重。密碼作為身份驗(yàn)證的重要手段,其安全性直接關(guān)系到系統(tǒng)的整體防護(hù)能力。一個(gè)簡(jiǎn)單的密碼可能是黑客攻破系統(tǒng)的敲門(mén)磚,帶來(lái)無(wú)法估量的風(fēng)險(xiǎn)。因此,許多公司在操作系統(tǒng)中配置了強(qiáng)密碼策略,以確保每一個(gè)用戶密碼都能抵擋住外部的攻擊。
那么,如何有效地管理這些密碼復(fù)雜度要求呢?

在不同的Linux發(fā)行版中,配置密碼復(fù)雜度的方式有所不同。通常,密碼復(fù)雜度的管理是通過(guò)PAM來(lái)實(shí)現(xiàn)的。
Red Hat/CentOS/Fedora系列
在Red Hat Enterprise Linux``CentOS和Fedora等發(fā)行版中,密碼復(fù)雜度的配置主要通過(guò)PAM(Pluggable Authentication Modules,可插拔認(rèn)證模塊)中的pam_pwquality.so模塊來(lái)實(shí)現(xiàn)。具體操作方法如下:
簡(jiǎn)單來(lái)說(shuō),你需要對(duì)系統(tǒng)認(rèn)證文件進(jìn)行編輯:
打開(kāi)終端,輸入以下命令進(jìn)入配置文件編輯界面:
vim /etc/pam.d/system-auth執(zhí)行上述文件后,該文件內(nèi)容如下圖所示:

在該文件中,找到并修改與密碼質(zhì)量相關(guān)的配置項(xiàng),即可實(shí)現(xiàn)密碼復(fù)雜度的設(shè)置。
Debian/Ubuntu系列
對(duì)于Debian和Ubuntu等發(fā)行版,密碼復(fù)雜度的配置同樣使用pam_pwquality.so模塊,但配置文件位置有所不同。你需要編輯的是:
sudo nano /etc/pam.d/common-password在這里,你可以根據(jù)需要調(diào)整密碼策略參數(shù),確保密碼符合安全標(biāo)準(zhǔn)。
注意:在使用該功能之前先要確定系統(tǒng)是否安裝了該模塊。
配置項(xiàng)說(shuō)明
password requisite pam_pwquality.so minlen=8 ucredit=-2 lcredit=-1 dcredit=-4 ocredit=-1password 表示這個(gè)PAM模塊是用于密碼驗(yàn)證的。
requisite 是PAM模塊的控制標(biāo)志之一,它表示如果這個(gè)模塊驗(yàn)證失敗,接下來(lái)的驗(yàn)證流程會(huì)被中斷,用戶將無(wú)法繼續(xù)登錄或執(zhí)行相關(guān)操作。password requisite pam_pwquality.so后面添加或修改相關(guān)配置就可以了。具體的參數(shù)設(shè)置,可以參考以下信息:

在大型企業(yè)或組織中,單個(gè)用戶密碼的管理可能沒(méi)有太大挑戰(zhàn),但當(dāng)面臨大量用戶時(shí),如何批量配置密碼復(fù)雜度并確保其符合安全要求就成了一個(gè)難題。那么,如何有效地進(jìn)行批量管理?
通過(guò)腳本批量修改PAM配置
為了實(shí)現(xiàn)密碼策略的高效管理,可以開(kāi)發(fā)一個(gè)Shell腳本,利用sed或awk等工具對(duì)多個(gè)配置文件進(jìn)行自動(dòng)化批量更新。這樣不僅能夠顯著提升工作效率,還能確保密碼策略在各個(gè)系統(tǒng)組件中保持一致性和標(biāo)準(zhǔn)化管理。
以下是一個(gè)用于配置密碼復(fù)雜度的Shell腳本示例:
update_pam_pwquality() {
    # 配置項(xiàng)
    minlen="minlen=8"
    ucredit="ucredit=-2"
    lcredit="lcredit=-1"
    dcredit="dcredit=-4"
    ocredit="ocredit=-1"
    # 判斷 Linux 發(fā)行版并設(shè)置 PAM 配置文件路徑
    if [ -f /etc/os-release ]; then
        # 獲取發(fā)行版名稱
        source /etc/os-release
        DISTRO_NAME=$ID
        # 根據(jù)發(fā)行版選擇正確的 PAM 配置文件
        case"$DISTRO_NAME"in
            "centos"|"rhel"|"fedora")
                PAM_FILE="/etc/pam.d/system-auth"
                PACKAGE="libpwquality"
                PKG_MGR="yum"
                ;;
            "ubuntu"|"debian")
                PAM_FILE="/etc/pam.d/common-password"
                PACKAGE="libpam-pwquality"
                PKG_MGR="apt-get"
                ;;
            "arch"|"manjaro")
                PAM_FILE="/etc/pam.d/system-auth"
                PACKAGE="libpwquality"
                PKG_MGR="pacman"
                ;;
            "opensuse")
                PAM_FILE="/etc/pam.d/common-password"
                PACKAGE="libpwquality"
                PKG_MGR="zypper"
                ;;
            *)
                echo"Unsupported distribution: $DISTRO_NAME"
                exit 1
                ;;
        esac
    else
        echo"無(wú)法識(shí)別發(fā)行版,無(wú)法確定 PAM 配置文件路徑"
        exit 1
    fi
    # 檢查并安裝 pam_pwquality 模塊
    if ! rpm -q "$PACKAGE" >/dev/null 2>&1 && ! dpkg -l "$PACKAGE" >/dev/null 2>&1; then
        echo"正在安裝 $PACKAGE..."
        if ! sudo $PKG_MGR install -y "$PACKAGE"; then
            echo"安裝 $PACKAGE 失敗,請(qǐng)手動(dòng)安裝后重試"
            exit 1
        fi
    fi
    # 備份原始 PAM 配置文件
    BACKUP_FILE="$PAM_FILE.$(date +%Y%m%d%H%M%S)"
    cp -p "$PAM_FILE""$BACKUP_FILE"
    echo"配置文件已備份到 $BACKUP_FILE"
    # 定位到包含 "password.*pam_pwquality.so" 的行
    line_number=$(grep -n "^password.*pam_pwquality.so""$PAM_FILE" | cut -d: -f1)
    if [[ -n "$line_number" ]]; then
        # 獲取當(dāng)前行內(nèi)容
        current_line=$(sed -n "${line_number}p""$PAM_FILE")
        # 檢查并更新 minlen
        if [[ "$current_line" =~ minlen=([0-9]+) ]]; then
            current_minlen="${BASH_REMATCH[1]}"
            if [[ "$current_minlen" != "8" ]]; then
                # 更新 minlen 為 8
                current_line=$(echo"$current_line" | sed "s/minlen=$current_minlen/minlen=8/")
                echo"更新 minlen minlen to 8."
            fi
        else
            # 添加 minlen 配置
            current_line="$current_line $minlen"
            echo"添加 minlen=8."
        fi
        # 檢查并更新 ucredit
        if [[ "$current_line" =~ ucredit=(-?[0-9]+) ]]; then
            current_ucredit="${BASH_REMATCH[1]}"
            if [[ "$current_ucredit" != "-2" ]]; then
                # 更新 ucredit 為 -2
                current_line=$(echo"$current_line" | sed "s/ucredit=$current_ucredit/ucredit=-2/")
                echo"更新 ucredit to -2."
            fi
        else
            # 添加 ucredit 配置
            current_line="$current_line $ucredit"
            echo"添加 ucredit=-2."
        fi
        # 檢查并更新 lcredit
        if [[ "$current_line" =~ lcredit=(-?[0-9]+) ]]; then
            current_lcredit="${BASH_REMATCH[1]}"
            if [[ "$current_lcredit" != "-1" ]]; then
                # 更新 lcredit 為 -1
                current_line=$(echo"$current_line" | sed "s/lcredit=$current_lcredit/lcredit=-1/")
                echo"更新 lcredit to -1."
            fi
        else
            # 添加 lcredit 配置
            current_line="$current_line $lcredit"
            echo"添加 lcredit=-1."
        fi
        # 檢查并更新 dcredit
        if [[ "$current_line" =~ dcredit=(-?[0-9]+) ]]; then
            current_dcredit="${BASH_REMATCH[1]}"
            if [[ "$current_dcredit" != "-4" ]]; then
                # 更新 dcredit 為 -4
                current_line=$(echo"$current_line" | sed "s/dcredit=$current_dcredit/dcredit=-4/")
                echo"更新 dcredit to -4."
            fi
        else
            # 添加 dcredit 配置
            current_line="$current_line $dcredit"
            echo"添加 dcredit=-4."
        fi
        # 檢查并更新 ocredit
        if [[ "$current_line" =~ ocredit=(-?[0-9]+) ]]; then
            current_ocredit="${BASH_REMATCH[1]}"
            if [[ "$current_ocredit" != "-1" ]]; then
                # 更新 ocredit 為 -1
                current_line=$(echo"$current_line" | sed "s/ocredit=$current_ocredit/ocredit=-1/")
                echo"更新 ocredit to -1."
            fi
        else
            # 添加 ocredit 配置
            current_line="$current_line $ocredit"
            echo"添加 ocredit=-1."
        fi
        # 用修改后的行替換原文件中的行
        sed -i "${line_number}s|.*|$current_line|""$PAM_FILE"
        echo"Line updated in $PAM_FILE."
    else
        echo"password requisite pam_pwquality.so minlen=8 ucredit=-2 lcredit=-1 dcredit=-4 ocredit=-1" >> "$PAM_FILE"
        echo"密碼復(fù)雜度-未符合要求-已配置"
    fi
}














 
 
 








 
 
 
 