0% found this document useful (0 votes)
20 views18 pages

Linux Fundamentals Lab Manual 3

The document provides a comprehensive guide on essential system administration tasks, including user management, system monitoring, and command usage in a Linux environment. It includes exercises for creating and deleting users, managing permissions, checking system resources, and performing system updates. Each exercise is accompanied by commands, explanations, and expected outputs to facilitate understanding and execution.

Uploaded by

siroh98121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views18 pages

Linux Fundamentals Lab Manual 3

The document provides a comprehensive guide on essential system administration tasks, including user management, system monitoring, and command usage in a Linux environment. It includes exercises for creating and deleting users, managing permissions, checking system resources, and performing system updates. Each exercise is accompanied by commands, explanations, and expected outputs to facilitate understanding and execution.

Uploaded by

siroh98121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Essential System Administration, User Management, and Administrator

Privileges

Part 1: Essential System Administration Exercises

1. Create a New User

Exercise: Create a new user named student.

Command: sudo adduser student

Explanation:

• This command creates a new user and prompts for additional information, such
as password and optional details (full name, room number, etc.).
• The sudo ensures the command runs with administrative privileges.

Expected Output:

Adding user `student' ...


Adding new group `student' (1001) ...
Adding new user `student' (1001) with group `student' ...
Creating home directory `/home/student' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for student
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]

2. Delete a User

Exercise: Remove the user student and their home directory.

Command: sudo deluser student

sudo deluser --remove-home student

Explanation:

• This command deletes the student user and their home directory.
• The --remove-home flag ensures the user’s home directory is also deleted.

Expected Output:

info: Removing crontab ...


info: Removing user `student' ...

Looking for files to backup/remove ...


Removing user `student' ...
Removing home directory `/home/student' ...
Removing mail spool ...
Done.

3. Switch to Superuser

Exercise: Gain superuser (root) access temporarily.

Command: sudo -i

Explanation:

• The sudo -i command allows a user to log in as the superuser temporarily.


• A password is required unless the user is configured for passwordless sudo
access.
• Type exit to logout.

Expected Output:

root@hostname:~#

4. Check System Uptime

Exercise: Display how long the system has been running.

Command: uptime

Explanation:

• Shows the current time, system uptime, number of users logged in, and the
system’s load averages.

Expected Output:

15:23:45 up 3 days, 4:23, 1 user, load average: 0.15, 0.10, 0.05

5. List Active Processes

Exercise: Display a list of all running processes.

Command: ps aux

Explanation:

• Provides a detailed list of all running processes, including user ownership,


process ID (PID), and resource usage.
Expected Output:

USER PID %CPU %MEM VSZ RSS TTY STAT START TIME
COMMAND
root 1 0.0 0.1 169612 1176 ? Ss Jan04 0:03
/sbin/init
...

Command Explanation
ps Displays processes for the current shell session.
ps -f Shows a detailed view of processes with parent PID (PPID)
and start time.
ps -e or ps -A Lists all processes running on the system.
ps -l Displays processes in long format with additional technical
details.
ps -u username Shows processes owned by a specific user.
ps -o pid,cmd Displays custom columns (e.g., PID and command).
ps -t pts/1 Lists processes associated with a specific terminal (e.g.,
pts/1).
ps -eo pid,%cpu Lists processes with their CPU usage, customizable output.
`ps -ef grep xyz`
ps aux Displays all processes with memory, CPU usage, and user
details.

6. Disk Usage

Exercise: Find the total and free disk space on the system.

Command: df -h

Explanation:

• Displays disk usage in human-readable format (e.g., GB, MB).

Expected Output:

Filesystem Size Used Avail Use% Mounted on


/dev/sda1 50G 15G 33G 31% /
...

