歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> 關於Unix >> LinuxShadow-Password-HOWTO-7.將ShadowSui

LinuxShadow-Password-HOWTO-7.將ShadowSui

日期:2017/3/6 15:44:48   编辑:關於Unix
這節描述你需要知道有些程式在安裝時就已經有ShadowSuite。大部分的資訊在操作手冊可以找到。 7.1新增、修改和刪除使用者 ShadowSuite新增下列指令用來新增、修改和刪除使用者。這也是可以安裝adduser程式。 useradd useradd使令可用在系統中新增使用者。你
這節描述你需要知道有些程式在安裝時就已經有 Shadow Suite。大部分的資訊在操作手冊可以找到。


7.1 新增、修改和刪除使用者
Shadow Suite 新增下列指令用來新增、修改和刪除使用者。 這也是可以安裝 adduser 程式。


useradd
useradd 使令可用在系統中新增使用者。 你也可以采用此指令來改變預設字串。

你應該做的第一件事是檢查預設值設定和針對你的系統進行改變:

useradd -D


--------------------------------------------------------------------------------

GROUP=1
HOME=/home
INACTIVE=0
EXPIRE=0
SHELL=
SKEL=/etc/skel


--------------------------------------------------------------------------------

預設值不全是你要的,所以如果你開始新增使用者,你必須詳閱每個使用者資訊。而且,我們可能和應該改變設定值。

在我的系統上:

我要預設群組是 100
我要密碼每到 60 天就到期
我不要鎖住帳號因為密碼會到期
我要預設 shell 是 /bin/bash
為了這些改變,我要使用:
useradd -D -g100 -e60 -f0 -s/bin/bash

現在執行 useradd -D 將得到:


--------------------------------------------------------------------------------

GROUP=100
HOME=/home
INACTIVE=0
EXPIRE=60
SHELL=/bin/bash
SKEL=/etc/skel


--------------------------------------------------------------------------------


盡管依照你需要修改,預設值將存在 /etc/default/useradd.

先在你可以使用 useradd 來新增系統使用者。舉例說明,新增一使用者 fred 使用預設值方式如下:

useradd -m -c "Fred Flintstone" fred

這將在 /etc/passwd 檔中的一行建立如下:

fred:*:505:100:Fred Flintstone:/home/fred:/bin/bash

且在 /etc/shadow 檔中的一行建立如下;
fred:!:0:0:60:0:0:0:0

fred的根目錄將被建立且 /etc/skel 的內容將被復制因為指令句中有 -m 設定。
因為我們並未詳述 UID,系統會直接尋找下一個可獲得的編號。

fred的帳號被建立羅,但是 fred 仍然不能簽入直到我們不再鎖住(unlock)這個帳號。透過更改密碼完成 unlock 帳號,方法如下:


passwd fred


--------------------------------------------------------------------------------

Changing password for fred□Enter the new password (minimum of 5 characters)
Please use a combination of upper and lower case letters and numbers.
New Password: *******
Re-enter new password: *******


--------------------------------------------------------------------------------
現在 /etc/shadow 檔將包含:
fred:J0C.WDR1amIt6:9559:0:60:0:0:0:0

且 fred 將可以簽入和使用該系統。 useradd 和其他附帶 Shadow Suite 比較好的地方是可以自動改變 /etc/passwd 和 /etc/shadow 。 所以如果你正在新增一個使用者,且另一個使用者正在更改密碼,這兩個操作都可以正確的執行。
你使用提供的指令比直接存取 /etc/passwd 和 /etc/shadow 檔還好。 如果你正編輯 /etc/shadow 檔,且有個使用者在你編輯時要改變他的密碼,然後你儲存編輯結果,這個使用者的密碼將會遺失掉。

這裡是使用 useradd 和 passwd 新增使用者的一些 interactive script :


--------------------------------------------------------------------------------

