-
添加新用户
[root@server-01 ~]# adduser newuser
-
设置新用户的密码,需要输入两次密码
[root@server-01 ~]# passwd newuser Changing password for user newuser. New password: Retype new password: passwd: all authentication tokens updated successfully
授权sudo权限到新用户
-
编辑sudo配置文件
[root@server-01 ~]# visudo
-
看到以下文件
## 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
-
键盘输入 i 进入插入模式. 然后上下键移动光标到以下位置
## Allow root to run any commands anywhere root ALL=(ALL) ALL
-
添加新创建的用户信息到如下位置
<username> ALL=(ALL) ALL
## Allow root to run any commands anywhere root ALL=(ALL) ALL newuser ALL=(ALL) ALL
-
键盘点击ESC 然后 键盘输入:wq 保存退出
测试sudo权限
-
su
- <username>
命令切换到新用户[root@server-01 ~]# su - newuser [newuser@server-01 ~]$
-
使用
sudo -i
命令测试是否能切换到root[newuser@server-01 ~]$ sudo -i 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 newuser: [root@server-01 ~]#
-
执行whoami命令确认当前用户
[root@server-01 ~]# whoami root