Skip to content

Latest commit

ย 

History

History
233 lines (195 loc) ยท 5.01 KB

File metadata and controls

233 lines (195 loc) ยท 5.01 KB

๐Ÿ› ๏ธ 1. Display Help for useradd Command

๐Ÿ“Œ Command:

useradd -h

๐Ÿ“Œ Example Output:

Usage: useradd [options] LOGIN
Options:
  -b, --base-dir BASE_DIR       ๐Ÿ  Set base home directory
  -c, --comment COMMENT         ๐Ÿ“ Add user comment
  -d, --home-dir HOME_DIR       ๐Ÿ“‚ Specify home directory path
  -e, --expiredate EXPIRE_DATE  โณ Set account expiration date
  -g, --gid GROUP               ๐Ÿ‘ฅ Set primary group ID
  -G, --groups GROUPS           ๐Ÿ”„ Add secondary groups
  -m, --create-home             ๐Ÿก Create a home directory
  -M, --no-create-home          ๐Ÿšซ Do not create a home directory
  -s, --shell SHELL             ๐Ÿ–ฅ๏ธ Set login shell
  -u, --uid UID                 ๐Ÿ”ข Set user ID
  -p, --password PASSWORD       ๐Ÿ”‘ Set an encrypted password
  -r, --system                  โš™๏ธ Create a system user

โœ… This helps understand all useradd options.


๐Ÿ  2. Create a New User with Home Directory

๐Ÿ“Œ Command:

useradd -m -d /home/john john

โœ… Creates user john with a home directory /home/john.

๐Ÿ“Œ Verify:

tail -n 1 /etc/passwd

Expected Output:

john:x:1001:1001::/home/john:/bin/bash

๐Ÿ”‘ 3. Set Password for a User

๐Ÿ“Œ Command:

passwd john

โœ… Sets a password for user john.

๐Ÿ“Œ For an Encrypted Password:

useradd -p $(openssl passwd -1 "MySecurePassword") user1

โœ… Uses openssl to set an encrypted password.


๐ŸŽฏ 4. Create a User with a Specific UID

๐Ÿ“Œ Command:

useradd -u 1050 user2

โœ… Creates user2 with UID 1050.

๐Ÿ“Œ Verify:

id user2

Expected Output:

uid=1050(user2) gid=1050(user2) groups=1050(user2)

๐Ÿ‘ฅ 5. Assign a User to a Specific Group

๐Ÿ“Œ Command:

useradd -g developers user3

โœ… Creates user3 and assigns it to the developers group.

๐Ÿ“Œ Check Group:

id user3

๐Ÿ”„ 6. Add a User to Multiple Groups

๐Ÿ“Œ Command:

useradd -G sudo,developers user4

โœ… Adds user4 to sudo and developers groups.

๐Ÿ“Œ Verify:

groups user4

Expected Output:

user4 : user4 sudo developers

โณ 7. Set Account Expiration Date

๐Ÿ“Œ Command:

useradd -e 2025-12-31 user5

โœ… User user5 will expire on 31 Dec 2025.

๐Ÿ“Œ Verify:

chage -l user5

Expected Output:

Account expires: Dec 31, 2025

๐Ÿšซ 8. Create a User Without a Home Directory

๐Ÿ“Œ Command:

useradd --no-create-home user6

โœ… Creates user6 without a home directory.

๐Ÿ“Œ Verify:

ls -ld /home/user6

Expected Output: (No directory found)

ls: cannot access '/home/user6': No such file or directory

๐Ÿ–ฅ๏ธ 9. Set Default Shell for a User

๐Ÿ“Œ Command:

useradd -s /bin/zsh user7

โœ… Sets /bin/zsh as the default shell for user7.

๐Ÿ“Œ Verify:

cat /etc/passwd | grep user7

Expected Output:

user7:x:1007:1007::/home/user7:/bin/zsh

โš™๏ธ 10. Create a System User

๐Ÿ“Œ Command:

useradd -r systemuser

โœ… Creates a system user systemuser.

๐Ÿ“Œ Verify:

id systemuser

Expected Output:

uid=497(systemuser) gid=497(systemuser) groups=497(systemuser)

๐Ÿท๏ธ 11. Create a User with Custom Comments

๐Ÿ“Œ Command:

useradd -c "Project Manager" user8

โœ… Adds Project Manager as a comment for user8.

๐Ÿ“Œ Verify:

cat /etc/passwd | grep user8

Expected Output:

user8:x:1008:1008:Project Manager:/home/user8:/bin/bash

๐Ÿ 12. Delete a User

๐Ÿ“Œ Command:

userdel user9

โœ… Deletes user9 but keeps their home directory.

๐Ÿ“Œ Delete User and Home Directory

userdel -r user10

โœ… Deletes user10 and removes /home/user10.


๐Ÿ”ฅ Summary Table

๐ŸŽฏ Command ๐Ÿ“Œ Description
useradd -m john Creates a user john with a home directory
passwd john Sets a password for john
useradd -u 1050 user2 Creates user2 with UID 1050
useradd -g developers user3 Assigns user3 to developers group
useradd -G sudo,developers user4 Adds user4 to multiple groups
useradd -e 2025-12-31 user5 Sets an expiration date for user5
useradd --no-create-home user6 Creates user6 without a home directory
useradd -s /bin/zsh user7 Sets /bin/zsh as the shell for user7
useradd -r systemuser Creates a system user
useradd -c "Project Manager" user8 Adds a comment for user8
userdel user9 Deletes user9 but keeps home directory
userdel -r user10 Deletes user10 and removes home directory

โšก