3.8 sudo命令

sudo命令介绍

  • sudo命令,能够不切换用户就能够获取其余用户的权限来执行相关命令。(一般状况就是,给普通用户受权root用户的身份)
    • visudo命令,能够打开sudo命令的配置文件(会看到其实代开的是/etc/sudoers.tmp这个文件)
    [root@hf-01 ~]# visudo  会进入/etc/sudoers.tmp的配置文件中
    
     97 ## Allow root to run any commands anywhere
     98 root    ALL=(ALL)       ALL
     99 hanfeng ALL=(ALL)       /usr/bin/ls, /usr/bin/mv, /usr/bin/ls
     [root@hf-01 ~]#
    1. 输入 :set nu 来显示行号
    2. 咱们在root用户下面一行,在添加一个用户,并能够运/usr/bin/ls, usr/bin/mv, /usr/bin/cat命令,可写多个, 也可写ALL(表示全部)
    3. 而后 :wq 保存退出
  1. 默认root支持sudo,由于文件中默认有root ALL=(ALL) ALL 。
  2. 在这一行下面加入 hanfeng ALL=(ALL) /usr/bin/ls, /usr/bin/mv, /usr/bin/ls ,意思是hanfeng这个用户在执行sudo这个命令时,能够获取部分root用户的权限。
  • 从左到右依次为,第一个ALL就能够理解为主机的意思,第二个ALL是能够获取哪一个用户的权限,All就是全部包括root,第三个ALL是指使用sudo执行全部命令。

sudo命令的用法

sudo命令用法一

  • su命令能够切换用户身份
  • 在 su 在切换成普通用户后,是没法查看/root/目录的,这时用sudo命令,则可让该用户临时拥有root用户的权限
  • 使用在visudo命令中,编辑的命令要使用绝对路径
[root@hf-01 ~]# su - hanfeng
上一次登陆:四 11月  2 03:52:44 CST 2017pts/0 上
[hanfeng@hf-01 ~]$ ls /root/
ls: 没法打开目录/root/: 权限不够
[hanfeng@hf-01 ~]$ sudo /usr/bin/ls /root/  在执行命令后,会提示输入hanfeng用户的密码

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 hanfeng: 
11.txt	234    33.txt   ha.txt
[hanfeng@hf-01 ~]$ ls /root/    在hanfeng用户下直接去执行会发现没法打开/root/目录
ls: 没法打开目录/root/: 权限不够
[hanfeng@hf-01 ~]$ sudo /usr/bin/ls /root/
11.txt	234    33.txt   ha.txt
[hanfeng@hf-01 ~]$ mv /root/ha.txt /root/haha.txt
mv: 没法打开目录/root/: 权限不够
[hanfeng@hf-01 ~]$ sudo /usr/bin/mv /root/ha.txt /root/haha.txt
[hanfeng@hf-01 ~]$ 登出
[root@hf-01 ~]#

sudo命令用法二

  • 在visudo命令中, 编辑/etc/sudoers.tmp配置文件,设置NOPASSWD: ALL,则以后不再须要输入密码。
  • 在sudo命令下,可使用绝对路径命令,也能够直接使用命令去执行,获得的结果相同
[root@hf-01 ~]# visudo
[root@hf-01 ~]# su - user2
上一次登陆:四 11月  2 07:17:04 CST 2017pts/0 上
[user2@hf-01 ~]$ ls /root/
ls: 没法打开目录/root/: 权限不够
[user2@hf-01 ~]$ sudo ls /root/
11.txt	234    33.txt  haha.txt
[user2@hf-01 ~]$ sudo /usr/bin/ls /root/
11.txt	234    33.txt  haha.txt
[user2@hf-01 ~]$ 登出
[root@hf-01 ~]#

sudo命令用法三

  • 在visudo命令中,给一些用户设置一些别名,这里的别名至关于一个虚拟的用户
    • 如:User Aliases 给用户作一个别名
    • 其中的ADMINS是虚拟用户,jsmith, mikem是两个真实用户,因此说虚拟用户里面存在两个真实用户
## 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
  • 在visudo命令中,给命令设置一些别名
## 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

例子:this

[root@hf-01 ~]# visudo 进入到配置环境中

而后到
## Networking   那一段落最后加上
HANFENG_CMD = /usr/bin/ls, /usr/bin/mv, /usr/bin/cat

并将用户名hanfeng后面,去除那些绝对路径命令,修改上HANFENG_CMD,而后保存退出
root    ALL=(ALL)       ALL
hanfeng ALL=(ALL)       HANFENG_CMD

[root@hf-01 ~]# su - hanfeng
上一次登陆:四 11月  2 05:46:40 CST 2017pts/0 上
[hanfeng@hf-01 ~]$ sudo ls /root/       这里会发现能够查看/root/目录下的文件
[sudo] password for hanfeng: 
11.txt	234    33.txt   haha.txt
[hanfeng@hf-01 ~]$ sudo ls /root/
11.txt	234    33.txt   haha.txt
[hanfeng@hf-01 ~]$ sudo cat /root/haha.txt
[hanfeng@hf-01 ~]$ 登出
[root@hf-01 ~]#
  • 对用户组作出一些限制
## Allows people in group wheel to run all commands
    109 %wheel  ALL=(ALL)       ALL

sudo命令总结:

  1. 在visudo命令中的配置文件下,输入 :set nu 则每行会显示出行号。
  2. 在第一次使用sudo命令,去执行某条命令,会要求输入当前用户的密码,但在第二次执行该条命令时,直接输入便可执行(或者,在visudo的配置文件中,在该用户的写上无需密码,如hanfeng ALL=(ALL) NOPASSWD:ALL 就可直接登陆,无需密码了),再添加命令须要使用绝对路径
  3. 在visudo的配置文件中写错了,保存退出后,会报错,这时选择 e 而后回车继续进去编辑便可。
  4. 在visudo的配置文件中,添加命令,须要使用绝对路径(使用命令的时候可使用绝对路径命令或命令去执行)
  5. sudo命令,就是用普通用户临时拥有root用户的身份,去执行某一条命令。(这样就能够避免将root用户给普通用户了)
  6. 给用户、命令作一些别名,对用户组作出一些限制
相关文章
相关标签/搜索