Command Explanation
df Displays disk space usage for all mounted filesystems in default
format.
df -h Shows disk space in human-readable format (e.g., GB, MB).
df -a Includes all filesystems, including ones with zero size (e.g.,
pseudo-filesystems).
df -T Displays the type of each filesystem (e.g., ext4, xfs).
df -i Displays inode usage instead of disk space.
df --total Adds a total row summarizing all filesystems' disk usage.
df Displays disk usage for the filesystem containing the specified
/path/to/dir
directory.
df -x tmpfs Excludes specific filesystem types (e.g., tmpfs).
df -h -- Shows only the available space in human-readable format.
output=avail
df -P Displays output in POSIX-compatible format (single line per
filesystem).

7. Check Memory Usage

Exercise: Display the system's memory usage.

Command: free -h

Explanation:

• Shows memory (RAM) and swap usage in a human-readable format.

Expected Output:

total used free shared buff/cache


available
Mem: 7.8G 2.1G 4.5G 123M 1.2G
5.2G
Swap: 2.0G 0.0G 2.0G

Command Explanation
free Displays memory usage in kilobytes (default format).
free -h Shows memory usage in human-readable format (e.g., GB, MB).
free -m Displays memory usage in megabytes.
free -g Displays memory usage in gigabytes.
free -t Adds a "total" row showing combined memory and swap usage.
free -s 5 Continuously updates memory usage every 5 seconds.
free --si Uses SI units (powers of 10) instead of binary (powers of 2) for sizes.
free -c 3 Displays memory usage 3 times, then exits.
free -w Includes detailed statistics about available memory.
free -l Shows memory usage including high and low memory statistics.

8. Update and Upgrade the System

Exercise: Update and upgrade all system packages.

Command: sudo apt update && sudo apt upgrade -y

Explanation:

• apt update updates the package lists.


• apt upgrade upgrades installed packages to the latest versions.
• The -y flag automatically confirms prompts.
Expected Output:

Reading package lists... Done


Building dependency tree... Done
Calculating upgrade... Done
...

9. View System Logs

Exercise: View the system log file for errors.

Command: sudo tail -n 50 /var/log/syslog

Explanation:

• Displays the last 50 lines of the system log file.

Expected Output:

Jan 4 15:23:45 hostname kernel: [ 0.000000] Initializing cgroup


subsys cpuset
...

10. Backup Files

Exercise: Create a compressed backup of the /etc directory.

Command: sudo tar -czvf etc_backup.tar.gz /etc

Expected Output: This command creates a compressed archive of the /etc directory
and saves it as etc_backup.tar.gz in the current working directory.

/etc/
/etc/passwd
/etc/hosts
...

Explanation:

1. Creates a compressed .tar.gz archive of the /etc directory.


2. sudo: Runs the command with superuser privileges, necessary because the /etc
directory often contains files that require administrative permissions to read.
3. tar: A utility used to archive files and directories.
4. Options (-czvf):
o -c: Create a new archive.
o -z: Compress the archive using gzip.
o -v: Verbose mode; shows the progress of files being archived.
o -f: Specifies the name of the archive file (etc_backup.tar.gz).
5. etc_backup.tar.gz: The name of the resulting compressed archive file.
6. /etc: The directory to be archived and compressed. The /etc directory contains
system configuration files.
11. Monitor Resource Usage in Real-Time

Exercise: Monitor real-time CPU and memory usage.

Command: htop

Explanation:

• An interactive process viewer showing real-time resource usage.


• htop is an interactive and user-friendly command-line utility for monitoring
system processes, resource usage, and performance in real-time. It provides a
more visual and intuitive alternative to the top command.
• You might need to install it first: sudo apt install htop

Expected Output:

• A graphical display of CPU, memory, and process information.

12. Find Recently Modified Files

Exercise: Find files modified in the last 2 days in /home. Command:

find /home -type f -mtime -2

Explanation:

• Searches for files modified within the last 2 days in the /home directory.

Expected Output:

/home/user/document.txt
...

13. Set a Default Permission Mask

To set a default permission mask in Linux, you generally work with umask (user file
creation mask), which defines the default file permissions for newly created files and
directories. The umask command sets which permissions will not be assigned to newly
created files and directories.

The umask command specifies a default permission mask by subtracting the umask
value from the default permissions (which are typically 666 for files and 777 for
directories).

