su&sudo

来源:互联网 发布:阿里云如何重装系统 编辑:程序博客网 时间:2024/04/16 22:28

 

su是超级命令,sudo是受限超级命令,二者的比较见http://blog.sina.com.cn/s/blog_4c532c0f0100c3ax.html。

 

家里的电脑用虚拟机装了Fedora10,同样有登录图形界面而不是命令行且不能直接用root登录只能登录受限用户的困惑。

尝试在公司对Ubuntu的操作,进行sudo -i输入受限用户的密码后却收到提示:ly1031 is not in the sudoers file.This incident will be reported.(ly1031)是我的受限用户。

尝试在受限用户下直接输入su,并输入密码,正常进入root用户了。这与Ubuntu正好相反。

 

为什么会有这种不同呢?

 

Fedora 10里将普通用户添加到sudo组http://blog.sina.com.cn/s/blog_6056c1c30100dq5r.html

中是如下解释的,简单而又清晰:

在LINUX里当我们需要执行一条root权限的命令时,每次都要su到root,总是有些不方便?那么我们可以用sudo代替它。fedora 10默认新建的用户不在sudo组,但可以编辑/etc/sudoers文件将普通用户加入sudo组。要注意的是修改该文件只能使用visudo命令:

1、首先切换到root
#su -
(注意有- ,这和su不同,在用命令"su"的时候只是切换到root,但没有把root的环境变量传过去,还是当前用户的环境变量,而使用带横线参数的"su -"命令则将环境变量也一起转换过去,就象用root登录一样)

2、然后
#visudo
这个和vi的用法一样,移动光标,到最后一行,按a,进入append模式,输入以下这行内容
username ALL=(ALL)
然后按Esc,再输入:wq保存文件并退出,这样就把自己加入了sudo组,可以使用sudo命令了。
如果不想在sudo的时候输入密码,干脆就把上面那行换成下面这行即可:
username ALL=(ALL)NOPASSWD: ALL

3、
测试 sudo
测试以上是否正确配置了 sudo ,只需要在普通用户权限下输入
$ sudo whoami
如果配置正确,则命令会返回“root”字样。

***************************************************************************

而在Fedora (Redhat, RHEL)中使用sudo

http://hi.baidu.com/yangyingchao/blog/item/42e8ce03ccf8108bd43f7cb2.html 中则如下,详尽:

大家都知道,在Derbian和Ububtu中,为了安全起见,以普通用户登录以后在终端中不允许通过su命令来切换为超级用户,想要代替超级用户执行命令必须通过sudo命令来实现,使 用sudo命令确实比使用su更加安全。例如,在使用su的时候,由于本身属于超级用户,可以对任何文件进行任何操作,如果因为忘记自己以经成为了超级用 户、不小心删除了某些系统所需的重要文件,将会引起及其严重的后果。而如果不使用su,则想执行某个命令,系统会自动检察用户是否拥有这个权限,如果没 有,则提醒用户,这个时候再使用sudo命令来暂时替代超级用户来执行,则在这个过程中用户最起码会考虑一下操作是否正确,这样在一定程度上可以防止因粗 心而带来的灾难。
      但而在基于RedHat的Linux发行版(Fedora, RHEL, RedHat, 红旗……)默认使用的是su,而非sudo,如果想要使用sudo,必须正 确配置sudoers文件。sudoers位于/etc下,在使用sudo的时候,系统查询sudoers里面给用户分配的权限,然后根据查询结果进行相 关操作。下面这个文件是我从Fedora 8的sudoers中拷贝出来的,然后进行了翻译和整理,大家可以看一下,然后根据里面的提示进行操作,正确配置自己机器的sudoers文件,打造一个 更加安全的系统。


####################################################################
## Sudoers allows particular users to run various commands as the root user, without needing the root password.
## Sudoers 可以使特定的用户以根用户的身份执行各种命令,而无需根密码。

## Examples are provided at the bottom of the file for collections of related commands, which can then be delegated out to particular users or groups.
##文件的后面给出了相关指令集的例子,这些例子中的用户和组均可用特定的用户和组来代替(替换成你的系统里面的用户和组。)

## This file must be edited with the 'visudo' command.
## 这一文件必须使用visudo来进行编辑!!




## Host Aliases
## Groups of machines. You may prefer to use hostnames (perhaps using wildcards for entire domains) or IP addresses instead.
# Host_Alias     FILESERVERS = fs1, fs2
# Host_Alias     MAILSERVERS = smtp, smtp2
# 机器的组,可以使用hostname或者IP地址来替代。




