Quản Lý User Local và Group Local – LINUX CentOS

bởi | CentOS 7, LPI

Home » LPI » Quản Lý User Local và Group Local – LINUX CentOS

Quản lý User Local

– Kiểm tra danh sách các user đang có trong máy

[root@thanhdd ~]# cat /etc/passwd
=> Kiểm tra nội dung của file /etc/passwd. Sẽ thấy danh sách các user và thuộc tính của user
[root@thanhdd ~]# cat /etc/passwd | more
=> xem nội dung cua file /etc/passwd
[root@thanhdd ~]# tail -n 2 /etc/passwd
nginx:x:998:996::/home/nginx:/sbin/nologin
sinhvien:x:1000:1000::/home/sinhvien:/bin/bash
=> xem 2 dong cuoi cua file /etc/passwd
Username:Password_x:UID:GID:Thong_tin_User:Home folder:Shell dang nhap
:/bin/bash user đăng nhập được
:/sbin/ user dịch vụ

+ Để bảo mật /etc/passwd cần thay đổi quyền chỉ cho user có quyền root đọc hay thay đổi file này
[root@centos7 ~]# ls -l /etc/passwd
-rw-r--r-- 1 root root 988 Jun 12 01:11 /etc/passwd
-rw-r–r–: user other đang có quyền đọc file này

+ Kiểm tra các group có trong máy Linux
[root@thanhdd ~]# cat /etc/group
=> danh sach cac group dang co trong may tinh

– Tạo thêm user trong máy linux bằng câu lệnh “useradd”

[root@thanhdd ~]# useradd sinhvien
=> tao them user “sinhvien”
=> tao them group “sinhvien”
=> user “sinhvien” thuoc group “sinhvien”
=> home folder cua user o vi tri la “/home/sinhvien”
[root@thanhdd ~]# id sinhvien
uid=1000(sinhvien) gid=1000(sinhvien) groups=1000(sinhvien)
[root@thanhdd ~]# ls -l /home
total 0
drwx------ 2 sinhvien sinhvien 62 Jun 12 01:11 sinhvien
=> Sẽ tạo ra một home folder nữa cho user này
[root@thanhdd ~]# tail -n 1 /etc/passwd
sinhvien:x:1000:1000::/home/sinhvien:/bin/bash
=> Kiểm tra lại

+ Đặt password cho user
[root@thanhdd ~]# passwd sinhvien

– Tạo thêm user ở một vị trí folder ta xác định.

+ Mặc định các user được tạo ra thì Home folder ở trong /home
+ Ta tạo thêm một folder và tạo ra một user mới ở một folder ta xác định bằng câu
lệnh “useradd -d /folder_moi_tao/ten_user user”
[root@thanhdd ~]# mkdir /home-folder
=> Tạo “/home-folder”
[root@thanhdd ~]# useradd -d /home-folder/test_user user_test
[root@thanhdd ~]# cd /home-folder/
[root@thanhdd home-folder]# ls -l
total 4
drwx------ 2 user_test user_test 4096 Jun 12 01:30 test_user

– Tạo user theo cấu trúc file và folder ta xác định.

folder “/etc/skel” là cấu trúc tập file và folder của mỗi user khi được tạo ra
[root@thanhdd ~]# cd /etc/skel/
[root@thanhdd skel]# mkdir Software Backup
=> Tạo folder “Software Backup”
[root@thanhdd skel]# touch thong_tin_cong_ty.txt
=> Mỗi 1 user được tạo thêm ở trong máy tính sẽ có folder “Software”, folder “Backup”, file “thong_tin_cong_ty.txt” o trong home folder cua user
[root@thanhdd skel]# useradd test-123
[root@thanhdd skel]# cd /home/test-123/
[root@thanhdd test-123]# ls -l
total 0
drwxr-xr-x 2 test-123 test-123 6 Jun 12 01:35 Backup
drwxr-xr-x 2 test-123 test-123 6 Jun 12 01:35 Software
-rw-r--r-- 1 test-123 test-123 0 Jun 12 01:35 thong_tin_cong_ty.txt

– Xóa đi User và vẫn giữ lại Home folder

[root@thanhdd ~]# tail -n 5 /etc/passwd
postfix:x:89:89::/var/spool/postfix:/sbin/nologin
nginx:x:998:996::/home/nginx:/sbin/nologin
sinhvien:x:1000:1000::/home/sinhvien:/bin/bash
user_test:x:1001:1001::/home-folder/test_user:/bin/bash
test-123:x:1002:1002::/home/test-123:/bin/bash

