useradd -m -s /bin/bash <username>
useradd -r -s /sbin/nologin <service_user>
useradd -m -g <groupname> <username>
groupadd <group name> to create them:useradd -m -G wheel,docker,<groupname> <username>
passwd <username>
# View the current userwhoami# View user informationid <username># Output example: uid=1001(alice) gid=1001(alice) groups=1001(alice),10(wheel)# View all users (including system users)cat /etc/passwd# View logged-in userswhow# View recent login recordslast
# Change the primary group of a userusermod -g <groupname> <username># Add a user to a supplementary group (-a appends, otherwise the group will be replaced)usermod -aG wheel <username>usermod -aG docker <username># Change the home directory of a userusermod -d /new/home -m <username># Change the default Shell of a userusermod -s /bin/zsh <username># Change the username (login name)usermod -l <newname> <oldname># Lock a user account (prohibit login)usermod -L <username>passwd -l <username># Unlock a user accountusermod -U <username>passwd -u <username>
userdel -r also deletes the user's home directory and all data within it. This operation is irreversible. Confirm that you have backed up the data before proceeding.# Delete a user (keep the home directory)userdel <username># Delete a user and their home directory (use with caution!)userdel -r <username>
# Set a passwordpasswd <username># Set a password expiration policychage -M 90 <username> # Valid for a maximum of 90 dayschage -l <username> # View password expiration information# Force users to change their passwords at next loginpasswd --expire <username>chage -d 0 <username>
# Create a groupgroupadd <groupname># Create a group with a specified GIDgroupadd -g 2000 <groupname># Add a user to a groupusermod -aG <groupname> <username>gpasswd -a <username> <groupname># Remove a user from a groupgpasswd -d <username> <groupname># View group informationgroups <username>cat /etc/group | grep <groupname># Delete a groupgroupdel <groupname>
# Add a user to the wheel group (by default, members of the wheel group can use sudo on TencentOS)usermod -aG wheel <username># Verificationsu - <username>sudo whoami # Outputs root
visudo command to edit /etc/sudoers to avoid syntax errors that can lock out permissions.visudo
# Allow alice to run all commands (password required)alice ALL=(ALL) ALL# Allow alice to run all commands (no password required, for example, CI/CD accounts)alice ALL=(ALL) NOPASSWD: ALL# Allow members of the developers group to run all commands%developers ALL=(ALL) ALL# Allow only specific commands to be run (principle of least privilege)bob ALL=(ALL) /usr/bin/systemctl restart nginx, /usr/bin/systemctl status nginx# Allow alice to run commands as the mysql useralice ALL=(mysql) /usr/bin/mysqldump
/etc/sudoers.d/ directory (recommended to avoid modifying the main configuration file):# Create sudo configuration for aliceecho "alice ALL=(ALL) NOPASSWD: /usr/bin/systemctl restart nginx" > /etc/sudoers.d/alicechmod 440 /etc/sudoers.d/alice
# Change file permissionschmod 755 /opt/myapp/start.sh # rwxr-xr-xchmod 644 /etc/myapp/config.yaml # rw-r--r--# Recursively change directory permissionschmod -R 755 /opt/myapp/# Change permissions using symbolic notationchmod u+x script.sh # Add execute permission for the ownerchmod go-w file.txt # Remove write permission for group and otherschmod a+r file.txt # Add read permission for everyone# Change owner and groupchown <username>:<groupname> /opt/project/chown -R <username>:<groupname> /opt/project/ # Recursively
# SUID (run with the file owner's permissions)chmod u+s /usr/bin/passwd # Example: The passwd command requires SUID to modify /etc/shadow# SGID (run with the file's group permissions; for directories = new files inherit the directory's group)chmod g+s /shared/project/# Sticky Bit (for directories: only the file owner can delete their own files)chmod +t /tmpls -la /tmp # Displays as drwxrwxrwt
# Install the acl tooldnf install -y acl# Set permissions for a specific usersetfacl -m u:bob:rw /shared/file.txt# Set permissions for a specific groupsetfacl -m g:developers:rx /opt/project/# View ACLgetfacl /shared/file.txt# Delete ACLsetfacl -x u:bob /shared/file.txt# Set ACL recursivelysetfacl -R -m u:bob:rX /opt/project/
# Verify user creationid <username>cat /etc/passwd | grep <username># Verify group membersgroups <username># Verify sudo permissionssudo -l -U <username> # List the sudo permissions of a user# Verify file permissionsls -la /opt/myapp/getfacl /shared/file.txt # View ACL
newgrp command to refresh group information in the current session:# Takes effect in the current session (no re-login required)newgrp <groupname>
sudo: user is not in the sudoers file.wheel group.# Add a user to the wheel group with root privilegesusermod -aG wheel <username># Or add the configuration in /etc/sudoers.d/
フィードバック