history 15 of 36
Basic Linux Administration: Users, Groups and Permissions
Notes on creating users and groups, adding users to groups, reading and changing file permissions with chmod, and what /etc/shadow and /etc/group hold.
Written
From my Linux study notes, 2014. Some commands and versions here are old, so please reuse them carefully. Anything marked "added later", and the notes at the end, explain what has changed since.
My notes on managing users, groups and file permissions. Several of my original commands were incomplete or wrong, so each one has a corrected version underneath.
Before you start
- User and group commands, and reading
/etc/shadow, need root. Run them as root or withsudo. - Practise on a test machine or virtual machine, not a live server.
Users and groups
-
To make a new user:
Terminal window useradduseraddneeds a user name:Terminal window useradd username -
Change password:
Terminal window passwd usernameSets or changes the password of an existing user. Run as root to change another user’s password; a normal user running
passwdon its own changes their own password. (The original had curly quotes around “existing username”; no quotes are needed.) -
To create a group:
Terminal window groupaddLike
useradd, it needs a name:Terminal window groupadd groupname -
Create a new user and put in the existing group:
useradd groupname usernameCorrection: this form does not work;
useraddtakes only one name and needs an option to set the group. Use:Terminal window useradd -g groupname username-gsets the user’s primary group. To add the new user to extra (supplementary) groups instead, use-G, for exampleuseradd -G groupname username. -
To add an existing user into an existing group:
usermod -G groupname usernameCorrection: the original used a typographic dash (–G), which the shell does not recognise as an option; it must be a plain hyphen, as shown above. Also,
-Gon its own replaces all the user’s supplementary groups with the one given, which can remove them from groups they need (such aswheelorsudo). Add-a(append):Terminal window usermod -aG groupname usernameThe user needs to log out and back in for the new group to take effect. Check with
id username.
File permissions
-
To check the list of permissions:
Terminal window ls -l(The original had a typographic dash instead of a hyphen.) Example output line:
-rwxr-xr-- 1 user staff 120 Jul 22 10:00 script.shAfter the first character (
-for a file,dfor a directory), there are three sets ofrwx: owner, group and others. -
To change permission we use 777 (rwx rwx rwx).
The 1st 7 is for the owner (the user who owns the file), the 2nd 7 for the group, and the 3rd 7 for others (everyone else). The original said “admin user” and “end users”; the correct terms are owner and others.
- (r) 4 read only
- (w) 2 write permission
- (x) 1 execute permission
Add the numbers for each set: 7 = 4+2+1 (rwx), 6 = 4+2 (rw-), 5 = 4+1 (r-x), 4 = r–, 0 = no permission. So
755means owner rwx, group r-x, others r-x. -
To change the permission use:
chmod 777 "group" or "user name"Correction:
chmodchanges the permissions of a file or directory, not of a user or group. The name you give is the file:Terminal window chmod 755 filenameTo change which user or group owns a file, use
chown:Terminal window chown username:groupname filenameSafety note:
777lets every user on the system read, change and run the file. Avoid it, especially on web server files. Typical safe values are644for normal files and755for directories and scripts.
Account files in /etc
-
To see the encrypted password:
Terminal window cat /etc/shadow(The original wrote this as
<etc> cat shadow, meaning runcat shadowinside/etc.) Only root can read this file. Strictly, the passwords are stored as one-way hashes, not encrypted text, so they cannot be decrypted back to the password. Never copy or share this file. -
To see groups info:
Terminal window cat /etc/groupEach line has four fields separated by
:. Correction: the first field is the group name (not the user name):- group name
- group password (usually
x, meaning it is stored elsewhere or not used) - group ID number (GID)
- members of the group, separated by commas
Example:
developers:x:1001:alice,bob
Notes for today
getent group groupnameshows one group’s line, andgroups usernamelists the groups a user belongs to.gpasswd -d username groupnameremoves a user from a group;groupdel groupnamedeletes a group.- On Debian and Ubuntu,
adduser usernameandadduser username groupnameare friendlier alternatives touseraddandusermod -aG. - Admin rights: on Red Hat-family systems, members of the
wheelgroup can usesudoby default; on Debian and Ubuntu it is thesudogroup. - Symbolic
chmodis often clearer than numbers, for examplechmod u+x script.sh(owner can execute) orchmod o-w file(others cannot write).