Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม BASH

วิธีเพิ่มผู้ใช้ในกลุ่มใน Linux (พร้อมตัวอย่าง)

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

มาดูรายละเอียดกันเถอะ

การเพิ่มผู้ใช้ด้วยกลุ่ม (วงล้อ)

คุณได้ว่าจ้างผู้ดูแลระบบคนใหม่แล้ว และคุณจำเป็นต้องตั้งให้เป็นผู้ดูแลระบบบนเซิร์ฟเวอร์ไฟล์ คำสั่ง useradd เป็นเครื่องมือที่ง่ายและมีประสิทธิภาพสำหรับการเพิ่มผู้ใช้ ช่วยให้คุณสามารถเพิ่มผู้ใช้และกำหนดให้กับกลุ่มได้
NAME
       useradd - create a new user or update default new user information

SYNOPSIS
       useradd [options] LOGIN

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

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

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

นั่นคือสองฟิลด์หลักของเรา มาเพิ่มบ๊อบกันเถอะ

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

และเพิ่มเจนกันเถอะ

[admin@wsxdn.com ec2-user]# useradd -c "USA/CO/Denver Office" Admin.Jane -G wheel
ตอนนี้ เราทราบแล้วว่า Bob เป็นผู้ดูแลระบบและจะต้องมีสิทธิ์ของผู้ดูแลระบบ แต่บางครั้งคุณอาจไม่รู้ หรือคุณอาจลืมไป ดังนั้นเราจะต้องเพิ่มกลุ่มให้เขาในภายหลัง เจนถูกเพิ่มในวงล้อแล้ว กลุ่มล้อเป็นกลุ่มที่รวมเข้ากับระบบลีนุกซ์ส่วนใหญ่ เป็นกลุ่มเริ่มต้นที่อนุญาตให้ยกระดับสิทธิ์ได้
[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 เพื่อจัดเก็บรหัสผ่านแบบอินไลน์

[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]#

และเราไปที่นั่น ผู้ใช้รายหนึ่งเพิ่มด้วยกลุ่มล้อ ไม่จำเป็นต้องใช้คำสั่งกลุ่ม หากเราต้องการเพิ่มเจนในหลายกลุ่มในตอนแรก:

[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 อยู่ในกลุ่มผู้ใช้ของเธอ วงล้อสำหรับการเข้าถึง sudo และกลุ่มผู้ใช้ ec2

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

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

เราเพิ่ม 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.

มาเริ่มกันที่รายชื่อสมาชิกในกลุ่มล้อกันเลย

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

เรารู้ว่าบ๊อบไม่อยู่ที่นั่น มาเพิ่มบ๊อบกันเถอะ

[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 เป็นสมาชิกวงล้อแล้ว และเราตรวจสอบโดยตรวจสอบกับ groupmems -l, –list, ตัวเลือก

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

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