#!/bin/bash
#
# /sbin/newuser - A script to add users to the system using the Shadow
# Suite's useradd and passwd commands.
#
# Written my Mike Jackson as an example for the Linux
# Shadow Password Howto. Permission to use and modify is expressly granted.
#
# This could be modified to show the defaults and allow modification similar
# to the Slackware Adduser program. It could also be modified to disallow
# stupid entries. (i.e. better error checking).
#
##
# Defaults for the useradd command
##
GROUP=100 # Default Group
HOME=/home # Home directory location (/home/username)
SKEL=/etc/skel # Skeleton Directory
INACTIVE=0 # Days after password expires to disable aclearcase/" target="_blank" >ccount (0=never)
EXPIRE=60 # Days that a passwords lasts
SHELL=/bin/bash # Default Shell (full path)
##
# Defaults for the passwd command
##
PASSMIN=0 # Days between password changes
PASSWARN=14 # Days before password expires that a warning is given
##
# Ensure that root is running the script.
##
WHOAMI=`/usr/bin/whoami`
if [ $WHOAMI != "root" ]; then
echo "You must be root to add news users!"
exit 1
fi
##
# Ask for username and fullname.
##
echo ""
echo -n "Username: "
read USERNAME
echo -n "Full name: "
read FULLNAME
#
echo "Adding user: $USERNAME."
#
# Note that the "" around $FULLNAME is required because this field is
# almost always going to contain at least on space, and without the "'s
# the useradd command would think that you we moving on to the next
# parameter when it reached the SPACE character.
#
/usr/sbin/useradd -c"$FULLNAME" -d$HOME/$USERNAME -e$EXPIRE \
-f$INACTIVE -g$GROUP -m -k$SKEL -s$SHELL $USERNAME
##
# Set password defaults
##
/bin/passwd -n $PASSMIN -w $PASSWARN $USERNAME >/dev/null 2>&1
##
# Let the passwd command actually ask for password (twice)
##
/bin/passwd $USERNAME
##
# Show what was done.
##
echo ""
echo "Entry from /etc/passwd:"
echo -n " "
grep "$USERNAME:" /etc/passwd
echo "Entry from /etc/shadow:"
echo -n " "
grep "$USERNAME:" /etc/shadow
echo "Summary output of the passwd command:"
echo -n " "
passwd -S $USERNAME
echo ""


--------------------------------------------------------------------------------

新增使用者是用 script 比直接編輯 /etc/passwd / /etc/shadow 檔或使用像 Slackware 的 adduser 程式還要好。

需要更多 useradd 資訊請參照線上操作手冊。


usermod
usermod 程式是用在修改使用者資訊。它的參數使用和 useradd 程式類似。

如果你要更新 fred 的 shell,你要作下列步驟:

usermod -s /bin/tcsh fred

現在 fred 的 /etc/passwd 檔將變成:

fred:*:505:100:Fred Flintstone:/home/fred:/bin/tcsh

如果要使 fred 的帳號到期日為 09/15/97:
usermod -e 09/15/97 fred

現在 fred 在 /etc/shadow 的欄位變成:
fred:J0C.WDR1amIt6:9559:0:60:0:0:10119:0

需要更多 usermod 資訊請參照線上操作手冊。


userdel
userdel 用在刪除使用者,使用方法為:

userdel -r username

-r 參數可以將該使用者根目錄全部移除。位在期待目錄的檔案則需手動移除。
如果你只是要簡單的鎖住帳號而沒有要刪除它,建議你使用 passwd 指令。


7.2 passwd 指令和 passwd 老化
passwd 指令很明顯使用在改變密碼,除此之外,可由 root 使用在:

Lock 和 unlock 帳號 (-l and -u)
設定密碼合法的最大天數 (-x)
設定密碼改變間的最小天數 (-n)
設定密碼到期的警告天數 (-w)
設定在帳號未被鎖死密碼到期後的警告天數 (-i)
允許查詢帳號資訊 (-S)
舉例說明,如果要鎖死 fred 帳號:

passwd -S fred
fred P 03/04/96 0 60 0 0

這表示 fred 的密碼是有效的,它在 03/04/96 被修改且任何時間都可被修改, fred 將不會收到警告且帳號將不會因密碼到期而關閉。

這表示如果 fred 在密碼到期後簽入,它將被要求用一個新密碼簽入。


如果我們決定要警告 fred 在密碼過期前 14 天,且讓它的帳號在到期後14天警告,我們需要作下列步驟:


passwd -w14 -i14 fred

現在 fred 改變為:
fred P 03/04/96 0 60 14 14

需要更多 passwd 資訊請參照線上操作手冊。

7.3 login.defs 檔
/etc/login 檔是對 login 程式的 configuration file 且 對 Shadow Suite。

/etc/login 包含從預設值密碼改變的驅動設定。

