UserAndGroupAdminOnDebianUbuntu: User and Group Administration on Debian and Ubuntu
Copyright (C) 2020 – 2023 Exforge exforge@x386.org
# - This document is free text: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # any later version. # # - This document is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # - You should have received a copy of the GNU General Public License # along with this program. If not, see <https://www.gnu.org/licenses/>.
Specs
# - Aimed for Debian 12/11 and Ubuntu 22.04/20.04 Servers, but works fine on # Debian, Ubuntu and derivatives' desktops (Ubuntu, Kubuntu, Xubuntu, # Lubuntu, MX, Mint etc) too # # Sources: https://www.packtpub.com/networking-and-servers/mastering-ubuntu-server-second-edition
1. User Add and Delete
#-- 1.1. Add a new user jdoe and create home foldersudo useradd -d /home/jdoe -m jdoe
# #-- 1.2. Change user's passwordsudo passwd jdoe
# #-- 1.3. Delete a usersudo userdel jdoe
# remove home directory toosudo userdel -r jdoe
2. Files of User Information
#-- 2.1. /etc/passwd fileexforge:x:1000:1000:Exforge,,,:/home/exforge:/bin/bash username:pw:UID:GID:Name,Surname,XX:homefolder:shell
# #-- 2.2. /etc/shadow file # Passwords are stored as hashed in shadow file# exforge:$6$z09H4l.6$h....A/tDL0:18221:0:99999:7::: # username:pwHash:DatesSinceLastPwChange:MinDaysToChangePw: # MaxDaysToChangePw:DaysBeforeUserWarnedToChangePw: # DaysToPwExpire:DaysToUserDisable
# #-- 2.3. User pw information extracted from /etc/shadowsudo passwd -S username
# #-- 2.4. Default contents for home folders: # Contents of /etc/skel folder is distributed to created user's home directory
3. root user
#-- 3.1. root account is locked by default in Ubuntu. It is optional in # Debian. # To give a pw to (and unlock) rootsudo passwd
# #-- 3.2. switch to root account without unlocking itsudo -i
# #-- 3.3. switch to another user (if you know pw)su - username
# #-- 3.4. switch to another user (if you don't know pw)sudo su - username
4. Batch user add
#-- 4.1. Create a text file for userstouch users.txt
# #-- 4.2. Change the permissions of the filechmod 600 users.txt
# #-- 4.3. Add users information to the filenano users.txt
user1:password:::User1:/home/user1:/bin/bash user2:password:::User2:/home/user2:/bin/bash user3:password:::User3:/home/user3:/bin/bash
#username:passwd:uid:gid:full name:home_dir:shell # #-- 4.4. Process file to add userssudo newusers users.txt
# You can check users from /etc/passwd # #-- 4.5. It is a good idea to change passwords of the userssudo passwd user1
5. Group Management
#-- 5.1. List of groupsgroups
# orcat /etc/group
# it is similar to /etc/password # #-- 5.2. Add a new groupsudo groupadd admins
# #-- 5.3. Delete a groupsudo groupdel admins
# #-- 5.4. List members of a groupgetent group groupname
# #-- 5.5. Add a user to a group # -a append new group to groups of user # -G as a secondary groupsudo usermod -aG admins myuser
sudo usermod -a -G admins myuser
# orsudo gpasswd -a <username> <group>
# #-- 5.6. Change users primary groupsudo usermod -g admins myuser
# #-- 5.7. Remove user from a groupsudo gpasswd -d <username> <grouptoremove>
6. User manipulation
#-- 6.1. Change username # First change home directorysudo usermod -d /home/jsmith -m jdoe
# Then change usernamesudo usermod -l jsmith jdoe
# #-- 6.2. Lock a usersudo passwd -l <username>
# #-- 6.3. unlocksudo passwd -u <username>
# #-- 6.4. Password expiration infosudo chage -l <username>
#
7. sudo Group
# Members of sudo group can use sudo command #-- 7.1. Configuration of sudo group memberssudo visudo
%sudo ALL=(ALL:ALL) ALL
# sudo group members # can use sudo from any terminal # can use sudo to impersonate any user # can use sudo to impersonate any group # can use sudo for any commandcharlie ubuntu-server=(dscully:admins) /usr/bin/apt
# user charlie, # can only use sudo on ubuntu_server # can only impersonate dscully user # can only impersonate admins group # can only run /usr/bin/apt # # For a user to sudo without passwdansible ALL=(ALL) NOPASSWD: ALL
# #-- 7.2. List granted sudo privilegessudo -l