当前位置: 代码网 > 服务器>服务器>Linux > centos7修改sudoers文件给用户添加root权限方法

centos7修改sudoers文件给用户添加root权限方法

2026年09月09日 Linux 我要评论
centos7版本,我们不可能每次都用root账号登陆,root账号公认的不安全,所以一般都会创建一个专有权限的普通账号,下面创建一个具有root权限的普通账号[root@localhost ~]#

centos7 版本,我们不可能每次都用root账号登陆,root账号公认的不安全,所以一般都会创建一个专有权限的普通账号,

下面创建一个具有root权限的普通账号

[root@localhost ~]# groupadd test
[root@localhost ~]# useradd -g test test
[root@localhost ~]# passwd test
更改用户 test 的密码 。
新的 密码:
无效的密码: 过于简单化/系统化
无效的密码: 过于简单
重新输入新的 密码:
passwd: 所有的身份验证令牌已经成功更新。

创建完用户后

需要给用户添加权限。

[root@localhost ~]# chmod -v u+w /etc/sudoers  
mode of '/etc/sudoers' changed from 0440 (r--r-----) to 0640 (rw-r-----)

修改sudoers权限

使其可编辑,编辑sudoers文件。

[root@localhost ~]# vim /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, /usr/bin/systemctl start, /usr/bin/systemctl stop, /usr/bin/systemctl reload, /usr/bin/systemctl restart, /usr/bin/systemctl status, /usr/bin/systemctl enable, /usr/bin/systemctl disable

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

# prior to version 1.8.15, groups listed in sudoers that were not
# found in the system group database were passed to the group
# plugin, if any. starting with 1.8.15, only groups of the form
# %:group are resolved via the group plugin by default.
# we enable always_query_group_plugin to restore old behavior.
# disable this option for new behavior.
defaults    always_query_group_plugin

defaults    env_reset
defaults    env_keep =  "colors display hostname histsize 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

在下面添加同样的一样

root    all=(all)       all
test    all=(all)       all

注意:

test all=(all)       nopasswd:all

可以将第二组修改为nopasswd:all,这样使用sudo的时候就不用输入密码了。

将sudoers文件权限修改为不可编辑

[root@localhost ~]# chmod -v u-w /etc/sudoers   
mode of "/etc/sudoers" changed from 0640 (rw-r-----) to 0440 (r--r-----)

此时,具有root权限的用户test就操作完了。

可能出现问题

1、使用test用户进行切换(su root)root用户,提示鉴定故障。

可能在权限不足带来的问题。

检查办法

如下:

1)检查/etc目录下passwd的权限,该文件应该具备的权限如下:

[root@localhost ~]# ll /etc/passwd
-rw-r--r--. 1 root root 2347 4f  16 21:00 /etc/passwd

如果该权限不足,则使用chmod 补充完成。

2)检查/bin/su文件是否有s位权限 也有可能是所属用户为普通用户,该文件应该具备的权限如下:

[root@localhost ~]# ll /bin/su
-rwsrwxrwx. 1 root root 32128 8f   9 2019 /bin/su

如果该权限不足,则使用chmod u+s /bin/su补充完成即可。

总结

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

(0)

相关文章:

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

发表评论

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