当前位置: 代码网 > 服务器>服务器>Linux > Linux使用其他用户(非root用户)设置root权限及免密(Centos7为例)

Linux使用其他用户(非root用户)设置root权限及免密(Centos7为例)

2024年06月17日 Linux 我要评论
linux其他用户设置root权限及免密在linux系统中我们经常要创建一些非root用户来完成一些特定的操作,有时我们需要执行一些root用户才能执行的命令,这时我们需要给该用户设置root的权限下

linux其他用户设置root权限及免密

在linux系统中我们经常要创建一些非root用户来完成一些特定的操作,有时我们需要执行一些root用户才能执行的命令,这时我们需要给该用户设置root的权限

下面将演示如何操作:

1、首先创建一个test用户

[root@smiletian ~]# groupadd -g 1234 tian
[root@smiletian ~]# useradd -u 1234 -g tian test
[root@smiletian ~]# passwd test 
changing password for user test.
new password: 
retype new password: 
passwd: all authentication tokens updated successfully.

2、切换到test用户下

当我们执行sudo命令时,会提示输test用户密码,输完后提示,test用户不在sudoer文件里,所以是无法执行sudo命令的。

[root@smiletian test]# su - test
last login: sun sep 18 23:35:57 pdt 2022 on pts/2
[test@smiletian ~]$ sudo mkdir abc
[sudo] password for test: 
test is not in the sudoers file.  this incident will be reported.

3、切换到root用户

编辑 /etc/sudoers文件

[test@smiletian ~]$ su
password: 
[root@smiletian test]# vi /etc/sudoers

找到  ## allow root to run any commands anywhere 这栏,在root下添加 test用户,

## allow root to run any commands anywhere
root    all=(all)       all
test    all=(all)       all

找到## same thing without a password一栏,将%wheel all=(all)前面的#去掉,

  • 更改前:
## same thing without a password
#%wheel all=(all)       nopasswd: all 
  • 更改后:
## same thing without a password
%wheel all=(all)       nopasswd: all

wq!保存退出。

4、切换到test用户下

可以使用sudo命令,输入一次密码后,不再需要再每次都输入密码即可进行操作。

[root@smiletian test]# su - test
last login: sun sep 18 23:54:07 pdt 2022 on pts/2
[test@smiletian ~]$ su
password: 
[root@smiletian test]# vi /etc/sudoers
[root@smiletian test]# su - test
last login: mon sep 19 00:08:24 pdt 2022 on pts/2
[test@smiletian ~]$ sudo mkdir abc
[sudo] password for test: 
[test@smiletian ~]$ sudo mkdir abd
[test@smiletian ~]$ sudo mkdir abf
[test@smiletian ~]$ ll
total 0
drwxr-xr-x. 2 root root 6 sep 19 00:20 abc
drwxr-xr-x. 2 root root 6 sep 19 00:20 abd
drwxr-xr-x. 2 root root 6 sep 19 00:21 abf

linux配置root权限,免密执行sudo命令

配置用户具有root权限,方便后期加sudo执行root权限的命令

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

修改/etc/sudoers文件,在%wheel这行下面添加一行

如下所示:

## allow root to run any commands anywhere
root    all=(all)     all

## allows people in group wheel to run all commands
%wheel  all=(all)       all
用户名   all=(all)     nopasswd:all

注意:

用户名这一行不要直接放到root行下面,因为所有用户都属于wheel组,你先配置了用户名具有免密功能,但是程序执行到%wheel行时,该功能又被覆盖回需要密码。

所以用户名要放到%wheel这行下面。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com