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 --groups dygrp deyu21
    
  3. 查詢帳號 deyu21,其附屬群組為 dygrp。
    [root@kvm8 ~]# id deyu21
    uid=3586(deyu21) gid=3587(deyu21) groups=3587(deyu21),3586(dygrp)
    
  4. 設定 deyu21 的密碼
    [root@kvm8 ~]# echo qweqwe | passwd --stdin deyu21
    BAD PASSWORD: The password is shorter than 8 characters
    
  5. 登入 deyu21
    [root@kvm8 ~]# su --login deyu21
    
  6. 一般帳號 deyu21 無法使用 hwclock 命令查詢硬體時間
    [deyu21@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.
    
  7. 一般帳號 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.
    
    For security reasons, the password you type will not be visible.
    
    [sudo] password for deyu21:
    2025-10-22 14:44:45.757316+08:00