{"id":131,"date":"2025-11-01T12:00:00","date_gmt":"2025-11-01T12:00:00","guid":{"rendered":"https:\/\/rezachegini.com\/?p=131"},"modified":"2026-10-30T06:44:02","modified_gmt":"2026-10-30T06:44:02","slug":"learning-linux-users-groups-and-permissions","status":"publish","type":"post","link":"https:\/\/rezachegini.com\/?p=131","title":{"rendered":"Learning Linux Users, Groups, and Permissions"},"content":{"rendered":"<p class=\"wp-block-paragraph\">This post explains how users, groups, and file permissions work in Linux. It also shows how to manage access, ownership, and security with simple commands like chmod, chown, and usermod.<\/p><h3 class=\"wp-block-heading\">1. Understanding Users in Linux<\/h3><p class=\"wp-block-paragraph\">Linux has three types of users:<\/p><ul class=\"wp-block-list\"><li>Root (superuser): Can do anything. Used for administration. Be careful when using it.<\/li><li>System users: Used by services like Nginx or MySQL. They don\u2019t log in directly.<\/li><li>Regular users: People like me who use the system normally. Each one has a home directory.<\/li><\/ul><p class=\"wp-block-paragraph\">To check users:<\/p><pre class=\"wp-block-code\"><code>whoami     # shows my username\nid         # shows UID, GID, and groups<\/code><\/pre><h3 class=\"wp-block-heading\">2. User and Group Files<\/h3><ul class=\"wp-block-list\"><li>\/etc\/passwd \u2013 list of all users<\/li><li>\/etc\/shadow \u2013 stores encrypted passwords (only root can read)<\/li><li>\/etc\/group \u2013 list of all groups on the system<\/li><\/ul><p class=\"wp-block-paragraph\">You can see them with:<\/p><pre class=\"wp-block-code\"><code>cat \/etc\/passwd\nsudo cat \/etc\/shadow\ncat \/etc\/group<\/code><\/pre><h3 class=\"wp-block-heading\">3. Creating a User<\/h3><p class=\"wp-block-paragraph\">Create a user with a home directory and shell:<\/p><pre class=\"wp-block-code\"><code>sudo useradd -m -s \/bin\/bash john\nsudo passwd john<\/code><\/pre><p class=\"wp-block-paragraph\">Check the user:<\/p><pre class=\"wp-block-code\"><code>id john\nls \/home<\/code><\/pre><h3 class=\"wp-block-heading\">4. Modifying Users<\/h3><p class=\"wp-block-paragraph\">Change shell:<\/p><pre class=\"wp-block-code\"><code>sudo usermod -s \/bin\/zsh john<\/code><\/pre><p class=\"wp-block-paragraph\">Add to a group:<\/p><pre class=\"wp-block-code\"><code>sudo usermod -aG docker john<\/code><\/pre><p class=\"wp-block-paragraph\">Remember: -aG adds the user to a group, but -G alone replaces all groups.<\/p><p class=\"wp-block-paragraph\">Lock and unlock users:<\/p><pre class=\"wp-block-code\"><code>sudo usermod -L john    # lock\nsudo usermod -U john    # unlock<\/code><\/pre><h3 class=\"wp-block-heading\">5. Deleting Users<\/h3><p class=\"wp-block-paragraph\">Remove a user and their home folder:<\/p><pre class=\"wp-block-code\"><code>sudo userdel -r john<\/code><\/pre><p class=\"wp-block-paragraph\">Be careful -&gt; -r deletes all files in their home directory.<\/p><h3 class=\"wp-block-heading\">6. Understanding Groups<\/h3><p class=\"wp-block-paragraph\">Groups help manage access for multiple users. Each user belongs to:<\/p><ul class=\"wp-block-list\"><li>One primary group<\/li><li>Zero or more secondary groups<\/li><\/ul><p class=\"wp-block-paragraph\">To check group info:<\/p><pre class=\"wp-block-code\"><code>groups john\nid john<\/code><\/pre><h3 class=\"wp-block-heading\">7. Managing Groups<\/h3><p class=\"wp-block-paragraph\">Create or remove groups:<\/p><pre class=\"wp-block-code\"><code>sudo groupadd developers\nsudo groupdel developers<\/code><\/pre><p class=\"wp-block-paragraph\">Add or remove users from groups:<\/p><pre class=\"wp-block-code\"><code>sudo usermod -aG developers john\nsudo gpasswd -d john developers<\/code><\/pre><h3 class=\"wp-block-heading\">8. Switching Users<\/h3><p class=\"wp-block-paragraph\">Change user in terminal:<\/p><pre class=\"wp-block-code\"><code>su - john<\/code><\/pre><p class=\"wp-block-paragraph\">Go back:<\/p><pre class=\"wp-block-code\"><code>exit<\/code><\/pre><h3 class=\"wp-block-heading\">9. Using sudo<\/h3><p class=\"wp-block-paragraph\">sudo allows a normal user to run commands as root.<\/p><p class=\"wp-block-paragraph\">Examples:<\/p><pre class=\"wp-block-code\"><code>sudo apt update\nsudo systemctl restart nginx<\/code><\/pre><p class=\"wp-block-paragraph\">To get a root shell:<\/p><pre class=\"wp-block-code\"><code>sudo -i<\/code><\/pre><p class=\"wp-block-paragraph\">To see what you can run:<\/p><pre class=\"wp-block-code\"><code>sudo -l<\/code><\/pre><h3 class=\"wp-block-heading\">10. File Permissions (rwx)<\/h3><p class=\"wp-block-paragraph\">Check file permissions:<\/p><pre class=\"wp-block-code\"><code>ls -l<\/code><\/pre><p class=\"wp-block-paragraph\">Example:<\/p><pre class=\"wp-block-code\"><code>-rw-r--r-- 1 reza reza 1234 Oct 19 file.txt<\/code><\/pre><p class=\"wp-block-paragraph\">Here:<\/p><ul class=\"wp-block-list\"><li>rw- = owner can read and write<\/li><li>r&#8211; = group can read<\/li><li>r&#8211; = others can read<\/li><\/ul><h3 class=\"wp-block-heading\">11. Changing Permissions with chmod<\/h3><p class=\"wp-block-paragraph\">Letters method:<\/p><pre class=\"wp-block-code\"><code>chmod u+x script.sh    # add execute for user\nchmod g-w file.txt     # remove write for group<\/code><\/pre><p class=\"wp-block-paragraph\">Numeric Permissions in Linux<\/p><p class=\"wp-block-paragraph\">We can also set file permissions using numbers instead of letters.<\/p><p class=\"wp-block-paragraph\">Each number shows what kind of access is allowed:<\/p><ul class=\"wp-block-list\"><li>7 means read, write, and execute (rwx)<\/li><li>6 means read and write (rw-)<\/li><li>5 means read and execute (r-x)<\/li><li>4 means read only (r&#8211;)<\/li><\/ul><p class=\"wp-block-paragraph\">Examples:<\/p><pre class=\"wp-block-code\"><code>chmod 755 script.sh   # rwxr-xr-x\nchmod 644 file.txt    # rw-r--r--\nchmod 600 secret.key  # rw-------<\/code><\/pre><h3 class=\"wp-block-heading\">12. Changing File Owner with chown<\/h3><p class=\"wp-block-paragraph\">Change file owner and group:<\/p><pre class=\"wp-block-code\"><code>sudo chown john:developers file.txt<\/code><\/pre><p class=\"wp-block-paragraph\">For a whole folder:<\/p><pre class=\"wp-block-code\"><code>sudo chown -R john:developers \/path\/to\/folder<\/code><\/pre><h3 class=\"wp-block-heading\">13. Common Examples<\/h3><h3 class=\"wp-block-heading\">Create a New User for Development<\/h3><pre class=\"wp-block-code\"><code>sudo useradd -m -s \/bin\/bash -c \"John Doe\" john\nsudo passwd john\nsudo usermod -aG sudo john\nsudo usermod -aG docker john<\/code><\/pre><h3 class=\"wp-block-heading\">Set Up a Project Folder<\/h3><pre class=\"wp-block-code\"><code>sudo mkdir \/var\/www\/myproject\nsudo chown john:developers \/var\/www\/myproject\nsudo chmod 775 \/var\/www\/myproject<\/code><\/pre><h3 class=\"wp-block-heading\">14. Safety Tips<\/h3><p class=\"wp-block-paragraph\">Never use dangerous commands like:<\/p><pre class=\"wp-block-code\"><code>sudo chmod 777 \/\nrm -rf \/<\/code><\/pre><p class=\"wp-block-paragraph\">Always double-check before deleting or changing ownership.<\/p><h3 class=\"wp-block-heading\">What I Learned<\/h3><ol class=\"wp-block-list\"><li>Each user and group has a unique ID (UID and GID).<\/li><li>Use sudo instead of logging in as root.<\/li><li>Understand r, w, x to manage access correctly.<\/li><li>Use chmod for permissions and chown for ownership.<\/li><li>Always use usermod -aG to add groups safely.<\/li><\/ol>","protected":false},"excerpt":{"rendered":"<p>This post explains how users, groups, and file permissions work in Linux. It also shows how to manage access, ownership, and security&hellip;<\/p>\n","protected":false},"author":2,"featured_media":133,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_rezaops_part":"3","_rezaops_linkedin_url":"https:\/\/www.linkedin.com\/pulse\/learning-linux-users-groups-permissions-reza-chegini-cawac\/","footnotes":""},"categories":[10,74],"tags":[8,76,82,38,81],"series":[75],"class_list":["post-131","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops","category-linux","tag-devops","tag-linux","tag-permissions","tag-security","tag-users-and-groups","series-linux-fundamentals"],"jetpack_featured_media_url":"https:\/\/rezachegini.com\/wp-content\/uploads\/2026\/10\/learning-linux-users-groups-permissions-cover.jpg","_links":{"self":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts\/131","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=131"}],"version-history":[{"count":1,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts\/131\/revisions"}],"predecessor-version":[{"id":132,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/posts\/131\/revisions\/132"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=\/wp\/v2\/media\/133"}],"wp:attachment":[{"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=131"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=131"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=131"},{"taxonomy":"series","embeddable":true,"href":"https:\/\/rezachegini.com\/index.php?rest_route=%2Fwp%2Fv2%2Fseries&post=131"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}