Cấu hình visudo phân quyền người dùng trong LINUX Centos 7

bởi | CentOS 7, LPI

Home » LPI » Cấu hình visudo phân quyền người dùng trong LINUX Centos 7

cấu hình cho 1 user có quyền root

– vào file visudo
[cent@thanhdd root]$ visudo
or
[cent@thanhdd root]# vi /etc/sudoers
đi đến dòng 98
## thêm 1 dòng cho user: testuser để cho user test có toàn quyền root
root ALL=(ALL) ALL
testuser ALL=(ALL) ALL

# Kiểm tra thử
root@thanhdd ~]# su testuser
[testuser@thanhdd root]$ sudo /usr/bin/cat /etc/shadow
Thực hiện được lệnh này
#user thuộc group cent thì thực hiện được mọi lệnh và không cần nhập
%cent ALL=(ALL) NOPASSWD: ALL

Đặt một số lệnh user có quyền root không được phép

– Ví dụ: không cho testuser có quyền shutdown
#vào file visudo
[root@thanhdd ~]# visudo
# thêm alias cho loạt lệnh tắt máy và lệnh sửa file visudo
Cmnd_Alias SHUTDOWN = mnd_Alias SHUTDOWN = /sbin/halt, /sbin/shutdown, /sbin/poweroff, /sbin/reboot, /sbin/init, /usr/sbin/visudo
# add !SHUTDOWN để phủ định các lệnh trong comand alias SHUTDOWN cho usertest. không cho usertest có quyền thực hiện các lệnh này
cent ALL=(ALL) ALL, !SHUTDOWN
# Thử lại:
[cent@thanhdd root]$ visudo
visudo: /etc/sudoers: Permission denied

Cấp quyền root cho một nhóm người dùng

#vào file visudo
[root@thanhdd ~]# visudo
# thêm alias cho 1 loạt lệnh quản lý người dùng
Cmnd_Alias USERTEST = /usr/sbin/useradd, /usr/sbin/userdel, /usr/sbin/usermod, \
/usr/bin/passwd, /usr/sbin/visudo
# thêm 1 dòng ở dưới cùng, để cho phép nhóm usertest có thể thực hiện 1 số lệnh quản lý user
%usertest ALL=(ALL) USERTEST
# tạo group usertest và nhóm 1 số user vào group này
[root@thanhdd ~]# groupadd usertest
[root@thanhdd ~]# usermod -aG usertest test1
# Thử cho user test1 tạo thêm user test3:
[test1@thanhdd ~]$ sudo /usr/sbin/useradd test3
[test1@thanhdd ~]$ sudo /usr/bin/passwd test3

cấp 1 số lệnh đặc quyền root cho 1 user khác

# VD cấp cho user test3 có quyền thực thi 2 lệnh init và systemclt
test3 ALL=(ALL) /usr/sbin/init, /bin/systemctl
# thử reboot máy
[test3@thanhdd ~]$ sudo init 6
reboot thành công

Nhật ký sudo

Nhật ký cho sudo được lưu trong /var/log/secure nhưng có nhiều loại nhật ký trong đó. Vì vậy, muốn giữ nhật ký của sudo trong một tệp riêng, hãy thiết lập như sau.
[root@thanhdd ~]# visudo
#thêm 1 dòng sau vào cuối file visudo
Defaults syslog=local1
# vi sửa file /etc/rsyslog.conf
[root@thanhdd ~]# vi /etc/rsyslog.conf
# dòng 54 add thêm ;local1.none

*.info;mail.none;authpriv.none;cron.none;local1.none /var/log/messages

add thêm 1 dòng:

local1.*                                                /var/log/sudo.log

# sau đó restart rsyslog [root@thanhdd ~]# systemctl restart rsyslog

# Kiểm tra log [root@thanhdd ~]# cat /var/log/secure