Linux — User & Group ( Practice Question)

Sonam Kumari Singh
4 min readJan 13, 2024

--

Q. Create a new user “sonam” without home directory and with user id 1286 ?

Here we used ‘-M‘ option to create user without user’s home directory. Where ‘-u‘ defines new user’s UID

root@localhost ~# useradd -M -u 1286 sonam

Then we check user is created or not… using that command

root@localhost ~# id “username”

Q. Create a new user diffgrup with user id 1364 and add group id 1864 ?

Every user must have a User ID (UID) User Identification Number. By default UID 0 is reserved for root user and UID’s ranging from 1–99 are reserved for other predefined accounts. Further UID’s ranging from 100–999 are reserved for system accounts and groups.

When creating a new group, you can assign a unique group ID (GID) to distinguish it from other groups. For this, you can use the -g <GID> flag with the groupadd command. Here’s the command that creates a group diffgrup and assigns it a GID of 1864.

root@localhost ~# groupadd -g 1864 diffgrup

root@localhost ~# useradd -u 1364 -g 1864 diffgrup

root@localhost ~# id diffgrup

Q. Create a user expiry which should be expired by the date 15 jan 2024 and password shouldbe expired after 5 days.?

The date on which the user account will be disabled. The date is specified in the format YYYY-MM-DD. If not specified, useradd will use the default expiry date specified by the EXPIRE variable in /etc/default/useradd, or an empty string (no expiry) by default.

  • f, — inactive INACTIVE
  • -e, — expiredate EXPIRE_DATE

The number of days after a password expires until the account is permanently disabled. A value of 0 disables the account as soon as the password has expired, and a value of -1 disables the feature.

If not specified, useradd will use the default inactivity period specified by the INACTIVE variable in /etc/default/useradd, or -1 by default.

root@localhost ~# useradd -e 2024–01–15 -f 5 expiry

Q. Create a user “sonam” such that it should not be able to access shell ?

root@localhost ~# useradd -s /sbin/nologin nolog

We can assign different login shells to a each user with ‘-s‘ option and /sbin/nologin or /usr/sbin/nologin used as a shell in Linux to politely refuse a login attempt.

Q. How can I run a shell as a user that has nologin shell access?

root@localhost ~# sudo su sonam -
root@localhost ~# cat /etc/passwd | grep sonam
root@localhost ~# sudo -u sonam -s bash
sonam@localhost ~# echo hello everyone > nologin.txt

Q. Add a User disabled without Home Directory, No Shell, No Group and Custom Comment “it’s disabled forever” ?

Here we used the ‘-M‘ option to create a user without the user’s home directory and ‘-N‘ argument is used that tells the system to only create username (without group). The ‘-r‘ argument is for creating a system user.

root@localhost ~# useradd -M -N -r -s /bin/false -c “it’s disabled forever” disabled

Q. Create a user immortal such that it’s password shouldn’t expire ?

# useradd -f -1 immortal 

The ‘-f‘ argument is used to define the number of days after a password expires. A value of 0 inactive the user account as soon as the password has expired. By default, the password expiry value set to -1 means never expire.

For reference: https://linux.die.net/man/8/useraddor use: man <command>

Self Practice

Group:

  1. Create a group ninety with 1780 groupid.
  2. Create a group named random such that it should have a group id in a defined range randomly with a single command.
  3. Add user diffgrup in the ninety group

Modifying details:

Change user nohome home directory to /home/demo and also change user id from 1286 to 1596.

Change user diffgrup user id from 1364 to 19833.

Modify user expiry such that it should be expired by the date 15 june 2022 and password should notbe expired.

modify user nolog such that it should be able to access sea shell.

modify User disabled and update details such that after change it should be with Home Directory /home/disable, with bash Shell, should be in the random Group.

Modify the user immortal such that it’s password should expire in 3 days.

--

--

Sonam Kumari Singh

SONAM here! Grateful for your connection! Tech enthusiast exploring new languages, deep into DevOps, with a spotlight on Linux. 😊🚀