Default permissions:

o Files: 666 (rw-rw-rw-)


o Directories: 777 (rwxrwxrwx)
The umask value is subtracted from these defaults. For example: If the umask is 022,
the result for files will be 644 (rw-r--r--), and for directories, it will be 755 (rwxr-xr-
x).

Exercise: Set the default permission mask so new files are created with rw-r--r--.
Command:

umask 022

Explanation:

• Ensures new files have the correct default permissions.

Expected Output:

• No output; use touch testfile && ls -l testfile to verify.


• If you set the umask to 000 but the file still doesn't have rwxrwxrwx permissions,
it might be due to how the file system and operating system handle file
permissions.

Common Umask Values:

Umask Value Resulting Permissions Resulting Permissions


(Files) (Directories)
0000 666 (rw-rw-rw-) 777 (rwxrwxrwx)
0222 444 (r--r--r--) 555 (r-xr-xr-x)
0022 644 (rw-r--r--) 755 (rwxr-xr-x)
0077 600 (rw-------) 700 (rwx------)

14. Grant Temporary sudo Access

Exercise: Temporarily grant student sudo access.

Command: sudo visudo

• Add the following line:

student ALL=(ALL) NOPASSWD: ALL

Explanation:

• Modifies the sudoers file to allow passwordless sudo access for student.

Expected Output:

• No output; test by running sudo -u student ls /root.


Part 2: User Management Exercises

1. Create a New User

Exercise: Create a user named student.

Command: sudo adduser student

Explanation:

• Creates a new user and prompts for additional details.

Expected Output:

Adding user `student' ...


Adding new group `student' (1001) ...
Adding new user `student' (1001) with group `student' ...
Creating home directory `/home/student' ...
Copying files from `/etc/skel' ...
Enter new UNIX password:
Retype new UNIX password:
passwd: password updated successfully
Changing the user information for student
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n]

2. Set a Password for a User

Exercise: Set a password for student.

Command: sudo passwd student

Explanation:

• Sets or changes the password for the user.

Expected Output:

Enter new UNIX password:


Retype new UNIX password:
passwd: password updated successfully

3. Add User to a Group

Exercise: Add student to the sudo group.

Command: sudo usermod -aG sudo student


Explanation:

• Adds the user to a secondary group, allowing administrative privileges.

Expected Output:

• No output if successful. Verify with:

groups student

4. List All Users

Exercise: List all users on the system.

Command: cut -d: -f1 /etc/passwd

Explanation:

• Extracts usernames from the /etc/passwd file, which stores user information.

Expected Output:

root
student
...

5. Delete a User

Exercise: Delete the user student and their home directory.

Command: sudo deluser --remove-home student

Explanation:

• Deletes the user and associated files, including their home directory.

Expected Output:

Looking for files to backup/remove ...


Removing user `student' ...
Removing home directory `/home/student' ...
Removing mail spool ...
Done.

6. Check User Groups

Exercise: Display the groups a user belongs to.

Command: groups student

Explanation:
• Lists the groups associated with the user.

Expected Output:

student : student sudo

7. Create a Group

Exercise: Create a new group named developers.

Command: sudo groupadd developers

Explanation:

• Adds a new group to the system.

Expected Output:

• No output if successful. Verify with:

groupadd developers or cat /etc/group

8. Change User's Shell

Exercise: Change the shell of student to /bin/bash.

Command: sudo usermod -s /bin/bash student

Explanation:

• Updates the user's default shell.

Expected Output:

• No output if successful. Verify with:

getent passwd student

9. Set Expiry Date for a User

Exercise: Set the expiry date of student to 2025-12-31.

Command: sudo chage -E 2025-12-31 student

Explanation:

• Sets the account expiration date.

Expected Output:

• No output if successful. Verify with:


sudo chage -l student

10. View Last Login

Exercise: Check the last login time of all users.

Command: last

Explanation:

• Displays login history.

Expected Output:

student pts/0 192.168.1.2 Wed Jan 5 14:22 still logged


