$npx -y skills add chaterm/terminal-skills --skill user-permissionsLinux user and permission management
| 1 | # User and Permission Management |
| 2 | |
| 3 | ## Overview |
| 4 | Linux user management, group management, sudo configuration, ACL permissions and other skills. |
| 5 | |
| 6 | ## User Management |
| 7 | |
| 8 | ### View Users |
| 9 | ```bash |
| 10 | # Current user |
| 11 | whoami |
| 12 | id |
| 13 | |
| 14 | # User information |
| 15 | id username |
| 16 | finger username |
| 17 | |
| 18 | # All users |
| 19 | cat /etc/passwd |
| 20 | getent passwd |
| 21 | |
| 22 | # Logged in users |
| 23 | who |
| 24 | w |
| 25 | last # Login history |
| 26 | ``` |
| 27 | |
| 28 | ### User Operations |
| 29 | ```bash |
| 30 | # Create user |
| 31 | useradd username |
| 32 | useradd -m -s /bin/bash username # Create home directory, specify shell |
| 33 | useradd -G group1,group2 username # Specify supplementary groups |
| 34 | |
| 35 | # Modify user |
| 36 | usermod -aG groupname username # Add to group |
| 37 | usermod -s /bin/zsh username # Change shell |
| 38 | usermod -L username # Lock user |
| 39 | usermod -U username # Unlock user |
| 40 | |
| 41 | # Delete user |
| 42 | userdel username |
| 43 | userdel -r username # Also delete home directory |
| 44 | |
| 45 | # Change password |
| 46 | passwd username |
| 47 | passwd -l username # Lock password |
| 48 | passwd -u username # Unlock password |
| 49 | chage -l username # View password policy |
| 50 | ``` |
| 51 | |
| 52 | ## Group Management |
| 53 | |
| 54 | ### View Groups |
| 55 | ```bash |
| 56 | # User's groups |
| 57 | groups username |
| 58 | id -Gn username |
| 59 | |
| 60 | # All groups |
| 61 | cat /etc/group |
| 62 | getent group |
| 63 | |
| 64 | # Group members |
| 65 | getent group groupname |
| 66 | ``` |
| 67 | |
| 68 | ### Group Operations |
| 69 | ```bash |
| 70 | # Create group |
| 71 | groupadd groupname |
| 72 | groupadd -g 1001 groupname # Specify GID |
| 73 | |
| 74 | # Modify group |
| 75 | groupmod -n newname oldname # Rename |
| 76 | |
| 77 | # Delete group |
| 78 | groupdel groupname |
| 79 | |
| 80 | # Manage group members |
| 81 | gpasswd -a username groupname # Add user |
| 82 | gpasswd -d username groupname # Remove user |
| 83 | gpasswd -M user1,user2 groupname # Set member list |
| 84 | ``` |
| 85 | |
| 86 | ## sudo Configuration |
| 87 | |
| 88 | ### Basic Usage |
| 89 | ```bash |
| 90 | # Execute as root |
| 91 | sudo command |
| 92 | sudo -i # Switch to root shell |
| 93 | sudo -u username command # Execute as another user |
| 94 | |
| 95 | # View permissions |
| 96 | sudo -l |
| 97 | ``` |
| 98 | |
| 99 | ### sudoers Configuration |
| 100 | ```bash |
| 101 | # Edit sudoers (recommended method) |
| 102 | visudo |
| 103 | |
| 104 | # Or edit files under /etc/sudoers.d/ |
| 105 | visudo -f /etc/sudoers.d/username |
| 106 | ``` |
| 107 | |
| 108 | ### Common Configuration Examples |
| 109 | ```bash |
| 110 | # /etc/sudoers.d/username |
| 111 | |
| 112 | # Full privileges |
| 113 | username ALL=(ALL:ALL) ALL |
| 114 | |
| 115 | # No password required |
| 116 | username ALL=(ALL) NOPASSWD: ALL |
| 117 | |
| 118 | # Specific commands |
| 119 | username ALL=(ALL) /usr/bin/systemctl restart nginx |
| 120 | |
| 121 | # Specific commands without password |
| 122 | username ALL=(ALL) NOPASSWD: /usr/bin/docker |
| 123 | |
| 124 | # Group privileges |
| 125 | %groupname ALL=(ALL) ALL |
| 126 | ``` |
| 127 | |
| 128 | ## ACL Permissions |
| 129 | |
| 130 | ### View ACL |
| 131 | ```bash |
| 132 | getfacl file |
| 133 | getfacl -R dir # Recursive view |
| 134 | ``` |
| 135 | |
| 136 | ### Set ACL |
| 137 | ```bash |
| 138 | # Set user permissions |
| 139 | setfacl -m u:username:rwx file |
| 140 | setfacl -m u:username:rx dir |
| 141 | |
| 142 | # Set group permissions |
| 143 | setfacl -m g:groupname:rx file |
| 144 | |
| 145 | # Set default ACL (new files inherit) |
| 146 | setfacl -d -m u:username:rwx dir |
| 147 | |
| 148 | # Recursive set |
| 149 | setfacl -R -m u:username:rx dir |
| 150 | |
| 151 | # Remove ACL |
| 152 | setfacl -x u:username file # Remove specific |
| 153 | setfacl -b file # Remove all |
| 154 | ``` |
| 155 | |
| 156 | ## Special Permissions |
| 157 | |
| 158 | ### SUID/SGID/Sticky |
| 159 | ```bash |
| 160 | # SUID (4) - Execute as file owner |
| 161 | chmod u+s file |
| 162 | chmod 4755 file |
| 163 | |
| 164 | # SGID (2) - Execute as file group/directory inherits group |
| 165 | chmod g+s file |
| 166 | chmod 2755 dir |
| 167 | |
| 168 | # Sticky (1) - Only owner can delete |
| 169 | chmod +t dir |
| 170 | chmod 1777 dir |
| 171 | |
| 172 | # View |
| 173 | ls -la |
| 174 | # -rwsr-xr-x SUID |
| 175 | # -rwxr-sr-x SGID |
| 176 | # drwxrwxrwt Sticky |
| 177 | ``` |
| 178 | |
| 179 | ## Common Scenarios |
| 180 | |
| 181 | ### Scenario 1: Create Developer User |
| 182 | ```bash |
| 183 | # Create user and group |
| 184 | groupadd developers |
| 185 | useradd -m -s /bin/bash -G developers devuser |
| 186 | |
| 187 | # Set password |
| 188 | passwd devuser |
| 189 | |
| 190 | # Configure sudo |
| 191 | echo "devuser ALL=(ALL) NOPASSWD: /usr/bin/docker, /usr/bin/systemctl" > /etc/sudoers.d/devuser |
| 192 | chmod 440 /etc/sudoers.d/devuser |
| 193 | ``` |
| 194 | |
| 195 | ### Scenario 2: Shared Directory Permissions |
| 196 | ```bash |
| 197 | # Create shared directory |
| 198 | mkdir /shared |
| 199 | groupadd shared |
| 200 | chown root:shared /shared |
| 201 | chmod 2775 /shared # SGID ensures new files inherit group |
| 202 | |
| 203 | # Add users to group |
| 204 | usermod -aG shared user1 |
| 205 | usermod -aG shared user2 |
| 206 | ``` |
| 207 | |
| 208 | ### Scenario 3: Restrict User to Specific Commands |
| 209 | ```bash |
| 210 | # /etc/sudoers.d/limited-user |
| 211 | limited ALL=(ALL) NOPASSWD: /usr/bin/systemctl status *, /usr/bin/journalctl |
| 212 | ``` |
| 213 | |
| 214 | ## Troubleshooting |
| 215 | |
| 216 | | Problem | Solution | |
| 217 | |---------|----------| |
| 218 | | sudo permission denied | Check `/etc/sudoers.d/` configuration | |
| 219 | | User cannot login | Check shell, password lock status | |
| 220 | | Group permissions not working | Re-login or `newgrp groupname` | |
| 221 | | ACL not working | Check if filesystem supports ACL | |