Dru Lavigne
01/03/2001
FreeBSD是個(gè)多用戶環(huán)境;一個(gè)系統(tǒng)管理員主要的工作之一就是建立用戶賬號(hào)并向用戶提供一個(gè)安全的工作環(huán)境。要有效地完成此工作需要在建立任何用戶之前作一些預(yù)期規(guī)劃。
即使如果你是個(gè)人FreeBSD系統(tǒng)的唯一用戶,仍需要至少建立一個(gè)用戶賬號(hào)來(lái)完成常規(guī)的工作。記住,只在工作需要超級(jí)用戶權(quán)限的時(shí)候才用超級(jí)用戶賬號(hào)。
在你的FreeBSD系統(tǒng)上象一位系統(tǒng)管理員那樣練習(xí)規(guī)劃是個(gè)好主意,因?yàn)槟憧梢栽鲩L(zhǎng)管理“實(shí)際”生產(chǎn)環(huán)境的基本技術(shù)。
當(dāng)你建立一個(gè)用戶時(shí),在后臺(tái)會(huì)發(fā)生很多事情:更新一些數(shù)據(jù)庫(kù),建立郵件目錄以及用戶的宿主目錄。當(dāng)建立用戶時(shí),你必須提供一定數(shù)量的信息,包括用戶名和口令。在這篇文章中,我打算集中于用戶策略以及建立用戶賬號(hào)上。
你建立的每個(gè)用戶賬號(hào)都必須有一個(gè)在FreeBSD系統(tǒng)上唯一的名字。在較小的環(huán)境這很容易做到,但如果你必須建立一打,成百甚至上千個(gè)用戶就稍微要困難一點(diǎn)了。為了有助于確保唯一性,你應(yīng)該有一套用戶命名策略。如果沒(méi)有適當(dāng)?shù)牟呗?,你需要建立一套命名方案,緊記一些用于用戶名的限制。首先,用戶名應(yīng)被限制在16個(gè)字符之內(nèi),而一些協(xié)議,如NIS,需要用戶名少于8個(gè)字符。其次,用戶名不能以連字符“-”起首。再次,你應(yīng)該避免使用大寫字母和句點(diǎn),因?yàn)檫@些會(huì)搞亂一些郵件程序。
在較小環(huán)境中的策略樣例可以用用戶的名和姓首字母來(lái)建立用戶名。你可能要對(duì)此稍加修改以避免沖突。例如,如果你需要為Mike Smith和Mike Spencer建立賬號(hào),可以建立“mikes”和“michaels”,或“mikesm”和“mikesp”。
其它的策略可以用用戶的姓和名首字母并作適當(dāng)?shù)男薷膩?lái)建立用戶名以避免沖突。如果你需要為Mark Smith和Michelle Smith建立賬號(hào),可以建立“smithma”和“smithmi”。
在稍大的環(huán)境中,你可能要用較多字符分別表示名和姓。例如,如果命名策略是名部分的首四個(gè)字符后跟姓部分的首四個(gè)字符,那么Mark Smith應(yīng)該是“marksmit”,而Michelle Smith應(yīng)該是“michsmit”。你仍需要有一份后備計(jì)劃用于那些姓和名都少于指定的字符數(shù)量。例如,如果我需要用上述策略為My Lee建立一個(gè)賬號(hào),我應(yīng)該建立“my_lee”。
除非你處于一個(gè)相當(dāng)小的環(huán)境之中,避免非說(shuō)明性的昵稱,如我用在自己系統(tǒng)上的“biko”和“genisis”是比較明智的。
總而言之,一個(gè)好的用戶命名策略表明了對(duì)用戶名的限制,而且是避免命令沖突的一個(gè)應(yīng)急方法。
一旦你決定了一套命令方案,你就可以用adduser工具建立用戶賬號(hào)了。這個(gè)工具有一個(gè)配置文件/etc/adduser.conf,它還會(huì)讀取一個(gè)訊息文件/etc/adduser.message。這些文件在你首次使用adduser工具前還沒(méi)有被創(chuàng)建。下面跟我一起建立一個(gè)用戶;我會(huì)使用v或verbose開關(guān),所以我們將看到所有可能的提示信息。你會(huì)看到adduser首先創(chuàng)建它的配置文件,然后在建立用戶時(shí)把它作為一個(gè)模板。
adduser -v
You are not root!
哦??雌饋?lái)這是個(gè)需要root權(quán)限的管理工作。讓我們?cè)僭囈淮危?
su
PassWord:
adduser -v
/etc/adduser.conf: No such file or directory
可以看到這是我首次在該系統(tǒng)上使用“adduser”工具;它還沒(méi)有一個(gè)配置文件。
Use option "-silent"
if you don't want to see all warnings and questions.
Check /etc/shells
Check /etc/master.passwd
Check /etc/group
Enter your default shell: bash csh date no sh tcsh [sh]: tcsh
adduser讀取一個(gè)含有系統(tǒng)上安裝的所有命令解釋器的路徑的文件/etc/shells;然后顯示用戶可以使用的解釋器。注意,向用戶提供的缺省解釋器是Bourne shell (sh),但我把它改為tcsh。
Your default shell is: tcsh -> /bin/tcsh
Enter your default HOME partition: [/home]:
Copy dotfiles from: /usr/share/skel no [/usr/share/skel]:
當(dāng)我們建完用戶并在接受了從/usr/share/skel目錄拷貝點(diǎn)文件看到的信息之后,可以去看一下那個(gè)目錄里的內(nèi)容。
Send message from file: /etc/adduser.message no
[/etc/adduser.message]:
Create "/etc/adduser.message"? (y/n) [y]:
Use passwords (y/n) [y]:
Write your configuration to /etc/adduser.conf? (y/n) [y]:
可以看到adduser在/etc目錄下建立了adduser.message和adduser.conf文件?,F(xiàn)在準(zhǔn)備建立我們的用戶:
Ok, let's go.
Don't worry about mistakes.
I will give you the chance later to correct any input.
Enter username [a-z0-9_-]: dlavigne
Enter full name []: Dru Lavigne
Enter shell bash csh date no sh tcsh [tcsh]:
Enter home directory (full path) [/home/dlavigne]:
Uid [1000]:
Enter login class: default []:
Login group dlavigne [dlavigne]:
Login group is "dlavigne".
Invite dlavigne into other groups: guest no
[no]:
Enter password []:
Enter password again []:
Name: dlavigne
Password: ****
Fullname: Dru Lavigne
Uid: 1000
Gid: 1000 (dlavigne)
Class:
Groups: dlavigne
HOME: /home/dlavigne
Shell: /bin/tcsh
OK? (y/n) [y]:
Added user "dlavigne"
Send message to "dlavigne" and: no root second_mail_address
[no]:
Dru Lavigne,
your account "dlavigne" was created.
Have fun!
See also chpass(1), finger(1), passwd(1)
Add anything to default message (y/n) [n]:
Send message (y/n) [y]:
Copy files from /usr/share/skel to /home/dlavigne
Add another user? (y/n) [y]: n
Goodbye!
現(xiàn)在我已經(jīng)順利地建立了一個(gè)用戶,我將離開超級(jí)用戶賬號(hào)。讓我們以新用戶身份登錄,看一下他們有些什么東東并描述一下這里的內(nèi)容:
login: dlavigne
Password:
pwd
/usr/home/dlavigne
ls -la
total 12
drwxr-xr-x 2 dlavigne dlavigne 512 Dec 30 11:21 ./
drwxr-xr-x 4 root wheel 512 Dec 30 10:44 ../
-rw-r--r-- 1 dlavigne dlavigne 628 Dec 30 10:44 .cshrc
-rw-r--r-- 1 dlavigne dlavigne 299 Dec 30 10:44 .login
-rw-r--r-- 1 dlavigne dlavigne 160 Dec 30 10:44 .login_conf
-rw------- 1 dlavigne dlavigne 371 Dec 30 10:44 .mail_aliases
-rw-r--r-- 1 dlavigne dlavigne 331 Dec 30 10:44 .mailrc
-rw-r--r-- 1 dlavigne dlavigne 722 Dec 30 10:44 .PRofile
-rw------- 1 dlavigne dlavigne 276 Dec 30 10:44 .rhosts
-rw-r--r-- 1 dlavigne dlavigne 852 Dec 30 10:44 .shrc
你可以看到為用戶建立了一個(gè)宿主目錄,里面包含很多以點(diǎn)起首的文件。記得我們使用adduser工具時(shí)提到的點(diǎn)文件目錄嗎?讓我們現(xiàn)在來(lái)看一下:
ls -l /usr/share/skel
total 10
drwxr-xr-x 2 root wheel 512 Dec 8 11:53 ./
drwxr-xr-x 26 root wheel 512 Nov 20 08:11 ../
-rw-r--r-- 1 root wheel 628 Nov 20 07:01 dot.cshrc
-rw-r--r-- 1 root wheel 299 Nov 20 07:01 dot.login
-rw-r--r-- 1 root wheel 160 Nov 20 07:01 dot.login_conf
-rw------- 1 root wheel 371 Nov 20 07:01 dot.mail_aliases
-rw-r--r-- 1 root wheel 331 Nov 20 07:01 dot.mailrc
-rw-r--r-- 1 root wheel 722 Nov 20 07:01 dot.profile
-rw------- 1 root wheel 276 Nov 20 07:01 dot.rhosts
-rw-r--r-- 1 root wheel 852 Nov 20 07:01 dot.shrc
可以看到在我們新用戶的宿主目錄建立的八個(gè)文件都是從這個(gè)目錄中拷貝的模板文件。另外也可以看到只有超級(jí)用戶才可以編輯skel目錄里的文件。例如,如果你希望所有用戶都得到定制過(guò)的命令解釋器提示符,那么超級(jí)用戶可以修改usr/share/skel/dot.cshrc文件,此文件會(huì)在建立賬號(hào)時(shí)拷貝到所有用戶的宿主目錄中。還有,超級(jí)用戶也可以在該目錄中放入他希望用戶得到的任何其它點(diǎn)文件;例如你可以為用戶建立定制好的.xinitrc文件。
現(xiàn)在讓我們看一下adduser工具建立的/etc/adduser.message文件:
more /etc/adduser.message
#
# Message file for adduser(8)
# comment: "#"
# default variables: $name, $fullname, $password
# other variables: see /etc/adduser.conf after
# line "## DO NOT DELETE THIS LINE!"
#
$fullname,
your account "$name" was created.
Have fun!
See also chpass(1), finger(1), passwd(1)
讓我們把它和新用戶收到的信息比較一下;我使用mail命令該用戶的郵件信息:
login: dlavigne
Password:
You have mail.
mail
Mail version 8.1 6/6/93. Type ? for help.
"/var/mail/dlavigne": 1 message 1 new
>N 1 genisis Sat Dec 30 12:24 20/540 "Welcome"
& 1
Message 1:
From genisis Sat Dec 30 12:24:19 2000
Date: Sat, 30 Dec 2000 12:24:19 -0500 (EST)
From: User Genisis
To: dlavigne
Subject: Welcome
Dru Lavigne,
your account "dlavigne" was created.
Have fun!
See also chpass(1), finger(1), passwd(1)
& ^D Saved 1 message in mbox
> ^D exit
你可以看到我在成為超級(jí)用戶以便使用adduser命令這前是以用戶“genisis”登錄的。我的新用戶收到的信息就是adduser.message中包含的內(nèi)容,而插入的$fullname和$name變量都以實(shí)際的值作了替換。
當(dāng)建立了賬號(hào)時(shí),我們會(huì)得到以下的選項(xiàng):
Add anything to default message (y/n) [n]:
如果我要建立其它賬號(hào)并打入y,我會(huì)得到以下提示:
Use "." or ^D alone on a file to finish your message
我打入的內(nèi)容都會(huì)被加入到缺省的信息中,然后發(fā)給該特定的用戶;但是,這不會(huì)覆蓋我原先創(chuàng)建的/etc/adduser.message文件。讓我們來(lái)試一下這樣做;我將添加一個(gè)稱為“test”的賬號(hào),顯示我們關(guān)心的輸出并“剪掉”其余的:
adduser
<剪掉>
Add anything to default message (y/n) [n]: y
Use "." or ^D alone on a line to finish your message.
Don't forget that every Friday is pizza day!
.
Send message (y/n) [y]: y
現(xiàn)在讓我們看一下用戶test的郵件信息是什么樣的:
login: test
Password:
You have mail.
mail
Mail version 8.1 6/6/93. Type ? for help.
"/var/mail/test": 1 message 1 new
>N 1 genisis Sat Dec 30 12:47 22/567 "Welcome"
& 1
Message 1:
From genisis Sat Dec 30 12:47:07 2000
Date: Sat, 30 Dec 2000 12:47:07 -0500 (EST)
From: User Genisis
To: test
Subject: Welcome
test,
your account "test" was created.
Have fun!
See also chpass(1), finger(1), passwd(1)
Don't forget that every Friday is pizza day!
再看一下新信息是否被加到了模板文件:
more /etc/adduser.message
#
# Message file for adduser(8)
# comment: "#"
# default variables: $name, $fullname, $password
# other variables: see /etc/adduser.conf after
# line "## DO NOT DELETE THIS LINE!"
#
$fullname,
your account "$name" was created.
Have fun!
See also chpass(1), finger(1), passwd(1)
看來(lái)只有該用戶收到了附加的信息,這正如我們所期望的。在離開adduser.message文件之前我還想做更多的例子。我想讓用戶在他們的歡迎郵件中收到下面這條附加信息:
If you have any problems, contact the administrator at admin@thiscompany.com
試一下你自己建立一個(gè)賬號(hào)并把這行加到他們的信息中;你會(huì)看到當(dāng)你以該用戶登錄時(shí),附加行沒(méi)有出現(xiàn)在郵件信息中。但是如果你把這行改成:
If you have any problems, contact the administrator at admin/@thiscompany.com
那么用戶就會(huì)收到附加信息了。注意,我們必須用/符號(hào)避開@符號(hào),以便正確解釋。
最后,讓我們看一下剛才建立的adduser.conf文件:
more /etc/adduser.conf
# /etc/adduser.conf - automatic generated by adduser(8)
#
# Note: adduser read *and* write this file
# You may change values, but don't add new things before the
# line "## DO NOT DELETE THIS LINE!"
# use password for new users
# defaultpasswd = yes | no
defaultpasswd = yes
# copy dotfiles from this dir ("/usr/share/skel" or "no")
dotdir = "/usr/share/skel"
# send this file to new user
# ("/etc/adduser.message" or "no")
send_message = "/etc/adduser.message"
# config file for adduser ("/etc/adduser.conf")
config = "/etc/adduser.conf"
# logfile ("/var/log/adduser" or "no")
logfile = "/var/log/adduser"
# default HOME directory ("/home")
home = "/home"
# List of directories where shells located
# path = ('/bin', '/usr/bin', '/usr/local/bin')
path = ('/bin', '/usr/bin', '/usr/local/bin')
# common shell list, first element has higher priority
# shellpref = ('bash', 'tcsh', 'ksh', 'csh', 'sh')
shellpref = ('csh', 'sh', 'bash', 'tcsh', 'ksh', 'no', 'date')
# defaultshell if not empty ("bash")
defaultshell = "tcsh"
# defaultgroup ('USER' for same as
# username or any other valid group)
defaultgroup = USER
# defaultclass if not empty
defaultclass = ""
# new users get this uid (1000)
uid_start = "1000"
## DO NOT DELETE THIS LINE!
## your own variables, see /etc/adduser.message
## end
你可以看到這是個(gè)簡(jiǎn)單的文件,它包含了對(duì)adduser工具提示問(wèn)題的回答。你看到的新信息只是該文件含有adduser記錄文件的存放位置。如果看一下這個(gè)記錄文件,會(huì)看到建立賬號(hào)時(shí)的記錄:
more /var/log/adduser
2000/12/30 12:24:18 dlavigne:*:1000:1000(dlavigne):Dru Lavigne
2000/12/30 12:47:06 test:*:1001:1001(test):test
Read more from FreeBSD Basics.