一 設置sudo爲不須要密碼php
有時候咱們只須要執行一條root權限的命令也要su到root,是否是有些不方便?這時能夠用sudo代替。默認新建的用戶不在sudo組,須要編輯/etc/sudoers文件將用戶加入,該文件只能使用visudo命令,安全
1) 首先須要切換到root, su - (注意有- ,這和su是不一樣的,在用命令"su"的時候只是切換到root,但沒有把root的環境變量傳過去,仍是當前用乎的環境變量,用"su -"命令將環境變量也一塊兒帶過去,就象和root登陸同樣)app
2) 而後 visudo 或者 vi /etc/sudoers, visudo 這個和vi的用法同樣,因爲可能會有人不太熟悉vi,因此簡要說一下步驟ide
移動光標,到一行root ALL=(ALL) ALL的下一行,按a,進入append模式,輸入
your_user_name ALL=(ALL) ALLui
而後按Esc,再輸入:w保存文件,再:q退出this
這樣就把本身加入了sudo組,可使用sudo命令了。spa
3) 默認5分鐘後剛纔輸入的sodo密碼過時,下次sudo須要從新輸入密碼,若是以爲在sudo的時候輸入密碼麻煩,把剛纔的輸入換成以下內容便可:
your_user_name ALL=(ALL) NOPASSWD: ALLblog
至於安全問題,對於通常我的用戶,我以爲這樣也能夠的。ci
4)若是你想設置只有某些命令能夠sudo的話,your_user_name ALL= (root) NOPASSWD: /sbin/mount, (root) NOPASSWD: /bin/umount, (root) NOPASSWD: /mnt/mount, (root) NOPASSWD: /bin/rm, (root) NOPASSWD: /usr/bin/make, (root) NOPASSWD: /bin/ln, (root) NOPASSWD: /bin/sh, (root) NOPASSWD: /bin/mv, (root) NOPASSWD: /bin/chown, (root) NOPASSWD: /bin/chgrp, (root) NOPASSWD: /bin/cp, (root) NOPASSWD: /bin/chmodrem
注意: 有的時候你的將用戶設了nopasswd,可是不起做用,緣由是被後面的group的設置覆蓋了,須要把group的設置也改成nopasswd。
joe ALL=(ALL) NOPASSWD: ALL
%admin ALL=(ALL) NOPASSWD: ALL
參考:
http://blog.163.com/love-love-l/blog/static/21078304201071232234518/
二 設置su爲不須要密碼
若是須要對某用戶su命令也不須要輸入密碼,則須要修改下列的:
1)切換到root權限;
2)建立group爲wheel,命令爲groupadd wheel;
3)將用戶加入wheel group中,命令爲usermod -G wheel joe;
4)修改su的配置文件/etc/pam.d/su,增長下列項:
auth required pam_wheel.so group=wheel
# Uncomment this if you want wheel members to be able to
# su without a password.
auth sufficient pam_wheel.so trust use_uid
至此你可使用例如以下的命令且不須要輸入密碼:su joe -c command。
參考:
http://cosminswiki.com/index.php/How_to_let_users_su_without_password
http://ag-up.com/?p=457
二 sudoers 編輯出錯後的補救方法
/etc/sudoers: syntax error near line
sudo: parse error in /etc/sudoers near line 25
sudo: no valid sudoers sources found, quitting
終極解決方案:
1. shift進入recovery模式
2. 選中root帳號
3. chmod 666 /dev/null
mount -o remount rw /
4. nano /etc/sudoers
恢復本文件內容並存盤。
# /etc/sudoers
#
# This file MUST be edited with the 'visudo' command as root.
#
# See the man page for details on how to write a sudoers file.
#
Defaults env_reset
# Host alias specification
# User alias specification
# Cmnd alias specification
# User privilege specification
root ALL=(ALL) ALL
# Allow members of group sudo to execute any command after they have
# provided their password
# (Note that later entries override this, so you might need to move
# it further down)
%sudo ALL=(ALL) ALL
#
#includedir /etc/sudoers.d
# Members of the admin group may gain root privileges
%admin ALL=(ALL) ALL