Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม Bash

เพิ่มผู้ใช้เข้ากลุ่ม Linux: คู่มือขั้นตอนพร้อมตัวอย่าง

ในบทช่วยสอนนี้ เราจะตรวจสอบการเพิ่มผู้ใช้เข้ากลุ่ม คำสั่ง Bash shell เป็นเครื่องมือที่ทรงพลังสำหรับบรรลุความต้องการเฉพาะ กลุ่มเป็นวิธีที่มีประสิทธิภาพในการแชร์และปกป้องข้อมูล สิทธิ์ไฟล์ใน Linux อนุญาตให้คุณตั้งค่าการเป็นเจ้าของโดยสิทธิ์ผู้ใช้ สิทธิ์กลุ่ม และสากล นอกจากนี้ยังเป็นวิธีที่สมบูรณ์ในการรักษาสิทธิ์ผู้ใช้ให้กับ root

ลองขุดลึกลงไป

เพิ่มผู้ใช้พร้อมกลุ่ม (wheel)

คุณได้รับแอดมินใหม่ และคุณต้องทำให้พวกเขาเป็นผู้ดูแลระบบบนไฟล์เซิร์ฟเวอร์ คำสั่ง useradd เป็นเครื่องมือที่ง่ายและทนทานสำหรับเพิ่มผู้ใช้ มันอนุญาตให้คุณเพิ่มผู้ใช้และกำหนดให้พวกเขาเข้ากลุ่ม

NAME useradd - create a new user or update default new user information SYNOPSIS useradd [options] LOGIN

นี่คือพื้นฐานจากหน้าคู่มือ useradd เริ่มจากด้านบนโดยเพิ่มผู้ใช้ใหม่ของเรา Admin Bob และ Admin Jane

ตัวเลือกทั่วไปสำหรับ useradd

-c, --comment COMMENT -G, --groups GROUP1[,GROUP2,...[,GROUPN]]] 

นี่คือฟิลด์หลักสองฟิลด์ของเรา ตอนนี้มาเพิ่ม Bob กัน

[admin@wsxdn.com ec2-user]# useradd -c "USA/CO/Denver Office" Admin.Bob

และมาเพิ่ม Jane

[admin@wsxdn.com ec2-user]# useradd -c "USA/CO/Denver Office" Admin.Jane -G wheel

ตอนนี้ เรารู้ว่า Bob เป็นแอดมินและจะต้องการสิทธิ์การบริหาร แต่บางครั้ง คุณอาจไม่รู้ หรือคุณอาจลืมไป ดังนั้นเราจะต้องเพิ่มกลุ่มให้เขาในภายหลัง Jane ถูกเพิ่มเข้า wheel แล้ว กลุ่ม wheel เป็นกลุ่มที่สร้างไว้ในระบบ Linux ส่วนใหญ่ เป็นกลุ่มค่าเริ่มต้นที่อนุญาตให้เพิ่มสิทธิ์ (privilege escalation)

[admin@wsxdn.com ec2-user]$ sudo su 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 Admin.Jane: Sorry, try again. 

พูดถึงขั้นตอนที่ลืมไป ดูเหมือนเราลืมตั้งรหัสผ่าน ฉันไม่แนะนำให้ตั้งรหัสผ่านด้วยคำสั่ง useradd แทนที่จะเป็นเช่นนั้น จำได้ต้องติดตามด้วยคำสั่ง passwd passwd จะพรอมป์ตด้วยเทอร์มินัลที่ปลอดภที่ไม่เก็บในประวัติ หรือ คุณสามารถใช้ read -sp เพื่อเก็บรหัสผ่านแบบ inline

[admin@wsxdn.com ec2-user]# passwd Admin.Jane Changing password for user Admin.Jane. New password: Retype new password: passwd: all authentication tokens updated successfully. [admin@wsxdn.com ec2-user]# su Admin.Jane [admin@wsxdn.com ec2-user]$ sudo su 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 Admin.Jane: [admin@wsxdn.com ec2-user]#

