DevOps / Sys Admin Q & A #24 : Linux User Account Management
Let's look at the /etc/passwd:
# cat /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin ... syslog:x:104:108::/home/syslog:/bin/false k:x:1000:1000:K Hong,,,:/home/k:/bin/bash ... kafka:x:1002:1002::/home/kafka:
The /etc/passwd file is a colon-separated file that contains the following information:
- User name.
- Encrypted password.
- User ID number (UID).
- User's group ID number (GID).
- Full name of the user.
- User home directory.
- Login shell.
Note that the password is stored as a single "x" character (ie. not actually stored in this file), and the actual password data is stored in a file called /etc/shadow.
The /etc/shadow file contains encrypted password as well as other information such as account or password expiration values, etc. The /etc/shadow file is readable only by the root account and is therefore less of a security risk.
# cat /etc/passwd root:x:0:0:root:/root:/bin/bash daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin bin:x:2:2:bin:/bin:/usr/sbin/nologin sys:x:3:3:sys:/dev:/usr/sbin/nologin ... k:$6$Sguv0Sl...V5HkJo/:17059:0:99999:7::: ... logstash:!:17212:::::: kibana:!:17215:::::: elasticsearch:*:17217:0:99999:7::: kafka:!:17235:0:99999:7:::
Note that this /etc/shadow file, unlike the /etc/passwd, is not readable by unprivileged users.
If the password field contains some string that is not a valid result of crypt, for instance ! or *, the user will not be able to use a unix password to log in.
The /etc/group file stores group information or defines the user groups i.e. it defines the groups to which users belong.
# head -5 /etc/group root:x:0: daemon:x:1: bin:x:2: sys:x:3: adm:x:4:syslog,k
- Group_name: The name of group.
- Password: Generally password is not used, hence it is empty/blank. It can store encrypted password.
- Group ID (GID): Each user must be assigned a group ID.
- Group List: It is a list of users who are members of the group.
Users can be created using a useradd command:
# useradd -m -d /home/testuser -s /bin/bash testuser
Here we difined user's home directory, the shell type, and the username.
# tail /etc/passwd ... kafka:x:1002:1002::/home/kafka: testuser:x:1003:1003::/home/testuser:/bin/bash
We can see the user and group ids have been auto incremented from the previous user.
# tail /etc/shadow ... testuser:!:17254:0:99999:7:::
Note that we cannot login (see the '!' in password column).
So, we need to set a password:
# passwd testuser Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
Then, let's look into the shadow file again:
# tail /etc/shadow ... testuser:$6$SY5Idvn1$oM0D3MDRlDnSJ7oSekayG3Dj9jGtQnIAVzqFy1lGZqJVWTLp8m81acIiunfFqgnlTvc/eGTBUWIqGw4WoIe4L/:17255:0:99999:7:::
Now, we see that the password has been set.
But we can lock it using usermod -L
:
# usermod -L testuser # tail /etc/shadow ... testuser:!$6$SY5Idvn1$oM0D3MDRlDnSJ7oSekayG3Dj9jGtQnIAVzqFy1lGZqJVWTLp8m81acIiunfFqgnlTvc/eGTBUWIqGw4WoIe4L/:17255:0:99999:7:::
We can unlock:
# usermod -U testuser
The lock/unlock can be done with passwd
:
# grep testuser /etc/shadow testuser:$6$ynuBla7u$.34OeavRiWPS.7sF3/UZPlw7i21Rj94SCqfBa5ZbxitAJUGI0p12gnHUw/lNIWI1nUN0V9WWTalADI6MLBpu8/:18483:0:99999:7:::
Lock the account:
# passwd -l testuser Locking password for user testuser. passwd: Success # grep testuser /etc/shadow testuser:!!$6$ynuBla7u$.34OeavRiWPS.7sF3/UZPlw7i21Rj94SCqfBa5ZbxitAJUGI0p12gnHUw/lNIWI1nUN0V9WWTalADI6MLBpu8/:18483:0:99999:7:::
Unlock the account:
# passwd -u testuser Unlocking password for user testuser. passwd: Success # grep testuser /etc/shadow testuser:$6$ynuBla7u$.34OeavRiWPS.7sF3/UZPlw7i21Rj94SCqfBa5ZbxitAJUGI0p12gnHUw/lNIWI1nUN0V9WWTalADI6MLBpu8/:18483:0:99999:7:::
Let's check what's in the home directory:
# ls /home/testuser examples.desktop
Note that the "examples.desktop" came from /etc/skel:
# ls /etc/skel examples.desktop
We can also see the owner of the "testuser" has been set:
# ls -l /home drwxr-xr-x 86 k k 4096 Mar 27 22:04 k drwxr-xr-x 2 testuser testuser 4096 Mar 29 16:57 testuser # grep testuser /etc/group testuser:x:1003:
To delete a user, we can use userdel command:
# userdel testuser
At this point, the user has been removed from the three files: '/etc/passwd', '/etc/shadow', and '/etc/group'. However, we still need to delete the user's home directory:
# rm -rf /home/testuser
The chage
command changes the number of days between password changes and the date of the last password change.
This information is used by the system to determine when a user must change his/her password.
# chage testuser Changing the aging information for testuser Enter the new value, or press ENTER for the default Minimum Password Age [0]: 7 Maximum Password Age [99999]: 90 Last Password Change (YYYY-MM-DD) [2020-08-09]: Password Expiration Warning [7]: Password Inactive [-1]: Account Expiration Date (YYYY-MM-DD) [-1]:
The list of groups for "testuser":
# id testuser uid=1001(testuser) gid=1001(testuser) groups=1001(testuser)
Let's add "sales" group to "testuser":
# groupadd sales # usermod -aG sales testuser # id testuser uid=1001(testuser) gid=1001(testuser) groups=1001(testuser),1002(sales)
DevOps
DevOps / Sys Admin Q & A
Linux - system, cmds & shell
- Linux Tips - links, vmstats, rsync
- Linux Tips 2 - ctrl a, curl r, tail -f, umask
- Linux - bash I
- Linux - bash II
- Linux - Uncompressing 7z file
- Linux - sed I (substitution: sed 's///', sed -i)
- Linux - sed II (file spacing, numbering, text conversion and substitution)
- Linux - sed III (selective printing of certain lines, selective definition of certain lines)
- Linux - 7 File types : Regular, Directory, Block file, Character device file, Pipe file, Symbolic link file, and Socket file
- Linux shell programming - introduction
- Linux shell programming - variables and functions (readonly, unset, and functions)
- Linux shell programming - special shell variables
- Linux shell programming : arrays - three different ways of declaring arrays & looping with $*/$@
- Linux shell programming : operations on array
- Linux shell programming : variables & commands substitution
- Linux shell programming : metacharacters & quotes
- Linux shell programming : input/output redirection & here document
- Linux shell programming : loop control - for, while, break, and break n
- Linux shell programming : string
- Linux shell programming : for-loop
- Linux shell programming : if/elif/else/fi
- Linux shell programming : Test
- Managing User Account - useradd, usermod, and userdel
- Linux Secure Shell (SSH) I : key generation, private key and public key
- Linux Secure Shell (SSH) II : ssh-agent & scp
- Linux Secure Shell (SSH) III : SSH Tunnel as Proxy - Dynamic Port Forwarding (SOCKS Proxy)
- Linux Secure Shell (SSH) IV : Local port forwarding (outgoing ssh tunnel)
- Linux Secure Shell (SSH) V : Reverse SSH Tunnel (remote port forwarding / incoming ssh tunnel) /)
- Linux Processes and Signals
- Linux Drivers 1
- tcpdump
- Linux Debugging using gdb
- Embedded Systems Programming I - Introduction
- Embedded Systems Programming II - gcc ARM Toolchain and Simple Code on Ubuntu/Fedora
- LXC (Linux Container) Install and Run
- Linux IPTables
- Hadoop - 1. Setting up on Ubuntu for Single-Node Cluster
- Hadoop - 2. Runing on Ubuntu for Single-Node Cluster
- ownCloud 7 install
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox I
- Ubuntu 14.04 guest on Mac OSX host using VirtualBox II
- Windows 8 guest on Mac OSX host using VirtualBox I
- Ubuntu Package Management System (apt-get vs dpkg)
- RPM Packaging
- How to Make a Self-Signed SSL Certificate
- Linux Q & A
- DevOps / Sys Admin questions
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization