如何实现使得一个普通用户以root身份运行命令
2012-03-01 16:03
761 查看
解决方法:
sudo命令允许用户在RHEL上以另外一个身份执行命令。sudo命令和su命令是不一样的,sudo更加灵活和安全。一个最重要的好处是它能在日志中记录所有通过sudo做的命令操作,默认记录日志为/var/log/secure。
sudo命令的配置文件为/etc/sudoers,该文件保存用来决定是否允许一个用户执行某个命令的规则定义,一般推荐用户用sudo的RPM包携带的visudo命令来对规则进行编辑定义。
假设我们希望normaluser这个用户能以root身份来执行程序,首先我们不对/etc/sudoers进行任何配置,直接使用sudo来执行:
sudo命令允许用户在RHEL上以另外一个身份执行命令。sudo命令和su命令是不一样的,sudo更加灵活和安全。一个最重要的好处是它能在日志中记录所有通过sudo做的命令操作,默认记录日志为/var/log/secure。
sudo命令的配置文件为/etc/sudoers,该文件保存用来决定是否允许一个用户执行某个命令的规则定义,一般推荐用户用sudo的RPM包携带的visudo命令来对规则进行编辑定义。
假设我们希望normaluser这个用户能以root身份来执行程序,首先我们不对/etc/sudoers进行任何配置,直接使用sudo来执行:
$ sudo /sbin/service sendmail restart Password: normaluser is not in the sudoers file. This incident will be reported.
sudo命令的失败执行会记录在/var/log/secure中:
# tail /var/log/secure ... Aug 2 14:37:49 somehost sudo: normaluser : user NOT in sudoers ; TTY=pts/2 ; PWD=/home/normaluser ; USER=root ; COMMAND=/sbin/service sendmail restart
在红帽RHEL中有一个特殊的“wheel”组,该组主要是用来完成一些特权操作。把普通用户添加到wheel组中(必须以root身份来做,并且建议wheel作为辅组):
# usermod -G normaluser,wheel normaluser
确认用户现在已经是wheel成员:
# groups normaluser normaluser : normaluser wheel
使用visudo命令来编辑文件/etc/sudoers:
# sudoers file. # # This file MUST be edited with the 'visudo' command as root. # # See the sudoers man page for the details on how to write a sudoers file. # # Host alias specification # User alias specification # Cmnd alias specification # Defaults specification # User privilege specification root ALL=(ALL) ALL # Uncomment to allow people in group wheel to run all commands # %wheel ALL=(ALL) ALL # Same thing without a password # %wheel ALL=(ALL) NOPASSWD: ALL # Samples # %users ALL=/sbin/mount /cdrom,/sbin/umount /cdrom # %users localhost=/sbin/shutdown -h now
请注意/etcsudoers文件里面的示例和注释,为了允许'wheel'组成员能以root身份运行命令,去掉下面一行的注释:
# Uncomment to allow people in group wheel to run all commands %wheel ALL=(ALL) ALL
vsisudo程序使用使用和vi编辑器一样快捷键,来进行操作。
然后以normaluser用户执行特权命令:
$ sudo /sbin/service sendmail restart Password: Shutting down sendmail: [ OK ] Shutting down sm-client: [ OK ] Starting sendmail: [ OK ] Starting sm-client: [ OK ]
/var/log/secure会记录该sudo命令的执行:
# tail /var/log/secure ... Aug 2 15:05:49 somehost sudo: normaluser : TTY=pts/2 ; PWD=/home/normaluser ; USER=root ; COMMAND=/sbin/service sendmail restart
原文:http://blog.csdn.net/fatsandwich/article/details/2164393
相关文章推荐
- 如何实现使得一个普通用户以root身份运行命令
- 如何实现使得一个普通用户以root身份运行命令和如何使用sudo命令?
- Linux如何用root身份注销一个已登录的普通用户
- Runas命令:能让域用户/普通User用户以管理员身份运行指定程序。
- Runas命令能让域用户/普通User用户以管理员身份运行指定程序
- 以root用户身份在jenkins中运行shell命令
- ubuntu普通用户如何切换到root用户身份?
- SOA架构中, 底层linux如何切换用户身份后运行函数命令?
- 如何让普通用户执行一些root用户才能执行的命令
- Runas命令:能让域用户/普通User用户以管理员身份运行指定程序
- 一、ElasticSearch5.6.3下载安装步骤 说明:ElasticSearch的运行不能用root执行,自己用useradd命令新建一个用户如下所示: sueradd chen passw
- c#如何实现一个线程暂停,等待用户输入文本后继续运行?
- 如何在 ASP.NET 应用程序中实现模拟用户身份(在ASP.NET中以管理员身份运行网站)
- c#如何实现一个线程暂停,等待用户输入文本后继续运行?
- 如何实现FreeBSD普通用户tab命令补全?
- c#如何实现一个线程暂停,等待用户输入文本后继续运行?
- 如何以管理员身份运行命令提示符打开DOS
- WinSCP 中普通用户以 root 身份登录 Linux
- 如何恢复linux的环境变量ubuntu中root用户没有办法使用命令