K. linux命令学习系列-用户切换su,sudo

1. su命令

su命令不加参数,默认切到root用户,需要输入root用户密码进行验证。

exit命令可退出root用户。

su命令可以加一个“-”再加上用户名,此时,不但身份变化了,同时也拥有了此用户的“用户环境”,比如用户家目录以及此用户的其它个性化设置。

2. sudo命令

强烈建议通过visudo命令来修改该文件,通过visudo修改,如果配置出错,会有提示。

不过,系统文档推荐的做法,不是直接修改/etc/sudoers文件,而是将修改写在/etc/sudoers.d/目录下的文件中。

如果使用这种方式修改sudoers,需要在/etc/sudoers文件的最后行,加上#includedir /etc/sudoers.d一行(默认已有):

[root@hadoop01 ~]# cat /etc/sudoers
## Sudoers allows particular users to run various commands as
## the root user, without needing the root password.
##
## 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.

## 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

## 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 ADMINS = jsmith, mikem


## 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/bin/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

#
# Refuse to run if unable to disable echo on the tty.
#
Defaults   !visiblepw

#
# Preserving HOME has security implications since many programs
# use it when searching for configuration files. Note that HOME
# is already set when the the env_reset option is enabled, so
# this option is only effective for configurations where either
# env_reset is disabled or HOME is present in the env_keep list.
#
Defaults    always_set_home

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

#
# Adding HOME to env_keep may enable a user to run unrestricted
# commands via sudo.
#
# Defaults   env_keep += "HOME"

Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin

## Next comes the main part: which users can run what software on 
## which machines (the sudoers file can be shared between multiple
## systems).
## 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 ALL = NETWORKING, SOFTWARE, SERVICES, STORAGE, DELEGATING, PROCESSES, LOCATE, DRIVERS

## Allows people in group wheel to run all commands
# %wheel    ALL=(ALL)   ALL

## Same thing without a password
# %wheel    ALL=(ALL)   NOPASSWD: ALL

## Allows members of the users group to mount and unmount the 
## cdrom as root
# %users  ALL=/sbin/mount /mnt/cdrom, /sbin/umount /mnt/cdrom

## Allows members of the users group to shutdown this system
# %users  localhost=/sbin/shutdown -h now

## Read drop-in files from /etc/sudoers.d (the # here does not mean a comment)
#includedir /etc/sudoers.d

注意了,这里的指令#includedir是一个整体, 前面的#号不能丢,并非注释,也不能在#号后有空格。

任何在/etc/sudoers.d/目录下,不以~号结尾的文件和不包含.号的文件,都会被解析成/etc/sudoers的内容。

lisi 192.168.216.130=(root) NOPASSWD:  /usr/sbin/usradd

授权用户/组 主机=[(切换到哪些用户或组)] [是否需要输入密码验证] 命令1,命令2,...

[root@hadoop01 home]# su - zhangsan
[zhangsan@hadoop01 ~]$ ll
总用量 0
[zhangsan@hadoop01 ~]$ cd ..
[zhangsan@hadoop01 home]$ ll
总用量 16
drwx------. 3 lisi     lisi     4096 9月   2 05:44 lisi
drwxr-xr-x. 3 root     root     4096 8月  29 06:45 test
drwx------. 3 wangwu   wangwu   4096 9月   2 06:39 wangwu
drwx------. 3 zhangsan zhangsan 4096 8月  30 09:30 zhangsan
[zhangsan@hadoop01 home]$ sudo useradd zhaoliu

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 zhangsan: 
zhangsan 不在 sudoers 文件中。此事将被报告。
[root@hadoop01 sudoers.d]# su - lisi
[lisi@hadoop01 ~]$ cd ..
[lisi@hadoop01 home]$ ll
总用量 12
drwx------. 3 lisi     lisi     4096 9月   2 05:44 lisi
drwxr-xr-x. 3 root     root     4096 8月  29 06:45 test
drwx------. 3 zhangsan zhangsan 4096 8月  30 09:30 zhangsan
[lisi@hadoop01 home]$ useradd wangwu
-bash: /usr/sbin/useradd: 权限不够
[lisi@hadoop01 home]$ sudo useradd wangwu
正在创建信箱文件: 文件已存在
[lisi@hadoop01 home]$ ll
总用量 16
drwx------. 3 lisi     lisi     4096 9月   2 05:44 lisi
drwxr-xr-x. 3 root     root     4096 8月  29 06:45 test
drwx------. 3 wangwu   wangwu   4096 9月   2 06:39 wangwu
drwx------. 3 zhangsan zhangsan 4096 8月  30 09:30 zhangsan

如果你将授权写成如下安全性欠妥的格式:

lucy ALL=(ALL) chown,chmod,useradd

命令的绝对路径可通过which指令查看到: 比如which useradd可以查看到命令useradd的绝对路径: /usr/sbin/useradd

你可能感兴趣的:(K. linux命令学习系列-用户切换su,sudo)