และนั่นคือทั้งหมด ผู้ใช้หนึ่งคนถูกเพิ่มพร้อมกลุ่ม wheel ไม่ต้องใช้คำสั่งกลุ่ม หากเราต้องการเพิ่ม Jane เข้ากลุ่มหลายกลุ่มตั้งแต่แรก:

[admin@wsxdn.com ec2-user]# useradd Admin.Jane -G wheel,ec2-users [admin@wsxdn.com ec2-user]# passwd Admin.Jane [admin@wsxdn.com ec2-user]# su Admin.Jane [admin@wsxdn.com ec2-user]$ groups Admin.Jane wheel ec2-users [admin@wsxdn.com ec2-user]$

และนั่นคือทั้งหมด Admin.Jane เป็นสมาชิกของกลุ่มผู้ใช้ของตัวเอง wheel สำหรับการเข้าถึง sudo และกลุ่ม ec2-users

แต่ Bob เป็นอย่างไร? เนื่องจาก Bob ถูกสร้างโดยไม่มีการเข้าถึงกลุ่ม wheel เราจะต้องใช้คำสั่งที่แตกต่างกัน

เพิ่มผู้ใช้เข้ากลุ่ม

เราเพิ่ม Bob เป็นผู้ใช้แอดมินโดยไม่มีสิทธิ์แอดมิน มาแก้ไขด้วย groupmems

ตัวเลือกทั่วไปสำหรับ groupmems

SYNOPSIS groupmems -a user_name | -d user_name | [-g group_name] | -l | -p OPTIONS The options which apply to the groupmems command are: -a, --add user_name Add an user to the group membership list. If the /etc/gshadow file exist, and the group has no entry in the /etc/gshadow file, a new entry will be created. -d, --delete user_name Delete a user from the group membership list. If the /etc/gshadow file exist, the user will be removed from the list of members and administrators of the group. If the /etc/gshadow file exist, and the group has no entry in the /etc/gshadow file, a new entry will be created. -g, --group group_name The superuser can specify which group membership list to modify. -h, --help Display help message and exit. -l, --list List the group membership list.

มาเริ่มด้วยการรับรายการสมาชิกในกลุ่ม wheel

[admin@wsxdn.com ec2-user]# getent group wheel wheel:x:10:ec2-user,samberry,Admin.Steve,Admin.Jane

เรารู้ว่า Bob ไม่อยู่ที่นั่น ดังนั้นมาเพิ่ม Bob

[admin@wsxdn.com ec2-user]# groupmems -a Admin.Bob -g wheel [admin@wsxdn.com ec2-user]# groupmems -g wheel -l ec2-user samberry Admin.Steve Admin.Jane Admin.Bob

นั่นคือทั้งหมด Admin.Bob ตอนนี้เป็นสมาชิกของ wheel และเรายืนยันโดยตรวจสอบด้วยตัวเลือก groupmems -l, –list

นี่คือพื้นฐานของการจัดการกลุ่มบน Linux สำหรับอินสแตนซ์ Linux ที่บ้าน คุณอาจเห็นกลุ่มค่าเริ่มต้นเป็น adm แทน wheel

admin@wsxdn.com:/home/samuelberry# groupmems -g adm -l syslog samuelberry

หากคุณสงสัย ตรวจสอบไฟล์กำหนดค่า /etc/sudoers ของคุณเพื่อดูว่าคุณมีอะไร

admin@wsxdn.com:/home/samuelberry# cat /etc/sudoers # Members of the admin group may gain root privileges %admin ALL=(ALL) ALL # Allow members of group sudo to execute any command %sudo ALL=(ALL:ALL) ALL [admin@wsxdn.com ec2-user]$ cat /etc/sudoers ## Allows people in group wheel to run all commands %wheelALL=(ALL)ALL ## Same thing without a password # %wheelALL=(ALL)NOPASSWD: ALL 

คุณอาจสังเกตเห็น เราเพิ่มผู้ใช้เข้าอินสแตนซ์ EC2 สัปดาห์หน้า بصفส่วนหนึ่งของการจัดการผู้ใช้/กลุ่ม ฉันจะแนะนำการเพิ่มผู้ใช้เข้าอินสแตนซ์ Linux ผ่าน EC2