sudo 群組測試

  1. 範例:visudo 編輯 /etc/sudoers,增加群組 dygrp 可以 root 權限從任何主機執行命令 /sbin/hwclock。
    [root@kvm8 ~]# visudo
    [root@kvm8 ~]# grep dygrp /etc/sudoers
    %dygrp	ALL=(ALL) 	/sbin/hwclock
    
  2. 新增群組 dygrp,並新增用戶 deyu21,其附屬群組為 dygrp。
    [root@kvm8 ~]# groupadd dygrp
    [root@kvm8 ~]# useradd deyu21 -G dygrp
    
  3. 設定 deyu21 的密碼
    [root@kvm8 ~]# echo qweqwe | passwd --stdin deyu21
    Changing password for user deyu21.
    passwd: all authentication tokens updated successfully.
    
  4. 切換身份為 deyu21
    [root@kvm8 ~]# su - deyu21
    
  5. 一般用戶 deyu21 無法使用 hwclock 命令查詢硬體時間
    [deyu9@kvm8 ~]$ hwclock
    hwclock: Cannot access the Hardware Clock via any known method.
    hwclock: Use the --verbose option to see the details of our search for an access method.
    
  6. 一般用戶 deyu21 附屬群組 dygrp,可以使用 sudo hwclock 查詢硬體時間。
    [deyu21@kvm8 ~]$ sudo hwclock
    
    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 deyu21:
    2022-08-30 15:26:52.203540+08:00