当前位置:   article > 正文

Linux上使用sudo命令给其他用户提权,为其赋予超级用户权限执行部分无权限命令以及sudo的常见参数简介_给用户赋sudo权限 linux

给用户赋sudo权限 linux

 本文基于Linux上CentOS 7版本进行配置演示

目录

一.编辑配置文件给其他用户配置提权

1.查看配置文件内常用的参数

2.查看其他用户无权限时的情况

3.为其他用户配置sudo权限并测试

二.配置文件使其他用户执行sudo命令时无需密码

1.深感输密码的麻烦,为其设置使用sudo无需密码

2.在其他用户方测试

三.sudo命令的常见参数

1.sudo -h

2.sudo -l

3.sudo -u 用户名

4.sudo -k

5.sudo -b 命令


一.编辑配置文件给其他用户配置提权

1.查看配置文件内常用的参数

(1)通过cat /etc/sudoers来查看

[root@sulibao ~]# cat /etc/sudoers

 (2)通过visudo来查看/编辑

[root@sulibao ~]# visudo

(3)配置文件内常见的参数

Allow root to run any commands anywhere 允许某些用户在任何地方运行任何命令
Allows people in group wheel to run all commands允许组中的人员运行所有命令
Same thing without a password允许某些用户使用命令时无需密码
Allows members of the users group to mount and unmount the cdrom as root允许用户组的成员作为root用户装载和卸载CDROM 
Allows members of the users group to shutdown this system允许用户组的成员关闭此系统

2.查看其他用户无权限时的情况

切换至其他用户下连创建目录的权限都没有,需要为其提权

  1. [sulibao@sulibao ~]$ mkdir /aaa
  2. mkdir: cannot create directory ‘/aaa’: Permission denied

3.为其他用户配置sudo权限并测试

(1)选择编辑/etc/sudoers或visudo其中一种方式修改,此处以vim /etc/sudoers作演示

[root@sulibao ~]# vim /etc/sudoers

(2)找到Allows people in group wheel to run all commands这一行在行后按图示格式编辑

进入vim后可以输入“:set nu”来设置行号方便查看,大概在第100行

编辑完成后使用“:wq!”强制保存退出 

也是可以通过%组名来指定组内成员的sudo权限

(3)在其他用户方进行测试,输入用户密码后命令即生效

注意:如果用户是远程连接单独出来的新连接需要断开连接重连,也可以直接在root用户下使用“su - 用户名”登录上去

  1. [sulibao@sulibao ~]$ mkdir /bbb
  2. mkdir: cannot create directory ‘/bbb’: Permission denied
  3. [sulibao@sulibao ~]$ sudo mkdir /bbb
  4. [sudo] password for sulibao:
  5. [sulibao@sulibao ~]$ ll / | grep bbb
  6. drwxr-xr-x 2 root root 6 Jan 9 19:06 bbb

二.配置文件使其他用户执行sudo命令时无需密码

1.深感输密码的麻烦,为其设置使用sudo无需密码

编辑配置文件/etc/sudoers,找到Same thing without a password这行(大概在110行)按图示编辑,同样强制保存退出0646081ee93b42fca9db4ae28880309d.png

2.在其他用户方测试

  1. [sulibao@sulibao ~]$ mkdir /qwe
  2. mkdir: cannot create directory ‘/qwe’: Permission denied
  3. [sulibao@sulibao ~]$ sudo mkdir /qwe
  4. [sulibao@sulibao ~]$ // 配置成功,不提示输入普通用户密码

三.sudo命令的常见参数

1.sudo -h

