一般都不建議直接使用 root 來登入系統,而是建議先使用一般使用者登入,在有須要做系統管理時,再使用 su – 切換成 root 身份來執行。但有時在多人管理的系統中,不方便把 root 的密碼直接告訴所有的人,這時候可以改用 sudo 指令,因為使用者在切換成 root 身份時,只要輸入自己的密碼即可,且可以設定產生 log 檔,方便做更進一步稽核。
建立使用者
# useradd t850008
設定密碼
# passwd t850008
[@more@]
# visudo
加入下面二行,讓使用者可以取得所有 root 的權限
# add at the last line: user ‘t850008’ can use all root privilege
t850008 ALL=(ALL) ALL
切換到 t850008 使用者
# su – t850008
列出 /etc/shadow 內容
$ cat /etc/shadow
cat: /etc/shadow: 拒絕不符權限的操作
使用 sudo 指令並列出 /etc/shadow 第 2-5 行的內容
$ sudo cat /etc/shadow | sed -n ‘2,5p’
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
[sudo] password for t850008:
bin:*:16231:0:99999:7:::
daemon:*:16231:0:99999:7:::
adm:*:16231:0:99999:7:::
lp:*:16231:0:99999:7:::
做更進一步的限制
讓使用者無法關機或重新啟動系統
# visudo
t850008 ALL=(ALL) ALL,!SHUTDOWN
Cmnd_Alias SHUTDOWN = /sbin/halt, /sbin/shutdown, /sbin/poweroff, /sbin/reboot, /sbin/init
切換到 t850008 使用者
# su – t850008
無法重新開機
$ sudo /sbin/shutdown -r now
[sudo] password for t850008:
Sorry, user t850008 is not allowed to execute ‘/sbin/shutdown -r now’ as root on localhost.home.