Linux Fundamentals Lab Manual 3
Linux Fundamentals Lab Manual 3
Privileges
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:
2. Delete a User
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:
3. Switch to Superuser
Command: sudo -i
Explanation:
Expected Output:
root@hostname:~#
Command: uptime
Explanation:
• Shows the current time, system uptime, number of users logged in, and the
system’s load averages.
Expected Output:
Command: ps aux
Explanation:
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:
Expected Output:
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).
Command: free -h
Explanation:
Expected Output:
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.
Explanation:
Explanation:
Expected Output:
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:
Command: htop
Explanation:
Expected Output:
Explanation:
• Searches for files modified within the last 2 days in the /home directory.
Expected Output:
/home/user/document.txt
...
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:
Exercise: Set the default permission mask so new files are created with rw-r--r--.
Command:
umask 022
Explanation:
Expected Output:
Explanation:
• Modifies the sudoers file to allow passwordless sudo access for student.
Expected Output:
Explanation:
Expected Output:
Explanation:
Expected Output:
Expected Output:
groups student
Explanation:
• Extracts usernames from the /etc/passwd file, which stores user information.
Expected Output:
root
student
...
5. Delete a User
Explanation:
• Deletes the user and associated files, including their home directory.
Expected Output:
Explanation:
• Lists the groups associated with the user.
Expected Output:
7. Create a Group
Explanation:
Expected Output:
Explanation:
Expected Output:
Explanation:
Expected Output:
Command: last
Explanation:
Expected Output:
Command: ip addr
Explanation:
Expected Output:
Explanation:
Expected Output:
Explanation:
Expected Output:
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:
Explanation:
• Uses nmap to identify open ports on the system.
Expected Output:
Explanation:
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
Exercise: Use Python to start a simple HTTP server on port 8000. Command:
Explanation:
• Starts a basic web server in the current directory, accessible via a web browser.
Expected Output:
Explanation:
• Shows the path packets take to reach the destination.
Expected Output:
1. Backup Script
Script:
#!/bin/bash
# This script creates a compressed backup of the /home directory.
Explanation:
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.
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:
Expected Output:
• The file system_log.txt is updated with CPU and memory usage every minute.
#!/bin/bash
# This script checks if disk usage exceeds 80% and sends an alert.
Explanation:
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.
Script:
#!/bin/bash
# This script checks if nginx is running and restarts it if necessary.
Explanation:
Expected Output:
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.
Explanation:
Expected Output:
• "System cleanup completed." is displayed, and old temporary files and logs are deleted.
#!/bin/bash
# This script displays the disk usage for each user in the /home directory.
Explanation:
Expected Output:
Script:
#!/bin/bash
# This script checks if google.com is reachable.
Expected Output:
Script:
#!/bin/bash
# This script archives files older than 7 days in /var/log.
Explanation:
Expected Output:
• The file old_logs.tar contains all log files older than 7 days.