列出sudo命令的帮助信息

  1. [sulibao@sulibao ~]$ sudo -h
  2. sudo - execute a command as another user
  3. usage: sudo -h | -K | -k | -V
  4. usage: sudo -v [-AknS] [-g group] [-h host] [-p prompt] [-u user]
  5. usage: sudo -l [-AknS] [-g group] [-h host] [-p prompt] [-U user] [-u user] [command]
  6. usage: sudo [-AbEHknPS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] [VAR=value] [-i|-s] [<command>]
  7. usage: sudo -e [-AknS] [-r role] [-t type] [-C num] [-g group] [-h host] [-p prompt] [-T timeout] [-u user] file ...
  8. Options:
  9. -A, --askpass use a helper program for password prompting
  10. -b, --background run command in the background
  11. -C, --close-from=num close all file descriptors >= num
  12. -E, --preserve-env preserve user environment when running command
  13. --preserve-env=list preserve specific environment variables
  14. -e, --edit edit files instead of running a command
  15. -g, --group=group run command as the specified group name or ID
  16. -H, --set-home set HOME variable to target user's home dir
  17. -h, --help display help message and exit
  18. -h, --host=host run command on host (if supported by plugin)
  19. -i, --login run login shell as the target user; a command may also be specified
  20. -K, --remove-timestamp remove timestamp file completely
  21. -k, --reset-timestamp invalidate timestamp file
  22. -l, --list list user's privileges or check a specific command; use twice for longer format
  23. -n, --non-interactive non-interactive mode, no prompts are used
  24. -P, --preserve-groups preserve group vector instead of setting to target's
  25. -p, --prompt=prompt use the specified password prompt
  26. -r, --role=role create SELinux security context with specified role
  27. -S, --stdin read password from standard input
  28. -s, --shell run shell as the target user; a command may also be specified
  29. -t, --type=type create SELinux security context with specified type
  30. -T, --command-timeout=timeout terminate command after the specified time limit
  31. -U, --other-user=user in list mode, display privileges for user
  32. -u, --user=user run command (or edit file) as specified user name or ID
  33. -V, --version display version information and exit
  34. -v, --validate update user's timestamp without running a command
  35. -- stop processing command line arguments
  36. [sulibao@sulibao ~]$

2.sudo -l

列出当前用户可以利用sudo执行哪些命令

  1. [sulibao@sulibao ~]$ sudo -l
  2. [sudo] password for sulibao:
  3. Matching Defaults entries for sulibao on sulibao:
  4. !visiblepw, always_set_home, match_group_by_gid, always_query_group_plugin, env_reset, env_keep="COLORS DISPLAY HOSTNAME HISTSIZE KDEDIR LS_COLORS", env_keep+="MAIL PS1 PS2 QTDIR
  5. USERNAME LANG LC_ADDRESS LC_CTYPE", env_keep+="LC_COLLATE LC_IDENTIFICATION LC_MEASUREMENT LC_MESSAGES", env_keep+="LC_MONETARY LC_NAME LC_NUMERIC LC_PAPER LC_TELEPHONE",
  6. env_keep+="LC_TIME LC_ALL LANGUAGE LINGUAS _XKB_CHARSET XAUTHORITY", secure_path=/sbin\:/bin\:/usr/sbin\:/usr/bin
  7. User sulibao may run the following commands on sulibao:
  8. (ALL) ALL

3.sudo -u 用户名

以该用户的身份执行命令

  1. [sulibao@sulibao ~]$ sudo -u root mkdir /asd
  2. [sulibao@sulibao ~]$ ll / | grep asd
  3. drwxr-xr-x 2 root root 6 Jan 9 22:28 asd

4.sudo -k

设置下次使用sudo时需要密码,即使已经在/etc/sudoers设置了无需密码,不过sudo -k的效果只生效一次

  1. [sulibao@sulibao ~]$ sudo mkdir /qwer
  2. [sulibao@sulibao ~]$ sudo -k
  3. [sulibao@sulibao ~]$ sudo mkdir /ppp //此时sudo -k后需要密码
  4. [sudo] password for sulibao:
  5. [sulibao@sulibao ~]$
  6. [sulibao@sulibao ~]$ sudo mkdir /ooo
  7. //生效一次后,使用sudo即继续按照配置文件/etc/sudoers内的配置执行
  8. [sulibao@sulibao ~]$

5.sudo -b 命令

在后台运行命令

[sulibao@sulibao ~]$ sudo -b mkdir /ttt

其余参数请查看sudo -h的内容,需要什么用什么。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/528133
推荐阅读
相关标签
  

闽ICP备14008679号