centos添加sudo用户

  1. 添加新用户

     [root@server-01 ~]# adduser newuser
    
  2. 设置新用户的密码,需要输入两次密码

    [root@server-01 ~]# passwd newuser
    Changing password for user newuser.
    New password:
    Retype new password:
    passwd: all authentication tokens updated successfully
    

授权sudo权限到新用户

  1. 编辑sudo配置文件

     [root@server-01 ~]# visudo
    
  2. 看到以下文件

    ## 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
    
  3. 键盘输入 i 进入插入模式. 然后上下键移动光标到以下位置

     ## Allow root to run any commands anywhere
     root    ALL=(ALL)       ALL
    
  4. 添加新创建的用户信息到如下位置<username> ALL=(ALL) ALL

    ## Allow root to run any commands anywhere
    root    ALL=(ALL)       ALL
    newuser ALL=(ALL)       ALL
    
  5. 键盘点击ESC  然后 键盘输入:wq 保存退出

测试sudo权限

  1. su - <username> 命令切换到新用户

     [root@server-01 ~]# su - newuser
     [newuser@server-01 ~]$ 
    
  2. 使用 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 ~]# 
    
  3.  执行whoami命令确认当前用户

     [root@server-01 ~]# whoami
     root
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值