Notes: Managing Users, Permissions, LDAP
Check your understanding of user and group management, su and sudo, file permissions, ACLs and LDAP directories.
-
/etc/passwd -
/etc/shadow -
/etc/group -
/etc/login.defs
Explanation: Each line of
/etc/passwddescribes one account: name, password placeholder, UID, primary GID, comment, home directory and shell. Password hashes live in/etc/shadow.
-
/etc/passwd -
/etc/shadow -
/etc/sudoers -
/etc/securetty
Explanation:
/etc/shadowholds salted one-way hashes plus aging fields and is not world-readable: Ubuntu lets only root and theshadowgroup read it, and Fedora gives it mode000. World-readable/etc/passwdonly holds anxplaceholder.
deploy:x:1001:2000:Deploy User:/srv/deploy:/usr/sbin/nologin
- UID 2000, primary GID 1001, home
/srv/deploy, and the password isx - UID 1001, member of 2000 groups, home
/usr/sbin/nologin - UID 1001, primary GID 2000, home
/srv/deploy, and no interactive login shell - UID 1001, primary GID 2000, and the password stored in plain text in field 2
Explanation: The fields are name, password placeholder (
xmeans "see/etc/shadow"), UID, primary GID, comment, home and shell. A shell ofnologinis typical for service accounts.
- The account has no password and anyone can log in
- The password must be changed at next login
- The password is stored unencrypted
- Password authentication is disabled for the account
Explanation: A hash prefixed with
!(or a field of*) cannot match any password, so password logins fail.passwd -llocks accounts this way, but other methods such as SSH keys may still work.
Q. Which command creates the user adam with a home directory populated from /etc/skel, whatever the distribution's default?
-
useradd adam -
useradd -m adam -
useradd -M adam -
usermod -m adam
Explanation:
-mcreates the home directory and copies the skeleton files into it. Without it, the result depends onCREATE_HOMEin/etc/login.defs: Fedora creates a home, Ubuntu does not.-Mexplicitly skips it, andusermodchanges existing accounts.
- They are copied from the skeleton directory
/etc/skel - They are generated from the settings in
/etc/login.defs - They are copied from root's home directory
- Bash creates them the first time bob logs in
Explanation:
useradd -mcopies everything in/etc/skel(or the directory given with-k) into the new home and gives it to the user. Changing/etc/skelaffects only accounts created later.
-
addusercan only create system accounts, whileuseraddcreates regular users -
useraddalways prompts for the full name and password, whileaddusernever does -
adduseris an interactive front end on Debian/Ubuntu but just a link touseraddon Fedora -
useraddonly works on Red Hat systems, andadduseronly on Debian
Explanation: Debian's
adduserasks for a password and details and creates the home for you; Fedora's/usr/sbin/adduseris a symlink touseradd. Becauseadduservaries (and minimal images may lack it),useraddis the safer choice in portable scripts.
Q. Which command adds the existing user bob to the supplementary group docker without removing his other groups?
-
usermod -G docker bob -
groupadd docker bob -
useradd -G docker bob -
usermod -aG docker bob
Explanation:
-aappends to the supplementary group list.useraddwould try to create a new user, andgroupaddcreates groups rather than adding members.
Q. You run usermod -aG docker bob while bob is logged in. In his existing shell, id -Gn still does not list docker. Why?
- Group membership is read at login, so his running shell keeps its old group list
-
usermodchanges only take effect after the system is rebooted -
-aGwrites the change to/etc/gshadowonly, not to/etc/group - Bob must run
passwdbefore a new group membership becomes active
Explanation: A process's supplementary groups are fixed when the login session starts.
/etc/groupis updated immediately, so a new login (ornewgrp docker/su - bob) picks it up; no reboot is needed.
- Bob is added to
dockerand keeps all of his other supplementary groups - Bob's primary group changes to
docker, and his supplementary groups stay - The command fails, because
-ais mandatory whenever-Gis used - Bob's supplementary groups are replaced, so he leaves every group except
docker
Explanation: Without
-a,-Gsets the complete list of supplementary groups. Forgetting-ais a classic way to accidentally remove someone fromsudoorwheel.
-
groupdel admins adam -
usermod -aG admins adam -
gpasswd -d adam admins -
userdel adam admins
Explanation:
gpasswd -ddeletes a single member from a group.groupdelremoves the whole group anduserdeldeletes the user account.
- The number of members
- The group ID (GID)
- Adam's UID
- The group's password
Explanation: The fields are group name, password placeholder, GID and a comma-separated member list.
getent group adminsprints the same format.
- Adam's password expiry information
- Adam's last login time
- Adam's UID, primary GID and group memberships
- Adam's home directory and shell
Explanation:
idprints something likeuid=1000(adam) gid=1000(adam) groups=1000(adam),27(sudo). Expiry details come fromchage -land logins fromlast.
-
useradd -s /bin/zsh carol -
chmod -s /bin/zsh carol -
export SHELL=/bin/zsh carol -
chsh -s /bin/zsh carol
Explanation:
chsh -supdates the shell field in/etc/passwd(as doesusermod -s).useraddcreates new accounts, and exportingSHELLonly affects the current session.
Q. An admin runs useradd -m -p 'Secret123' carol. What happens when carol tries to log in with the password Secret123?
- Login fails, because
-pexpects an already hashed password and stores the text as-is - Login works, because
useraddhashes the value before storing it - Login works only after carol first logs in with an empty password
- Login fails, because
-pis accepted only together with-r
Explanation:
-pwrites its argument straight into the hash field of/etc/shadow, andSecret123is not a valid hash, so no password can match it. Set passwords withpasswd carolorchpasswdinstead.
-
passwd -d dave -
passwd -l dave -
passwd -e dave -
passwd -S dave
Explanation:
-lprefixes the hash with!. It does not disable the account entirely, so SSH key logins may still work.-dremoves the password,-eforces a change at next login and-Sshows status.
-
chage -l alice -
chage -d 0 alice -
passwd -l alice -
usermod -L alice
Explanation:
chage -d 0sets the last-change date to day 0, so the password counts as expired and must be changed at login;passwd -e alicedoes the same.chage -lonly displays aging data, whilepasswd -landusermod -Llock the password.
-
userdel eve -
usermod -L eve -
rm -r /home/eve -
userdel -r eve
Explanation:
-rremoves the home directory and mail spool along with the account. Plainuserdelleaves the files behind, and deleting the directory alone leaves the account in place.
- 0
- 1
- 1000
- 65534
Explanation: The kernel grants superuser privileges to UID 0, whatever the account is called. Regular users usually start at 1000, and 65534 is commonly
nobody.
Q. You change adam's UID with usermod -u 2001 adam; it used to be 1001. What does ls -l show as the owner of a file he owned in /tmp?
-
adam, becauseusermodupdates every file on the system -
root, because orphaned files are handed to root -
1001, becauseusermodonly updates files in his home directory and mail spool -
nobody, because unknown UIDs are shown as 65534
Explanation: Files store a numeric UID.
usermod -ufixes ownership only inside the home directory and mail spool, so elsewhere the file keeps UID 1001, which no longer has a name. Fix it withfind / -xdev -uid 1001 -exec chown -h adam {} +.
-
su - adamswitches user without asking for adam's password -
su adamswitches to root first and only then to adam -
su - adamruns a single command as adam and then exits -
su - adamstarts a login shell with adam's environment and startup files
Explanation: The
-(or-l) option simulates a full login: adam's environment variables are loaded and you start in his home directory. Plainsu adamkeeps much of the caller's environment.
-
su adam whoami -
su adam -c "whoami" -
su -l whoami adam -
sudo -l adam whoami
Explanation:
su -cpasses a single command to the target user's shell, and the output appears in your terminal. Here it printsadam.
Q. To give user adam administrative rights through the default sudoers setup, which group do you add him to on Debian/Ubuntu versus RHEL/Fedora?
-
adminon both -
rooton both -
sudoon Debian/Ubuntu andwheelon RHEL/Fedora -
wheelon Debian/Ubuntu andsudoon RHEL/Fedora
Explanation: Debian-based systems grant
%sudofull rights, while Red Hat-based systems use%wheel. For example,usermod -aG wheel adamon RHEL.
-
visudolocks the file and checks its syntax before installing the change -
visudoencrypts the file so that other users cannot read the rules - Regular editors cannot open files in
/etcwithout special flags -
visudoautomatically grants the current user full sudo rights
Explanation: A syntax error in sudoers can stop
sudofrom working at all, locking you out of administration.visudovalidates the file and refuses to install a broken version.
- Grants the user named
wheelfull sudo rights - Lets all members of the
wheelgroup run any command as any user on any host - Lets all users run commands as members of the
wheelgroup - Denies the
wheelgroup any sudo rights
Explanation:
%marks a group. The fields are hosts (ALL), the users you may become ((ALL)) and the allowed commands (ALL). By default users still have to enter their own password.
- Adam can run
/sbin/rebootthrough sudo without being asked for a password - Adam can run any command without a password
- Adam can reboot only after entering the root password
- Every user can reboot without a password
Explanation: The rule names one user and one command, and the
NOPASSWD:tag skips authentication for it. Other commands still follow the remaining rules.
Q. Which sudoers line lets user alice run only /usr/bin/systemctl as root, still asking for her password?
-
alice ALL=(ALL) ALL -
alice ALL=(root) NOPASSWD: ALL -
alice ALL=(root) /usr/bin/systemctl -
alice ALL=(root) !/usr/bin/systemctl
Explanation: Listing one command restricts alice to it, and without
NOPASSWD:she must authenticate.ALLwould allow every command, and!would forbidsystemctlinstead.
- All processes currently running as root on this host
- The sudo log of commands the user has already run
- The members of the
sudogroup and their privileges - Which commands the invoking user may run with sudo on this host
Explanation:
sudo -l(list) prints the user's allowed and forbidden commands according to the sudoers policy. It is the quickest way to check what a new rule grants.
-
-E -
-i -
-l -
-u
Explanation:
sudo -iruns the target user's shell as a login shell, much likesu -.-Epreserves your environment,-llists privileges and-uonly chooses the target user.
Q. Which command runs make install as root while asking sudo to keep your current environment variables?
-
sudo -i make install -
sudo -s make install -
sudo -k make install -
sudo -E make install
Explanation:
-E(--preserve-env) requests that your environment be kept, if the security policy allows it.-iuses a login environment and-kdiscards cached credentials.
Q. Which sudoers Defaults setting makes commands run with a minimal, clean environment instead of the caller's variables?
-
env_keep -
requiretty -
env_reset -
secure_path
Explanation:
env_reset(on by default) discards most of the caller's environment.env_keeplists exceptions that are kept, andsecure_pathsets thePATHused for commands.
- The directory can be entered and traversed to reach entries inside it
- Programs stored in the directory can be run by any user
- New files can be created in the directory by group members
- The names of the entries in the directory can be listed
Explanation: For directories,
rlists names,wlets you create, delete and rename entries (together withx), andxallows access through the directory. Withoutx,cdand opening files inside fail.
Q. Bob has write and execute permission on /shared, which has no sticky bit. A file there is owned by alice with mode 444. Can bob delete it?
- No, because the file's mode
444makes it read-only for everyone - Yes, because deletion needs write and execute permission on the directory, not the file
- No, because only the owner of a file can ever delete it
- Yes, but only if bob is also a member of alice's primary group
Explanation: Removing a file removes a directory entry, so the directory's permissions decide.
rmmay ask about a write-protected file, but it succeeds. The sticky bit is what prevents this in shared directories.
-
rwxrwxr-- -
rw-r-xr-- -
rwxr-xr-- -
rwxr--r-x
Explanation: Each digit is the sum of read 4, write 2 and execute 1: 7 =
rwxfor the owner, 5 =r-xfor the group and 4 =r--for others.
-
664 -
640 -
755 -
644
Explanation:
u=rwgives the owner 6 andgo=rgives group and others 4 each. The=operator sets the permissions exactly rather than adding to them.
- It removes read, write and execute for others, leaving owner and group unchanged
- It grants read, write and execute permission to others
- It removes all permissions for the owner, leaving group and others
- It makes the file immutable, so nobody else can change it
Explanation:
oselects others and-removes the listed bits. Owner (u) and group (g) permissions are not touched.
-
rw-r--r-- -
rw-rw-r-- -
rwxr-x--- -
rw-r-----
Explanation: The umask clears bits:
666with group write and all of others' bits masked gives640. Directories requested as777would become750.
Q. With umask 033, which modes do a new file (requested as 666) and a new directory (requested as 777) get?
- File
644, directory744 - File
633, directory744 - File
644, directory755 - File
633, directory733
Explanation: The umask clears bits (
mode & ~umask); it is not subtraction.033removes write and execute for group and others, so666becomes644and777becomes744.
Q. Why can ordinary users change their own password even though /etc/shadow is writable only by root?
-
/etc/shadowhas the sticky bit set - Users are temporarily added to the
rootgroup -
/usr/bin/passwdis setuid root, so it runs with the file owner's privileges - Password changes are written to the user's home directory
Explanation: The setuid bit makes an executable run with its owner's user ID. Because setuid programs run as root, they must be audited carefully.
Q. On Linux, you run chmod 4755 on a root-owned shell script /usr/local/bin/backup.sh that runs id -u. What does it print when alice runs it?
-
0, because the setuid bit makes the script run as root - Alice's own UID, because Linux ignores setuid on interpreted scripts
-
0, but only if the script starts with#!/bin/bash - Nothing, because the kernel refuses to run setuid scripts
Explanation: The kernel honours setuid and setgid only on binary executables; for a
#!script it starts the interpreter with the caller's IDs. Grant the access with a narrow sudo rule instead.
- The setuid bit is set, but the owner has no execute permission
- The file is a socket, shown by
Sin the owner field - The sticky bit is set, and others have no execute permission
- The file is a symbolic link to a setuid program
Explanation: A lowercase
smeans setuid plus execute, whileSmeans the special bit is present without the underlyingx, which usually signals a mistake.
- Sticky bit (
chmod +t) - Setuid (
chmod u+s) - Setgid (
chmod g+s) - Group write (
chmod g+w)
Explanation: With setgid on a directory, new files get the directory's group and new subdirectories inherit setgid. It does not grant group write; use a suitable umask or a default ACL for that.
Q. /srv/proj has mode drwxrwsr-x. After chmod 0775 /srv/proj with GNU coreutils, what does ls -ld /srv/proj show?
-
drwxrwxr-x, because the leading0clears all special bits -
drwxrwsr-x, because GNUchmodkeeps a directory's setgid bit here -
drwxrwSr-x, because the group execute bit is removed -
drwxr-xr-x, because0775resets the group bits
Explanation: For directories, GNU
chmodpreserves the setuid and setgid bits when a numeric mode does not mention them explicitly. Clear the bit withchmod g-s /srv/projor a five-digit mode such aschmod 00775 /srv/proj.
- It makes every file created in the directory executable
- It prevents anyone, including root, from deleting files
- It makes the system empty the directory at every reboot
- Only a file's owner, the directory's owner or root can delete or rename the file
Explanation: Without the sticky bit, anyone with write permission on the directory could remove other users' files.
ls -ld /tmpshows it as atin the last position (drwxrwxrwt).
-
find / -perm /2000 -type f -
find / -perm /1000 -type d -
ls -lR / | grep rwx -
find / -perm /4000 -type f 2>/dev/null
Explanation: In octal, 4000 is setuid, 2000 is setgid and 1000 is the sticky bit.
-perm /4000matches files with that bit set;2>/dev/nullhides permission errors.
- Setgid regular files, plus every directory on the system
- Only setgid regular files and setgid directories
- Only setgid directories
- Nothing, because
-omust be written inside parentheses
Explanation: The implicit AND binds tighter than
-o, so this reads(-perm /2000 -type f) -o -type d. Group the type tests to get setgid files and directories:find / \( -type f -o -type d \) -perm /2000.
Q. Which command recursively sets mode 755 on every directory under project/ without changing regular files?
-
chmod -R 755 project/ -
find project/ -type f -exec chmod 755 {} + -
find project/ -type d -exec chmod 755 {} + -
chmod 755 project/*
Explanation:
find -type dselects only directories and passes them tochmod.chmod -Rwould also make every regular file executable, and the glob only reaches the top level.
-
chmod alice:devs script.sh -
chown alice:devs script.sh -
chgrp alice:devs script.sh -
usermod -g devs alice script.sh
Explanation:
chown user:groupsets both at once;chgrpchanges only the group.chmodchanges permission bits, not ownership.
Q. Which command grants user bob read and write access to report.txt through an ACL, keeping existing ACL entries?
-
chmod u:bob:rw report.txt -
setfacl -x u:bob:rw report.txt -
getfacl -m u:bob:rw report.txt -
setfacl -m u:bob:rw report.txt
Explanation:
setfacl -m(modify) adds or updates one entry and leaves others intact.-xremoves entries, andgetfaclonly displays ACLs.
-
setfacl -x g:sales budget.xls -
setfacl -b budget.xls -
setfacl -m g:sales:--- budget.xls -
chgrp -sales budget.xls
Explanation:
-xdeletes the named entry; no permissions are given in the spec.-bwould remove every extended entry, and-m g:sales:---keeps an entry that denies access.
Q. Which command removes all extended ACL entries from data/, keeping only the standard owner, group and other permissions?
-
setfacl -x data/ -
setfacl -b data/ -
chmod a-rwx data/ -
getfacl --remove-all data/
Explanation:
-b(--remove-all) strips named user, named group and mask entries, plus any default ACL, but retains the base permissions.-xneeds an entry to remove, andchmod a-rwxwould clear the base permissions instead.
Q. Which command makes every file later created in project/ grant group devs read and write access by default?
-
setfacl -m g:devs:rw project/ -
chmod g+s project/ -
setfacl -d -m g:devs:rw project/ -
setfacl -b project/
Explanation:
-dsets a default ACL, which new items in the directory inherit. Without-d, the entry applies only to the directory itself, and setgid changes group ownership, not permissions.
Q. Directory shared/ has the default ACL entry default:group:devs:rw-. A program running with umask 077 creates shared/new, requesting mode 666. What happens?
- The umask applies as usual, so
newisrw-------anddevshas no access - Default ACLs apply only to new subdirectories, not to files
-
newcopies the directory's own access ACL instead - The umask is not applied;
newinherits the default ACL, so groupdevscan read and write it
Explanation: When the parent has a default ACL, the new item's ACL comes from it, limited only by the mode the program requested; the umask is ignored. A program asking for
600would still leavedevswithout effective access.
user:joe:rwx #effective:r--
mask::r--
-
rwx, because named user entries ignore the mask - No access, because the mask overrides all entries
- Read only, because the mask limits named users and groups
- Read and execute, the default for named users
Explanation: The mask is the upper bound for named users, named groups and the owning group. Joe's entry says
rwx, but only the bits also present in the mask,r--, take effect.
- Only the owning group's own ACL entry
- The ACL mask, which can reduce named users' and groups' effective access
- Nothing, because
chmodis ignored on files with ACLs - It deletes all named ACL entries
Explanation: With an extended ACL, the group bits shown by
ls -lrepresent the mask. Changing them withchmodtherefore changes the mask and limits every named entry.
- The organizational unit (container) that holds the entry
- The user's login name
- The domain the directory belongs to
- The user's group membership
Explanation: A DN is read from the most specific part to the most general, like a path:
uid=jdoeis the entry,ou=usersits container, anddc=example,dc=comthe domain.
-
cn=example,cn=com -
dc=example,dc=com -
ou=example,ou=com -
dn=example.com
Explanation: Each part of the DNS name becomes a domain component (
dc). Searches and new entries are placed under this base, such asou=users,dc=example,dc=com.
- 389 and 636
- 53 and 853
- 88 and 464
- 443 and 8443
Explanation: LDAP listens on 389, where StartTLS can upgrade the connection, and LDAPS uses TLS from the start on 636. Ports 53/853 are DNS, 88/464 are Kerberos and 443/8443 are HTTPS.
Q. Which command searches the subtree dc=example,dc=com for the user jdoe using simple authentication?
-
ldapadd -x -b "dc=example,dc=com" "(uid=jdoe)" -
ldapsearch -b "(uid=jdoe)" "dc=example,dc=com" -
ldapsearch -x -b "dc=example,dc=com" "(uid=jdoe)" -
ldapwhoami -x "dc=example,dc=com" "(uid=jdoe)"
Explanation:
-xselects simple authentication,-bsets the search base, and the filter comes after it.ldapaddcreates entries andldapwhoamionly tests a bind.
-
(|(objectClass=person)(mail=*)) -
(objectClass=person,mail=*) -
(!(objectClass=person)(mail=*)) -
(&(objectClass=person)(mail=*))
Explanation: LDAP filters use prefix notation:
&is AND,|is OR and!is NOT.(mail=*)is a presence test that matches any entry with a
Q. Which file format is used to add, modify or export LDAP entries in bulk with tools such as ldapadd and ldapmodify?
- JSON
- CSV
- YAML
- LDIF
Explanation: LDIF (LDAP Data Interchange Format) is plain text listing a
dn:line followed by attributes. For example,ldapadd -x -D "cn=admin,dc=example,dc=com" -W -f user.ldifloads it.
Q. A user entry in LDAP has only the inetOrgPerson object class, and getent passwd jdoe returns nothing. What is missing for Linux login?
- The
posixAccountclass withuidNumber,gidNumber,homeDirectoryandloginShell - A
mailattribute holding the user's email address - An
ou=groupsentry listing the user's groups - The
organizationobject class on the user's entry
Explanation:
inetOrgPersondescribes a person, but NSS needs POSIX attributes to build a Unix account. AddingposixAccount(and oftenshadowAccount) with those attributes letsgetent passwd jdoeresolve.
Q. On a host whose users come from LDAP through SSSD, grep jdoe /etc/passwd finds nothing. Which command shows whether the system can resolve jdoe as an account?
-
grep jdoe /etc/shadow -
getent passwd jdoe -
ls -d /home/jdoe -
awk -F: '$1 == "jdoe"' /etc/passwd
Explanation:
getentasks every source listed in/etc/nsswitch.conf(such asfilesandsss), while/etc/passwdand/etc/shadowhold only local accounts. A directory user's home may not exist until the first login.
Q. ldapsearch -x -b "dc=wrong,dc=com" "(uid=jdoe)" ends with result: 32 No such object. What does that tell you?
- The client could not contact the LDAP server
- The bind password was wrong
- The server answered, but the search base DN does not exist
- The search ran but no entry matched the filter
Explanation: Code 32 means the base or target DN is missing. An unreachable server gives
Can't contact LDAP server (-1), a wrong passwordInvalid credentials (49), and a search with no matchesresult: 0 Successwith no entries.