/etc/login.defs 檔是一個很好的文件檔,然而仍有些事情要注意:


It contains flags that can be turned on or off that determine the amount of logging that takes place.
It contains pointers to other configuration files.
It contains defaults assignments for things like password aging.
跟去上述你可以發現這是一個重要檔,且你應該確認目前設定及你將對你系統的設定內容。


7.4 群組密碼
/etc/groups 檔包括允許是用者存取群組之密碼。 如果你定義 SHADOWGRP 在 /usr/src/shadow-YYMMDD/config.h 檔將開啟該功能。

如果你定義該常數且編譯它,你需建立一個 /etc/gshadow 檔來保存群組密碼和群組管理者資訊。


當你建立 /etc/shadow。你使用一個呼叫程式叫做 pwconv,該程式不會建立 /etc/gshadow 檔,但是這沒關系,只要你自行建立即可。


為了建立起始 /etc/gshadow 檔要執行下列步驟:

touch /etc/gshadow
chown root.root /etc/gshadow
chmod 700 /etc/gshadow

每次你建立一個新群組,它們會被加到 /etc/group 和 /etc/gshadow 檔。如果你透過新增或移除使用者來修改群組或改變群組密碼,/etc/gshadow 檔都將被改變。


groups, groupadd, groupmod, 和 groupdel 程式是用來供應 Shadow Suite 部分可以變更群組。

/etc/group 檔格式如下:

groupname:!:GID:member,member,...

其中:
groupname
The name of the group

!
The field that normally holds the password, but that is now relocated to the /etc/gshadow file.

GID
The numerical group ID number

member
List of group members

/etc/gshadow 檔格式如下:

groupname:password:admin,admin,...:member,member,...

其中:
groupname
The name of the group

password
The encoded group password.

admin
List of group administrators

member
List of group members

gpasswd 指令是用在新增或移除管理者和群組成員。 root 或其他在群組管理者人員可新增或移除群組成員。

群組密碼可以透過 passwd 指令改變,需透過 root 或在該群組管理者有權限的帳號方可修改。

Despite the fact that there is not currently a manual page for gpasswd, typing gpasswd without any parameters gives a listing of options. It's fairly easy to grasp how it all works once you understand the file formats and the concepts.



7.5 檢查程式一致性


pwck
pwck 程式提供在 /etc/passwd 和 /etc/shadow 檔的一致性檢查。它將檢查每個使用者名稱且依照下列步驟確認:


the correct number of fields
unique user name
valid user and group identifier
valid primary group
valid home directory
valid login shell
它也會警告沒有密碼的帳號。

在安裝 Shadow Suite 後執行 pwck 是一個很好的點子。它也可以每周或每月周期性的執行。 如果你使用 -r 參數,你可以用 cron 來執行且收到電子郵件報告


grpck
grpck 檢查 /etc/group 和 /etc/gshadow 檔一致性的程式。它作下列檢查:

the correct number of fields
unique group name
valid list of members and administrators
它也有 -r 參數自動產生報表。


7.6 Dial-up 密碼
Dial-up 密碼是另一個對系統防御的選項列,該系統允許撥接存取。 如果你有一個系統允許許多人區域網路連結,但是你想限制撥接的權限,那你需使用 dial-up 密碼。 為了要開啟 dial-up 密碼,你必須編輯 /etc/login.defs 檔且確定將 DIALUPS_CHECK_ENAB 設定為 yes.

有兩個檔案包括 dial-up 資訊, /etc/dialups 包括 ttys (one per line, with the leading "/dev/" removed)。如果 tty 有被列出, dial-up 表示已經被檢查。

第二個檔是 /etc/d_passwd 。 這個檔包括 shell 全部合法路徑名稱。

如果以個使用者簽入一條列在 /etc/dialups 的線(line),且他的 shell 被列在 /etc/d_passwd 檔,他將被允許存取透過提供正確的密碼。

另一個使用 dial-up 密碼的目的是設定指允許某些形式連結的線(可能是PPP 或 UUCP 連結)。如果一個使用者試著得到另一種形式連結 (i.e. a list of shells),他必須知道使用這條線的密碼。

在你可以在未來使用 dial-up 前,你密需建立一些檔案。

dpasswd 指令提供對在 /etc/d_passwd 檔的 shells 指派密碼。可以看操作手冊的到更多資訊。

Copyright © Linux教程網 All Rights Reserved