in
...

11. Display Network Interfaces

Exercise: List all network interfaces and their IP addresses.

Command: ip addr

Explanation:

• Shows details about network interfaces, including IP addresses.

Expected Output:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN


group default qlen 1000
inet 127.0.0.1/8 scope host lo
...
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel
state UP group default qlen 1000
inet 192.168.1.10/24 brd 192.168.1.255 scope global dynamic eth0
...

12. Check Connectivity

Exercise: Ping google.com 5 times.

Command: ping -c 5 google.com

Explanation:

• Sends ICMP packets to test connectivity.

Expected Output:

PING google.com (142.250.64.78): 56 data bytes


64 bytes from 142.250.64.78: icmp_seq=0 ttl=118 time=8.52 ms
...

13. View Active Connections

Exercise: Display all active TCP/UDP connections.

Command: netstat -tunap

Explanation:

• Shows active connections with details such as IP addresses and ports.

Expected Output:

Proto Recv-Q Send-Q Local Address Foreign Address State


PID/Program name
...

14. Download a File

Exercise: Download a file from a URL using wget.

Command:

wget https://skipsuniversity.edu.in/wp-
content/uploads/2023/09/skips_uni.jpg

Explanation:

• wget is a command-line utility for downloading files from the web. It supports
protocols such as HTTP, HTTPS, and FTP, making it a versatile tool for retrieving
content from remote servers.
• Downloads the file to the current directory.

Expected Output:

--2025-01-05 12:00:00-- https://example.com/sample.txt


Resolving example.com (example.com)... 93.184.216.34
Connecting to example.com (example.com)|93.184.216.34|:443...
connected.
HTTP request sent, awaiting response... 200 OK
Length: 1024 (1.0K) [text/plain]
Saving to: 'sample.txt'

15. Scan Open Ports

Exercise: Scan open ports on the local machine.

Command: sudo nmap localhost

Explanation:
• Uses nmap to identify open ports on the system.

Expected Output:

