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

例子:

[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. 给用户、命令做一些别名,对用户组做出一些限制

转载于:https://my.oschina.net/u/3707314/blog/1559474

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值