## User Aliases
## These aren't often necessary, as you can use regular groups (ie, from files, LDAP, NIS, etc) in this file - just use %groupname rather than USERALIAS.
# User_Alias yyc = yyc
#用户入口,这通常并不是必需的,因为我们可以用正常组来替代,即:使用组名(groupname)而不是USERALIAS。



## Command Aliases
## These are groups of related commands...
#控制入口,以下是相关命令的分组。

## Networking(网络)
Cmnd_Alias NETWORKING = /sbin/route, /sbin/ifconfig, /bin/ping, /sbin/dhclient, /usr/bin/net, /sbin/iptables, /usr/bin/rfcomm, /usr/bin/wvdial, /sbin/iwconfig, /sbin/mii-tool

## Installation and management of software(软件的安装和管理)
Cmnd_Alias SOFTWARE = /bin/rpm, /usr/bin/up2date, /usr/bin/yum

## Services(服务)
Cmnd_Alias SERVICES = /sbin/service, /sbin/chkconfig

## Updating the locate database(本地数据库升级)
Cmnd_Alias LOCATE = /usr/sbin/updatedb

## Storage(存储)
Cmnd_Alias STORAGE = /sbin/fdisk, /sbin/sfdisk, /sbin/parted, /sbin/partprobe, /bin/mount, /bin/umount

## Delegating permissions (委派权限)
Cmnd_Alias DELEGATING = /usr/sbin/visudo, /bin/chown, /bin/chmod, /bin/chgrp

## Processes (进程相关)
Cmnd_Alias PROCESSES = /bin/nice, /bin/kill, /usr/bin/kill, /usr/bin/killall

## Drivers(驱动)
Cmnd_Alias DRIVERS = /sbin/modprobe



# Defaults specification(默认说明)

#
# Disable "ssh hostname sudo <cmd>", because it will show the password in clear. You have to run "ssh -t hostname sudo <cmd>".
#禁止在使用SSH登录的时候使用sudo,这样会显示明码。如果想要在SSH登录的时候使用sudo,登录时候必须使用ssh -t <hostname> 来进行登录。
Defaults    requiretty

Defaults    env_reset
Defaults    env_keep = "COLORS DISPLAY HOSTNAME HISTSIZE INPUTRC KDEDIR /
                        LS_COLORS MAIL PS1 PS2 QTDIR USERNAME /
                        LANG LC_ADDRESS LC_CTYPE LC_COLLATE LC_IDENTIFICATION /
                        LC_MEASUREMENT LC_MESSAGES LC_MONETARY LC_NAME LC_NUMERIC /
                        LC_PAPER LC_TELEPHONE LC_TIME LC_ALL LANGUAGE LINGUAS /
                        _XKB_CHARSET XAUTHORITY"

## Next comes the main part: which users can run what software on which machines (the sudoers file can be shared between multiple systems).
## 这一部分是重点,他规定了哪些用户可以在哪些机器上去运行哪些软件。注:不同的系统可以使用同一个sudoers文件。
## Syntax:
##
##     user    MACHINE=COMMANDS
##
## The COMMANDS section may have other options added to it.
##
## Allow root to run any commands anywhere (规定:根用户可以在任何系统上执行所有程序)
root    ALL=(ALL)     ALL

## Allows members of the 'sys' group to run networking, software, service management apps and more.使组sys里面的用户可以执行网络,软件,服务管理以及其他命令。
# %sys ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS
#将上面的注释去掉即可实现该功能,其中sys改为自己机器上的组即可。下同。

## Allows people in group wheel to run all commands(使组wheel里面的用户可以执行所有程序,但使用sudo的时候##需要输入个人密码。)
# %wheel    ALL=(ALL)    ALL

## Same thing without a password (同上,但是不需要输入密码,我的系统在这里进行了修改,使我可以直接使用sudo命令)
# %wheel    ALL=(ALL)    NOPASSWD: ALL
%yyc
ALL=(ALL)    NOPASSWD: ALL
## Allows members of the users group to mount and unmount the cdrom as root
#允许wheel组里面的用户执行mount 和umount。
# %users ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

## Allows members of the users group to shutdown this system允许users里面的用户关闭系统。
# %users localhost=/sbin/shutdown -h now




##################################################################################################### 其实,如果只是想在一个用户不多的机器上实现sudo并不难,只要修改这个文件相应的位置即可,文件里面的用户,组,以及相关命##令都可以根据自己的实 际情况进行设定,这不难。但是如果一个服务器上有很多的用户,很多的组,如何去设定就要仔细推敲一下了

原创粉丝点击