Starting Nmap 7.91 ( https://nmap.org ) at 2025-01-05 12:00 UTC


Nmap scan report for localhost (127.0.0.1)
Host is up (0.00017s latency).
Not shown: 999 closed ports
PORT STATE SERVICE
22/tcp open ssh

16. Check DNS Resolution

Exercise: Resolve the IP address of example.com.

Command: nslookup www.skipsuniversity.edu.in

Explanation:

• Queries the DNS server for the domain's IP address.

Expected Output:

Server: 10.255.255.254
Address: 10.255.255.254#53

Non-authoritative answer:
www.skipsuniversity.edu.in canonical name =
skipsuniversity.edu.in.
Name: skipsuniversity.edu.in

17. Set Up a Basic Web Server

Exercise: Use Python to start a simple HTTP server on port 8000. Command:

python3 -m http.server 8000

Explanation:

• Starts a basic web server in the current directory, accessible via a web browser.

Expected Output:

Serving HTTP on 0.0.0.0 port 8000 ...

18. Trace Network Route

Exercise: Trace the route to google.com.

Command: traceroute google.com

Explanation:
• Shows the path packets take to reach the destination.

Expected Output:

traceroute to google.com (142.250.64.78), 30 hops max, 60 byte packets


1 192.168.1.1 (192.168.1.1) 1.123 ms 1.042 ms 1.031 ms
2 10.0.0.1 (10.0.0.1) 10.234 ms 10.213 ms 10.194 ms
...
Part 4: Shell Scripting Practical Assignments

1. Backup Script

Exercise: Write a script to back up the /home directory.

Script:

#!/bin/bash
# This script creates a compressed backup of the /home directory.

tar -czvf /home_backup.tar.gz /home


echo "Backup completed. The file is saved as /home_backup.tar.gz"

Explanation:

• tar -czvf: Compresses and archives files into a .tar.gz format.


• /home: Specifies the directory to back up.

Expected Output:

• The terminal shows the list of files being added to the backup archive.
• The message "Backup completed. The file is saved as /home_backup.tar.gz" is
displayed.

2. System Resource Monitoring Script

Exercise: Write a script to log CPU and memory usage every minute.
Script:

#!/bin/bash
# This script logs CPU and memory usage to system_log.txt every minute.

while true; do
echo "$(date): CPU and Memory Usage" >> system_log.txt
top -b -n1 | head -n5 >> system_log.txt
echo "Logged CPU and Memory usage."
sleep 60
done

Explanation:

• top -b -n1: Displays system resource usage in a non-interactive mode.


• sleep 60: Waits for 60 seconds before repeating.

Expected Output:

• The file system_log.txt is updated with CPU and memory usage every minute.

3. Disk Space Alert

Exercise: Write a script to alert if disk usage exceeds 80%.


Script:

#!/bin/bash
# This script checks if disk usage exceeds 80% and sends an alert.

usage=$(df / | grep / | awk '{print $5}' | sed 's/%//')


if [ $usage -gt 80 ]; then
echo "Disk usage is above 80%!" | mail -s "Disk Alert" [email protected]
else
echo "Disk usage is under control."
fi

Explanation:

• df /: Checks disk usage for the root filesystem.


• mail: Sends an email alert to the administrator.

Expected Output:

• If disk usage is above 80%, an email is sent with the subject "Disk Alert."
• Otherwise, the message "Disk usage is under control." is displayed.

4. Automated Service Restart

Exercise: Write a script to check if nginx is running and restart it if not.

Script:

#!/bin/bash
# This script checks if nginx is running and restarts it if necessary.

if ! systemctl is-active --quiet nginx; then


sudo systemctl restart nginx
echo "Nginx was restarted."
else
echo "Nginx is running."
fi

Explanation:

• systemctl is-active --quiet: Checks if the service is active.


• sudo systemctl restart: Restarts the service if it’s not running.

Expected Output:

• If nginx is down, "Nginx was restarted." is displayed.


• If nginx is running, "Nginx is running." is displayed.

5. System Cleanup Script

Exercise: Write a script to clean up temporary files and logs older than 10 days.

Script:
#!/bin/bash
# This script removes temporary files and logs older than 10 days.

find /tmp -type f -mtime +10 -exec rm -f {} \;


find /var/log -type f -mtime +10 -exec rm -f {} \;
echo "System cleanup completed."

Explanation:

• find: Searches for files older than 10 days (-mtime +10).


• -exec rm -f {}: Deletes the files found.

Expected Output:

• "System cleanup completed." is displayed, and old temporary files and logs are deleted.

6. User Disk Usage Report

Exercise: Write a script to report disk usage for each user.


Script:

#!/bin/bash
# This script displays the disk usage for each user in the /home directory.

for user in $(ls /home); do


echo "Disk usage for $user:"
du -sh /home/$user 2>/dev/null
done

Explanation:

• ls /home: Lists the directories in /home (one per user).


• du -sh: Shows the size of the directory in human-readable format.

Expected Output:

• Disk usage for each user in /home is displayed.

7. Network Connectivity Script

Exercise: Write a script to check if google.com is reachable.

Script:

#!/bin/bash
# This script checks if google.com is reachable.

if ping -c 1 google.com &> /dev/null; then


echo "Google is reachable."
else
echo "Google is not reachable."
fi
Explanation:

• ping -c 1: Sends one ping request to google.com.


• &> /dev/null: Suppresses the output of the ping command.

Expected Output:

• If reachable, "Google is reachable." is displayed.


• If not reachable, "Google is not reachable." is displayed.

8. File Management Script

Exercise: Write a script to archive files older than 7 days in /var/log.

Script:

#!/bin/bash
# This script archives files older than 7 days in /var/log.

find /var/log -type f -mtime +7 -exec tar -rvf old_logs.tar {} \;


echo "Archived old log files into old_logs.tar."

Explanation:

• find: Locates files older than 7 days.


• tar -rvf: Adds files to an existing archive.

Expected Output:

• The file old_logs.tar contains all log files older than 7 days.

You might also like