[root@thanhdd ~]# userdel test-123
=> xoa di user “test-123”
=> Home Folder cua user “test-123” van con duoc giu lai
[root@thanhdd ~]# cd /home
[root@thanhdd home]# ls -l
total 0
drwx------ 2 sinhvien sinhvien 62 Jun 12 01:11 sinhvien
drwx------ 4 1002 1002 121 Jun 12 01:35 test-123

– Xóa user và xóa cả Home Folder

[root@thanhdd home]# cd /home-folder/
[root@thanhdd home-folder]# ls -l
total 4
drwx------ 2 user_test user_test 4096 Jun 12 01:30 test_user

[root@thanhdd home-folder]# userdel -r user_test
=> “userdel -r” xóa user và home folder
[root@thanhdd home-folder]# ls -l
total 0

– Giả sử user “test-123” đã bị xóa. Tạo ra một user “test_test” có toàn quyền ở
trên home folder của user đã bị xóa /home/test-123

[root@thanhdd ~]# cd /home
[root@thanhdd home]# ls -l
total 0
drwx------ 2 sinhvien sinhvien 62 Jun 12 01:11 sinhvien
drwx------ 4 1002 1002 121 Jun 12 01:35 test-123
=> user “test-123” có user-id là 1002 và group-id là 1002

[root@thanhdd home]# useradd -u 1002 test_test
=> tạo ra user “test_test” có uid là 1002
[root@thanhdd home]# ls -l
total 0
drwx------ 2 sinhvien sinhvien 62 Jun 12 01:11 sinhvien
drwx------ 4 test_test test_test 121 Jun 12 01:35 test-123
drwx------ 4 test_test test_test 121 Jun 12 01:50 test_test

– Disable connect một user ở trong máy

+ Cách 1: su dung cau lenh “passwd”
[root@thanhdd ~]# passwd -l sinhvien
Locking password for user sinhvien
passwd: Success
=> Locking user sinhvien. user này sẽ phải dùng ssh mới truy cập được server
[root@thanhdd ~]# passwd -u sinhvien
Unlocking password for user sinhvien
passwd: Success
=> Unlocking user sinhvien

+ Cách 2: chinh sua “Shell Login” cua user o trong file /etc/passwd
[root@thanhdd ~]# vi /etc/passwd
sinhvien:x:1000:1000::/home/sinhvien:/bin/bash
=> Sửa lại thành /sbin/nologin

Quản lý Group

+ Kiểm tra các group có trong máy tính
[root@thanhdd ~]# cat /etc/group

+ Kiểm tra user này đang thuộc các group nào ?
[root@thanhdd ~]# groups
root
=> user dang dang nhap thi thuoc cac group nao

+ Kiểm tra group có những user nào
[root@thanhdd ~]# groups sinhvien
sinhvien : sinhvien

+ Tạo thêm group mới bằng câu lệnh “groupadd”
[root@thanhdd ~]# groupadd hocvien
=> tạo thêm group học viên
[root@thanhdd ~]# groupadd giaovien
=> tạo thêm group giáo viên

+ Xóa đi một group: “groupdel”
[root@thanhdd ~]# groupdel test123
=> xoa di group test123

+ Tạo user và gán user này vào group do ta chỉ định
[root@thanhdd ~]# groupadd giaovien
[root@thanhdd ~]# groupadd hocvien
[root@thanhdd ~]# useradd -g giaovien -G hocvien g1
-g: Gắn thuộc group chính là group giaovien (Primary - Group Owner)
-G: ngoài group giaovien(Primary) thì còn thuộc group hocvien
[root@thanhdd ~]# id g1
uid=1003(g1) gid=1004(giaovien) groups=1004(giaovien),1003(hocvien)

Thay đổi thông tin User và Group

+ Sử dụng câu lệnh “usermod” để thay đổi thông tin user và group
[root@thanhdd ~]# id g1
uid=1003(g1) gid=1004(giaovien) groups=1004(giaovien),1003(hocvien)
=> ten user la: g1
=> group primary la giaovien
=> ngoai group giaovien thi con thuoc group hocvien

+ Đổi lại group chính và phụ
[root@thanhdd ~]# usermod -g hocvien -G giaovien g1
[root@thanhdd ~]# id g1
uid=1003(g1) gid=1003(hocvien) groups=1003(hocvien),1004(giaovien)

+ Đổi sang group chính khác
[root@thanhdd ~]# groupadd giamdoc
[root@thanhdd ~]# usermod -g giamdoc -G hocvien,giaovien g1
[root@thanhdd ~]# id g1
uid=1003(g1) gid=1005(giamdoc) groups=1005(giamdoc),1003(hocvien),1004(giaovien)