一 设置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