User and Group Management in Linux
Linux is fundamentally a multi-user operating system. That means multiple people
(users) can log in and use the same machine without interfering with each other.
👉 Why is this important? Security, access control, and organization. For example, on a
server, you don’t want every user to have admin rights or access to each other’s files.
1. Users in Linux
🔹 A user is simply an account on the system. Each user has:
● Username
● User ID (UID) – a unique number identifying the user
● Home directory /home/username
● Default shell (like bash, zsh, etc.)
a) Adding a New User
Command:
bash
sudo adduser username
● Prompts for password and other details
● Automatically creates a home directory /home/username
Example:
bash
sudo adduser raj
✔ Creates user raj with home /home/raj.
b) Removing a User
Command:
bash
sudo deluser username
🚨 This only removes the user account, not their files.
To also remove the home directory and files:
bash
sudo deluser --remove-home username
Example:
bash
sudo deluser --remove-home raj
✔ Removes raj and deletes /home/raj.
c) Modifying a User
Use the usermod command to change user details.
1. Change username
bash
sudo usermod -l new_username old_username
Example:
bash
sudo usermod -l rahul raj
👉 raj is now renamed to rahul.
Note: If the user is logged in, you may need to log them out or kill their session:
bash
kill -9 PID
(PID is their process ID, which can be found with who or ps.)
2. Change user’s home directory
bash
sudo usermod -d /new/home/directory -m username
● -d → specifies new home directory
● -m → moves existing files from old home to new home
Example:
bash
sudo usermod -d /home/rahul_new -m rahul
2. Groups in Linux
🔹 A group is a collection of users.
● Used to manage permissions more efficiently.
● Example: The developers group can have access to code directories, while the
admins group has elevated privileges.
a) Adding a New Group
bash
sudo groupadd groupname
Example:
bash
sudo groupadd developers
✔ Creates a group named developers.
b) Adding a User to a Group
bash
sudo usermod -aG groupname username
● -a → append (adds without removing existing groups)
● -G → specifies group(s)
Example:
bash
sudo usermod -aG developers rahul
✔ Adds user rahul to group developers.
c) Removing a User from a Group
bash
sudo deluser username groupname
Example:
bash
sudo deluser rahul developers
✔ Removes user rahul from group developers.
3. Checking User and Group Info
● Who am I logged in as?
bash
whoami
● Show current user details and groups:
bash
id username
● Show all groups a user belongs to:
bash
groups username
● List all users:
bash
cat /etc/passwd
● List all groups:
bash
cat /etc/group
4. Practical Scenario for Students
Imagine you’re setting up a Linux machine in a college lab:
1. Each student gets their own user account (so they can’t delete each other’s files).
2. All students in "Class A" are added to group classA.
3. If there’s a project folder /projects/classA, you give group permissions so only
students in classA can access it.
4. If someone graduates, you delete their user (and optionally their home directory).
This way, Linux ensures organization and security automatically.
1. Introduction to File Transfers in Linux
Transferring files is a core admin skill. Secure protocols are preferred for safety.
A. SCP (Secure Copy Protocol)
● Purpose: Securely copy files and directories between local and remote systems
using SSH encryption.
● Syntax:
● text
scp [options] source destination
●
● Copy local to remote:
● text
scp [Link] user@remote_host:/path/
●
● Copy remote to local:
● text
scp user@remote_host:/path/[Link] /local/dir/
●
● Copy directories recursively:
● text
scp -r my_folder user@remote_host:/path/
●
● Common options:
-C (compression), -i (use SSH key), -P (port), -p (preserve attributes), -r
(recursive).
Note: Overwrites files silently. Needs file read/write permissions. Authentication via
password or SSH key.
B. FTP and SFTP
● FTP: Simple but not secure. Use only in trusted networks.
● SFTP: Secure alternative using SSH.
● text
sftp user@remote_host
●
● Then use interactive commands: get, put, ls.
C. rsync
● Purpose: Fast, efficient copying and synchronization of files and directories; only
differences are copied.
● Syntax:
● text
rsync [options] source destination
●
● Examples:
● Local to remote:
● text
rsync -avz [Link] user@remote_host:/path/
●
● Remote to local:
● text
rsync -avz user@remote_host:/path/[Link] /local/path/
●
● Directories (with progress):
● text
rsync -avz --progress dir/ user@remote_host:/path/
●
● Only transfer changes, supports resume (unlike scp).
2. Networking Commands
Basic networking commands diagnose, monitor, and troubleshoot connectivity.
A. Checking Connectivity
● ping
● Sends network packets to a target to check reachability.
● Usage:
● text
ping [Link]
ping -c 5 [Link]
●
● Output: Response times, packet loss.
● traceroute
● Tracks the path (hops) packets take to a destination.
● Usage:
● text
traceroute [Link]
●
● Output: Shows routers along the way.
B. Viewing Network Configuration
● ip
● Show interfaces and IP details:
● text
ip addr
ip route
ip link show
●
● More modern replacement for ifconfig.
● netstat / ss
● ss (modern) or netstat (legacy) show socket statistics:
● text
ss -tuln
netstat -ant
●
● View open ports, listening services.
● nslookup / dig
● Query DNS for IP/domain info:
● text
nslookup [Link]
dig [Link]
●
● Use for troubleshooting name resolution.
Why Shell Scripting?
Shell scripting helps in automating repetitive tasks, improving efficiency, and reducing
human errors. It acts as a powerful tool for managing system processes, file handling,
and data manipulation.
● Infrastructure Automation
○ Helps in server provisioning & configuration management.
○ Works with Docker, Kubernetes, Terraform, and Ansible for automation.
● CI/CD Pipeline Integration
○ Automates code deployments, testing, and version control with Git.
○ Executes builds, runs tests, and deploys applications seamlessly.
● Monitoring & Logging
○ Automates log analysis, alerts, and system health checks.
○ Helps in tracking application performance & identifying bottlenecks.
● Security & Compliance
○ Implements automated security audits and vulnerability scanning.
○ Manages user permissions, SSH keys, and firewall rules.
Real-World Use Cases
● Automating data extraction from APIs & databases.
● Managing log files & backups across servers.
● Automating server scaling & application deployments.
● Integrating shell scripts with Python, SQL, and cloud platforms.
Let’s run our first Shell Script:
echo "Hello, Shell Scripting!"
Creating and Executing a Script:
Create a New Shell Script
- Open the terminal and type:
nano My_script.sh
This will open the nano editor in your terminal.
Write Your First Shell Script
Inside nano, type the following lines:
#!/bin/bash
echo "Hello from my first shell script!"
What’s happening here?
● #!/bin/bash → This is called a shebang, which tells the system to run
the script using Bash.
● echo "..." → Prints text to the terminal.
Save & Exit nano
Once you’ve written the script, do the following:
● Press Ctrl + X → This will prompt you to save the file.
● Press Y (Yes) → This confirms that you want to save the changes.
● Press Enter → This saves the file with the name my_script.sh.
Now you’re back in the terminal.
Make the Script Executable
Before running the script, you need to give it permission to execute. Type:
chmod +x My_script.sh
Run Your Script
Now, run the script using:
./My_script.sh
Digital Clock Project Step by Step
Part A — Do it step by step in Terminal
1) Create a working folder (optional but tidy)
mkdir -p ~/scripts
cd ~/scripts
● mkdir -p creates the folder if it doesn’t exist (no error if it does).
● cd moves you into it so all files live here.
2) Create the script file
Use nano (simple text editor in terminal):
nano [Link]
● This opens a new file named [Link].
3) Paste the script into nano
Paste everything below (we’ll explain it line-by-line in Part B):
#!/bin/bash # Shebang: run this file with
/bin/bash
# Fancy Digital Clock Script with Colors
while true # Start an infinite loop (true
always succeeds)
do # Begin loop body
clear # Clear the terminal window so it
doesn't scroll
# Define colors
RED="\033[1;31m" # \033 = ESC; [1;31m = bold red
GREEN="\033[1;32m" # bold green
YELLOW="\033[1;33m" # bold yellow
BLUE="\033[1;34m" # bold blue
CYAN="\033[1;36m" # bold cyan
RESET="\033[0m" # reset all attributes
(color/bold/etc.)
# Current time
TIME=$(date +%T) # Command substitution: e.g.,
14:05:09 (24-hour)
DATE=$(date +"%A, %d %B %Y") # Full day, zero-padded date, full
month, year
# Print in a nice box with colors
echo -e "${BLUE}==============================${RESET}" # blue
line, then reset
echo -e "${CYAN} DIGITAL CLOCK ${RESET}" # cyan
title, then reset
echo -e "${BLUE}==============================${RESET}" # blue
line again
echo -e " ${GREEN}Time:${RESET} ${YELLOW}$TIME${RESET}" # label
green, value yellow
echo -e " ${GREEN}Date:${RESET} ${YELLOW}$DATE${RESET}" # same
for date
echo -e "${BLUE}==============================${RESET}" # bottom
border
sleep 1 # wait 1 second before next
refresh
done # end loop, repeat
4) Save and exit nano
● Press Ctrl + O → Enter to save.
● Press Ctrl + X to exit.
5) Make it executable
chmod +x [Link]
● chmod +x adds the “execute” permission so you can run it directly.
6) (Optional) Check permissions
ls -l [Link]
● You should see something like -rwxr-xr-x which means it’s executable.
7) Run the clock
./[Link]
● ./ runs the file in the current directory.
● Stop it any time with Ctrl + C.
If you get “Permission denied”: you missed chmod +x.
If you get “command not found”: check you’re in the right folder (pwd, ls), or run
bash [Link].
Part B — Explain every line & token
#!/bin/bash
● Shebang (#!): tells the OS to run this script with the Bash interpreter at /bin/bash.
# Fancy Digital Clock Script with Colors
● Comment: # makes the rest of the line ignored by the shell (for humans only).
while true
● while starts a loop that continues as long as its test/command returns exit code 0
(success).
● true is a command that always returns success ⇒ infinite loop.
do
● Begins the loop body (the commands that will repeat).
clear
● Clears the terminal display and moves the cursor to the top-left. Prevents scrolling; gives
a “live” refresh feel.
Color variables (ANSI escape sequences)
RED="\033[1;31m"
GREEN="\033[1;32m"
YELLOW="\033[1;33m"
BLUE="\033[1;34m"
CYAN="\033[1;36m"
RESET="\033[0m"
● NAME="value" assigns a string to a shell variable (no $ on the left when assigning).
● \033 is ESC (octal 033). With echo -e, \033 becomes the ESC control character.
● [ ... m selects text attributes (SGR codes):
○ 1 = bold/bright
○ 31,32,33,34,36 = red, green, yellow, blue, cyan
○ 0 = reset all attributes
● We use ${RESET} after colored sections to return to normal text so color doesn’t “leak”
into later output.
Capture current time & date
TIME=$(date +%T)
DATE=$(date +"%A, %d %B %Y")
● $( ... ) is command substitution: run the command and capture its output into the
variable.
● date +%T → HH:MM:SS (24-hour).
● date +"%A, %d %B %Y":
○ %A full weekday (e.g., Monday)
○ %d day with leading zero (01–31)
○ %B full month name (e.g., August)
○ %Y 4-digit year (e.g., 2025)
○ Quotes keep spaces and commas as part of the format.
Print the box (labels + values with colors)
echo -e "${BLUE}==============================${RESET}"
echo -e "${CYAN} DIGITAL CLOCK ${RESET}"
echo -e "${BLUE}==============================${RESET}"
● echo prints text.
● -e makes echo interpret backslash escapes inside expanded variables (so \033
becomes ESC).
● "${BLUE}...${RESET}":
○ ${BLUE} expands to the ESC code for blue.
○ 30 = chars are just a visual border—change as you like.
○ ${RESET} restores default color after each line.
echo -e " ${GREEN}Time:${RESET} ${YELLOW}$TIME${RESET}"
echo -e " ${GREEN}Date:${RESET} ${YELLOW}$DATE${RESET}"
● Leading space = small left padding.
● ${GREEN}Time:${RESET} prints the label in green, then resets.
● Multiple spaces align the values visually.
● ${YELLOW}$TIME${RESET} prints the value in yellow, then resets.
● $TIME and $DATE expand to the values captured earlier.
echo -e "${BLUE}==============================${RESET}"
● Bottom border, same as the top.
sleep 1
● Pauses for 1 second so the time updates once per second (throttle the loop).
done
● Ends the loop body; control jumps back to while true and repeats.