> For the complete documentation index, see [llms.txt](https://mrinalghimire.com.np/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://mrinalghimire.com.np/linux-system-administration/linux-system-administration.md).

# Linux System Administration

**Owner:** Mrinal Ghimire\
**Created:** August 4, 2026\
**Document Type:** Linux System Administration

### 1. Introduction

This documentation is a practical Linux system administration guide based on the Linux concepts, commands, and assignments covered so far.

It includes:

* Basic Linux commands
* SSH and SSH keys
* Users and groups
* File ownership and permissions
* `chmod`, `chown`, `usermod`, `gpasswd`
* Special permissions, including **SGID / Setgid**
* Collaborative directories
* Access Control Lists (ACL)
* Services and `systemctl`
* Processes and CPU monitoring
* Memory, swap, and disk management
* Filesystems and `/etc/fstab`
* Networking and listening ports
* UFW firewall
* Cron and crontab
* One-time jobs with `at`
* Time synchronization and Nepal timezone
* Bash scripting basics
* Searching for files and scripts
* `tar`, `gzip`, and `bzip2`
* Web server checks
* Package updates
* LEMP/LAMP-related administration
* Assignment 1 and Assignment 2 solutions
* Verification commands and troubleshooting

{% hint style="info" %}
Commands that modify the system normally require `sudo` or root privileges.
{% endhint %}

## 2. Linux Basics

### 2.1 What is Linux?

Linux is an operating system kernel used by many operating systems called Linux distributions.

Examples:

* Ubuntu
* Debian
* Rocky Linux
* AlmaLinux
* Fedora
* Red Hat Enterprise Linux

A Linux server is commonly managed from the command line.

### 2.2 Terminal and Shell

A terminal provides access to the shell.

The shell interprets commands such as:

```bash
ls
cd
pwd
mkdir
cp
mv
rm
```

A common shell is Bash.

Check the current shell:

```bash
echo $SHELL
```

## 3. Essential Linux Commands

### 3.1 `pwd`

Shows the current working directory.

```bash
pwd
```

Example:

```
/home/ubuntu
```

### 3.2 `ls`

Lists files and directories.

```bash
ls
```

Detailed listing:

```bash
ls -l
```

Show hidden files:

```bash
ls -la
```

Human-readable sizes:

```bash
ls -lh
```

Useful combination:

```bash
ls -lah
```

#### Common options

| Option | Meaning               |
| ------ | --------------------- |
| `-l`   | Long/detailed listing |
| `-a`   | Show hidden files     |
| `-h`   | Human-readable sizes  |

### 3.3 `cd`

Changes directory.

```bash
cd /etc
```

Go to home:

```bash
cd
```

Go to parent directory:

```bash
cd ..
```

Go to previous directory:

```bash
cd -
```

### 3.4 `mkdir`

Creates a directory.

```bash
mkdir test
```

Create parent directories when needed:

```bash
mkdir -p /data/backup
```

### 3.5 `touch`

Creates an empty file.

```bash
touch file.txt
```

### 3.6 `cat`

Displays file contents.

```bash
cat file.txt
```

### 3.7 `less`

Views a large file page by page.

```bash
less /var/log/auth.log
```

### 3.8 `head` and `tail`

Show the beginning or end of a file.

```bash
head file.txt
tail file.txt
```

Follow a log continuously:

```bash
tail -f /var/log/auth.log
```

### 3.9 `cp`

Copies files or directories.

```bash
cp file.txt /tmp/
```

Copy a directory recursively:

```bash
cp -r directory /tmp/
```

### 3.10 `mv`

Moves or renames files.

```bash
mv old.txt new.txt
```

### 3.11 `rm`

Removes files.

```bash
rm file.txt
```

Remove a directory recursively:

```bash
rm -r directory
```

{% hint style="warning" %}
Use recursive deletion carefully.
{% endhint %}

### 3.12 `echo`

Prints text.

```bash
echo "Hello"
```

Write to a file:

```bash
echo "Hello" > file.txt
```

Append:

```bash
echo "Another line" >> file.txt
```

`>` overwrites the file.

`>>` appends to the file.

## 4. Finding Information About the System

### 4.1 CPU cores

```bash
nproc
```

`nproc` displays the number of available processing units.

More detailed CPU information:

```bash
lscpu
```

### 4.2 Memory

```bash
free -h
```

Useful fields include:

* Total
* Used
* Free
* Available
* Swap

### 4.3 Disk space

```bash
df -h
```

This shows filesystem disk usage.

Example:

```
Filesystem      Size  Used Avail Use%
/dev/sda2        20G   8G   11G  43%
```

### 4.4 Disk partitions

```bash
lsblk
```

Detailed partition information:

```bash
sudo fdisk -l
```

### 4.5 Private IP address

```bash
ip addr
```

A concise command:

```bash
ip -4 addr
```

Another useful command:

```bash
hostname -I
```

### 4.6 Hostname

```bash
hostname
```

Detailed hostname information:

```bash
hostnamectl
```

### 4.7 Operating system

```bash
cat /etc/os-release
```

### 4.8 Kernel version

```bash
uname -r
```

Complete system information:

```bash
uname -a
```

## 5. File and Directory Permissions

Linux permissions are based on three classes:

1. Owner
2. Group
3. Others

Example:

```
-rwxr-xr--
```

Breakdown:

```
- rwx r-x r--
  │   │   │
  │   │   └── Others
  │   └────── Group
  └────────── Owner
```

Permission values:

| Permission | Symbol | Value |
| ---------- | ------ | ----- |
| Read       | `r`    | 4     |
| Write      | `w`    | 2     |
| Execute    | `x`    | 1     |

### 5.1 Numeric permissions

For example:

```bash
chmod 755 script.sh
```

Means:

* Owner: `7` = `rwx`
* Group: `5` = `r-x`
* Others: `5` = `r-x`

Another example:

```bash
chmod 644 file.txt
```

Means:

* Owner: read + write
* Group: read
* Others: read

### 5.2 Why `chmod 600`?

```bash
chmod 600 /swapfile
```

Means:

```
Owner  = read + write
Group  = no permissions
Others = no permissions
```

A swap file should not be accessible to normal users.

### 5.3 Symbolic permissions

Add execute permission:

```bash
chmod +x script.sh
```

Remove write permission from others:

```bash
chmod o-w file.txt
```

Give group write permission:

```bash
chmod g+w file.txt
```

## 6. Ownership: `chown`

Check ownership:

```bash
ls -l file.txt
```

Change owner:

```bash
sudo chown alice file.txt
```

Change owner and group:

```bash
sudo chown alice:developers file.txt
```

Change only group:

```bash
sudo chown :developers file.txt
```

Recursive ownership change:

```bash
sudo chown -R alice:developers /data
```

{% hint style="warning" %}
Use `-R` carefully.
{% endhint %}

## 7. Users and Groups

### 7.1 View users

```bash
cat /etc/passwd
```

Search for a specific user:

```bash
grep ubuntu /etc/passwd
```

### 7.2 Create a user

```bash
sudo useradd username
```

Create a user with a home directory:

```bash
sudo useradd -m username
```

Create a user with Bash:

```bash
sudo useradd -m -s /bin/bash username
```

### 7.3 Set a password

```bash
sudo passwd username
```

### 7.4 Create a group

```bash
sudo groupadd testing
```

### 7.5 Add a user to a secondary group

Recommended method:

```bash
sudo usermod -aG testing natasha
```

Important:

* `-a` = append
* `-G` = supplementary/secondary groups

{% hint style="warning" %}
Without `-a`, existing supplementary group memberships can be replaced.
{% endhint %}

### 7.6 `gpasswd`

Another way to add a user to a group:

```bash
sudo gpasswd -a natasha testing
```

### 7.7 Check group membership

```bash
groups natasha
```

Or:

```bash
id natasha
```

## 8. User Password and Account Status

Check password status:

```bash
sudo passwd -S ubuntu
```

Check password aging and account expiry:

```bash
sudo chage -l ubuntu
```

`chage -l` can show:

* Last password change
* Password expiration
* Password inactive period
* Account expiration
* Minimum password age
* Maximum password age

## 9. Non-Interactive User

If a user should not have an interactive login shell:

```bash
sudo useradd -m -s /usr/sbin/nologin sarah
```

Check the shell:

```bash
getent passwd sarah
```

A `nologin` shell prevents normal interactive shell access.

## 10. Setgid / SGID

### 10.1 What is Setgid?

Setgid on a directory makes newly created files and directories inherit the directory's group ownership.

This is useful for collaborative directories.

For example:

```
/home/admins
Group: testing
```

If the directory has SGID, files created inside it inherit:

```
testing
```

as their group.

### 10.2 Create a collaborative directory

{% stepper %}
{% step %}

#### Create group

```bash
sudo groupadd testing
```

{% endstep %}

{% step %}

#### Create users

```bash
sudo useradd -m natasha
sudo useradd -m harry
```

{% endstep %}

{% step %}

#### Add users to the group

```bash
sudo usermod -aG testing natasha
sudo usermod -aG testing harry
```

{% endstep %}

{% step %}

#### Create directory

```bash
sudo mkdir -p /home/admins
```

{% endstep %}

{% step %}

#### Set group ownership

```bash
sudo chown root:testing /home/admins
```

{% endstep %}

{% step %}

#### Set permissions and SGID

```bash
sudo chmod 2770 /home/admins
```

The leading `2` enables SGID.
{% endstep %}

{% step %}

#### Check

```bash
ls -ld /home/admins
```

You may see:

```
drwxrws---
```

The `s` in the group permission position indicates SGID.
{% endstep %}
{% endstepper %}

## 11. Understanding `chmod 2770`

```
2 7 7 0
│ │ │ └── Others: no access
│ │ └──── Group: rwx
│ └────── Owner: rwx
└──────── SGID
```

Therefore:

```bash
chmod 2770 /home/admins
```

means:

* Owner: full access
* Group: full access
* Others: no access
* SGID: enabled

## 12. ACL - Access Control Lists

Normal Linux permissions provide owner/group/other access.

ACLs allow more specific permissions for individual users.

Check ACL:

```bash
getfacl file.txt
```

Set an ACL:

```bash
sudo setfacl -m u:natasha:rw /var/tmp/fstab
```

Remove an ACL:

```bash
sudo setfacl -x u:natasha /var/tmp/fstab
```

View ACL:

```bash
getfacl /var/tmp/fstab
```

{% hint style="info" %}
ACL is especially useful when the normal owner/group/other permission model cannot express the required access.
{% endhint %}

## 13. Processes and Services

### 13.1 View processes

```bash
ps aux
```

Search for a process:

```bash
ps aux | grep nginx
```

### 13.2 `top`

```bash
top
```

Shows:

* CPU usage
* Memory usage
* Load average
* Processes
* System uptime

### 13.3 `htop`

If installed:

```bash
htop
```

It provides an interactive process monitor.

## 14. CPU Usage and Load Average

Check CPU usage:

```bash
top
```

or:

```bash
htop
```

Load average:

```bash
uptime
```

Example:

```
load average: 0.20, 0.15, 0.10
```

These normally represent approximately:

1. 1-minute load
2. 5-minute load
3. 15-minute load

#### Factors to consider when analyzing CPU load

* Number of CPU cores
* CPU utilization
* Load average
* Running processes
* I/O wait
* Memory pressure
* Swap activity
* Disk performance
* Network activity
* Processes consuming CPU

A load of `2` has a different meaning on a 2-core machine than on a 16-core machine.

## 15. System Services and `systemctl`

List services:

```bash
systemctl list-units --type=service
```

Check a service:

```bash
systemctl status nginx
```

Start:

```bash
sudo systemctl start nginx
```

Stop:

```bash
sudo systemctl stop nginx
```

Restart:

```bash
sudo systemctl restart nginx
```

Reload configuration:

```bash
sudo systemctl reload nginx
```

Enable at boot:

```bash
sudo systemctl enable nginx
```

Enable and start immediately:

```bash
sudo systemctl enable --now nginx
```

## 16. What is a Daemon?

A daemon is a background service/process that normally provides a service without direct user interaction.

Examples:

* `sshd` - SSH service
* `cron` - scheduled jobs
* `nginx` - web server
* `atd` - one-time scheduled jobs

## 17. SSH

### 17.1 Connect to a server

```bash
ssh ubuntu@192.168.122.160
```

General format:

```bash
ssh username@server-ip
```

### 17.2 SSH keys

Generate an RSA key:

```bash
ssh-keygen -t rsa
```

Default files commonly include:

```
~/.ssh/id_rsa
~/.ssh/id_rsa.pub
```

* Private key: keep secret
* Public key: can be installed on the server

### 17.3 Copy a public key

```bash
ssh-copy-id username@server-ip
```

Then connect:

```bash
ssh username@server-ip
```

The server uses the public key in:

```
~/.ssh/authorized_keys
```

### 17.4 SSH configuration

Common configuration file:

```bash
/etc/ssh/sshd_config
```

Check authentication settings:

```bash
sudo grep -E "PasswordAuthentication|PubkeyAuthentication" /etc/ssh/sshd_config
```

After changing SSH configuration, validate and reload/restart carefully:

```bash
sudo sshd -t
sudo systemctl restart ssh
```

{% hint style="warning" %}
Keep an existing SSH session open while testing a new configuration.
{% endhint %}

## 18. Networking

### 18.1 IP address

```bash
ip addr
```

IPv4:

```bash
ip -4 addr
```

### 18.2 Routing table

```bash
ip route
```

### 18.3 DNS information

```bash
resolvectl status
```

DNS normally uses port:

```
53
```

### 18.4 Listening ports

Use:

```bash
sudo ss -tulnp
```

Useful options:

| Option | Meaning                 |
| ------ | ----------------------- |
| `-t`   | TCP                     |
| `-u`   | UDP                     |
| `-l`   | Listening               |
| `-n`   | Numeric addresses/ports |
| `-p`   | Process information     |

Check port 80:

```bash
sudo ss -tulnp | grep :80
```

## 19. Understanding `UNCONN`

When using:

```bash
ss -tulnp
```

UDP sockets may display:

```
UNCONN
```

This means the UDP socket is not connected to one specific remote endpoint.

It does not automatically mean the service is broken.

## 20. Web Server Check

Check whether port 80 is listening:

```bash
sudo ss -tlnp | grep :80
```

Check HTTP locally:

```bash
curl -I http://127.0.0.1
```

Get the page content:

```bash
curl http://127.0.0.1
```

Check Nginx:

```bash
systemctl status nginx
```

Check Apache:

```bash
systemctl status apache2
```

## 21. UFW Firewall

Check status:

```bash
sudo ufw status
```

Enable UFW:

```bash
sudo ufw enable
```

Allow SSH:

```bash
sudo ufw allow ssh
```

Allow HTTP:

```bash
sudo ufw allow 80/tcp
```

Allow HTTPS:

```bash
sudo ufw allow 443/tcp
```

Allow SSH only from a specific server:

```bash
sudo ufw allow from SERVER_IP to any port 22 proto tcp
```

Deny traffic:

```bash
sudo ufw deny PORT
```

Check numbered rules:

```bash
sudo ufw status numbered
```

## 22. Cron

### 22.1 What is Cron?

Cron is used to run commands or scripts automatically on a recurring schedule.

Check whether cron is installed:

```bash
dpkg -s cron
```

Check service:

```bash
systemctl status cron
```

### 22.2 Crontab

Edit the current user's crontab:

```bash
crontab -e
```

List jobs:

```bash
crontab -l
```

System-wide cron locations include:

```
/etc/crontab
/etc/cron.d/
/etc/cron.daily/
/etc/cron.hourly/
/etc/cron.weekly/
/etc/cron.monthly/
```

### 22.3 Cron format

```
minute hour day-of-month month day-of-week command
```

Example:

```
23 14 * * * /bin/echo hello
```

This means:

* Minute: 23
* Hour: 14
* Every day
* Every month
* Every weekday

Therefore it runs every day at:

```
14:23
```

### 22.4 Every 2 minutes

```
*/2 * * * * command
```

## 23. `at` - One-Time Scheduling

Cron is for recurring jobs.

`at` is for one-time jobs.

Check service:

```bash
systemctl status atd
```

Enable and start:

```bash
sudo systemctl enable --now atd
```

Schedule a one-time command:

```bash
echo "mkdir -p ~/coll" | at now + 3 minutes
```

View jobs:

```bash
atq
```

Remove a job:

```bash
atrm JOB_ID
```

## 24. Timezone and Time Synchronization

For Nepal, the timezone is:

```
Asia/Kathmandu
```

Check current time:

```bash
date
```

Check timezone:

```bash
timedatectl
```

Set timezone:

```bash
sudo timedatectl set-timezone Asia/Kathmandu
```

Check synchronization:

```bash
timedatectl status
```

If using Chrony:

```bash
chronyc tracking
```

View time sources:

```bash
chronyc sources
```

{% hint style="info" %}
Do not repeatedly correct server time manually. Configure the system timezone and a network time synchronization service such as Chrony/NTP.
{% endhint %}

## 25. System Boot Time

Check when the system started:

```bash
uptime -s
```

Also:

```bash
uptime
```

## 26. Package Updates

Check package updates:

```bash
sudo apt update
```

Upgrade installed packages:

```bash
sudo apt upgrade
```

A common full maintenance sequence:

```bash
sudo apt update
sudo apt upgrade
```

Check installed package information:

```bash
apt list --upgradable
```

## 27. Disk and Filesystem Management

### 27.1 Disk usage

```bash
df -h
```

Directory size:

```bash
du -sh /data
```

Find large directories:

```bash
du -sh /* 2>/dev/null
```

### 27.2 Block devices

```bash
lsblk
```

## 28. Swap Memory

Swap is disk space used as additional virtual memory when RAM pressure occurs.

Check swap:

```bash
free -h
```

or:

```bash
swapon --show
```

### 28.1 Create a swap file

{% stepper %}
{% step %}

#### Create the swap file

```bash
sudo fallocate -l 2G /swapfile
```

{% endstep %}

{% step %}

#### Set secure permissions

```bash
sudo chmod 600 /swapfile
```

{% endstep %}

{% step %}

#### Format as swap

```bash
sudo mkswap /swapfile
```

{% endstep %}

{% step %}

#### Enable swap

```bash
sudo swapon /swapfile
```

{% endstep %}

{% step %}

#### Verify

```bash
swapon --show
```

{% endstep %}
{% endstepper %}

### 28.2 Make swap permanent

Edit:

```bash
sudo nano /etc/fstab
```

Add:

```
/swapfile none swap sw 0 0
```

Meaning:

* `/swapfile` = swap file
* `none` = no mount point
* `swap` = filesystem type
* `sw` = swap options
* `0 0` = dump and fsck fields

### 28.3 Swap priority

Check:

```bash
swapon --show
```

A priority value influences which swap device is preferred.

Higher priority is preferred over lower priority.

### 28.4 Swappiness

Check:

```bash
cat /proc/sys/vm/swappiness
```

Temporarily change:

```bash
sudo sysctl vm.swappiness=10
```

## 29. `/etc/fstab`

`/etc/fstab` contains filesystem and mount configuration.

View:

```bash
cat /etc/fstab
```

Edit:

```bash
sudo nano /etc/fstab
```

{% hint style="warning" %}
An incorrect `/etc/fstab` entry can affect booting.
{% endhint %}

After changing it, test mounts where appropriate:

```bash
sudo mount -a
```

If there is an error, fix it before rebooting.

## 30. File Searching

Find all shell scripts:

```bash
find / -type f -name "*.sh" 2>/dev/null
```

Explanation:

* `find /` = search from root
* `-type f` = regular files
* `-name "*.sh"` = filenames ending in `.sh`
* `2>/dev/null` = hide permission-denied errors

## 31. `grep`

Search text:

```bash
grep "hello" file.txt
```

Case-insensitive:

```bash
grep -i "hello" file.txt
```

Recursive:

```bash
grep -R "PasswordAuthentication" /etc/ssh/
```

Use regular expressions:

```bash
grep -E "PasswordAuthentication|PubkeyAuthentication" /etc/ssh/sshd_config
```

## 32. Redirection

Overwrite:

```bash
command > output.txt
```

Append:

```bash
command >> output.txt
```

Redirect errors:

```bash
command 2>/dev/null
```

Redirect standard output and errors:

```bash
command > output.txt 2>&1
```

## 33. Pipes

A pipe sends the output of one command into another command.

```bash
ps aux | grep nginx
```

Another example:

```bash
ss -tulnp | grep :80
```

## 34. Tar Archives

### 34.1 Create a gzip-compressed tar archive

```bash
tar -czf /backup/data.tar.gz /data
```

Meaning:

* `c` = create
* `z` = gzip
* `f` = filename

### 34.2 List gzip archive contents

```bash
tar -tzf /backup/data.tar.gz
```

### 34.3 Extract gzip archive

```bash
tar -xzf /backup/data.tar.gz
```

Meaning:

* `x` = extract
* `z` = gzip
* `f` = file

## 35. Bzip2 Compression

Create a bzip2-compressed archive:

```bash
tar -cjf /var/tmp/tmp.tar.bz2 /tmp
```

Options:

* `c` = create
* `j` = bzip2
* `f` = archive filename

List contents:

```bash
tar -tjf /var/tmp/tmp.tar.bz2
```

Extract:

```bash
tar -xjf /var/tmp/tmp.tar.bz2
```

## 36. Tar Option Summary

| Command    | Purpose                 |
| ---------- | ----------------------- |
| `tar -cf`  | Create uncompressed tar |
| `tar -czf` | Create gzip tar         |
| `tar -cjf` | Create bzip2 tar        |
| `tar -xf`  | Extract tar             |
| `tar -xzf` | Extract gzip tar        |
| `tar -xjf` | Extract bzip2 tar       |
| `tar -tf`  | List tar contents       |
| `tar -tzf` | List gzip archive       |
| `tar -tjf` | List bzip2 archive      |

## 37. gzip

Compress a file:

```bash
gzip file.txt
```

This normally creates:

```
file.txt.gz
```

Decompress:

```bash
gunzip file.txt.gz
```

## 38. bzip2

Compress:

```bash
bzip2 file.txt
```

Decompress:

```bash
bunzip2 file.txt.bz2
```

## 39. Bash Scripting

Create a script:

```bash
nano script.sh
```

Start with:

```bash
#!/bin/bash
```

Make executable:

```bash
chmod +x script.sh
```

Run:

```bash
./script.sh
```

Or:

```bash
bash script.sh
```

### 39.1 Variables

```bash
a=100
b=500

echo "$a"
echo "$b"
```

Do not put spaces around `=` in Bash variable assignment.

### 39.2 User input

```bash
#!/bin/bash

echo -n "Enter a number: "
read number

echo "You entered: $number"
```

`echo -n` prevents the cursor from moving to the next line.

### 39.3 Conditions

Example:

```bash
if [ "$a" -eq "$b" ]; then
    echo "Numbers are equal"
else
    echo "Numbers are not equal"
fi
```

Common numeric operators:

| Operator | Meaning               |
| -------- | --------------------- |
| `-eq`    | Equal                 |
| `-ne`    | Not equal             |
| `-gt`    | Greater than          |
| `-lt`    | Less than             |
| `-ge`    | Greater than or equal |
| `-le`    | Less than or equal    |

## 40. Prime Number Logic

A number is prime if it is greater than 1 and has no divisors other than 1 and itself.

A basic Bash approach:

```bash
#!/bin/bash

read -p "Enter a number: " n

if [ "$n" -lt 2 ]; then
    echo "Not prime"
    exit
fi

for ((i=2; i*i<=n; i++)); do
    if [ $((n % i)) -eq 0 ]; then
        echo "Not prime"
        exit
    fi
done

echo "Prime"
```

## 41. Assignment 1 - System Discovery

### Question 1: SSH to the server

Example:

```bash
ssh ubuntu@SERVER_IP
```

Using an identity file:

```bash
ssh -i ~/.ssh/id_rsa ubuntu@SERVER_IP
```

### Question 2: System information

#### CPU core

```bash
nproc
```

#### Memory

```bash
free -h
```

#### Total disk space

```bash
df -h
```

#### Disk partitions

```bash
lsblk
```

#### Private IP

```bash
hostname -I
```

#### Hostname

```bash
hostname
```

#### OS name

```bash
cat /etc/os-release
```

#### Kernel version

```bash
uname -r
```

## 42. Assignment 1 - Cron

Check installation:

```bash
dpkg -s cron
```

Check service:

```bash
systemctl status cron
```

Purpose:

{% hint style="info" %}
Cron automatically executes commands and scripts according to a recurring schedule.
{% endhint %}

## 43. Assignment 1 - Listening Ports

Run:

```bash
sudo ss -tulnp
```

This identifies listening TCP/UDP ports and associated processes.

## 44. Assignment 1 - Web Server

Check port 80:

```bash
sudo ss -tlnp | grep :80
```

Test locally:

```bash
curl -I http://127.0.0.1
```

Check Nginx:

```bash
systemctl status nginx
```

## 45. Assignment 1 - Users

List users:

```bash
cat /etc/passwd
```

Human users can also be investigated with:

```bash
getent passwd
```

Check a particular user:

```bash
id username
```

## 46. Assignment 1 - Ubuntu Password Status

```bash
sudo passwd -S ubuntu
```

Detailed password aging:

```bash
sudo chage -l ubuntu
```

## 47. Assignment 1 - Find `.sh` Files

```bash
find / -type f -name "*.sh" 2>/dev/null
```

## 48. Assignment 1 - Correct Nepal Time Permanently

Do not repeatedly set the clock manually.

Set timezone:

```bash
sudo timedatectl set-timezone Asia/Kathmandu
```

Check:

```bash
timedatectl
```

If using Chrony:

```bash
chronyc tracking
chronyc sources
```

The objective is to use automatic network time synchronization so the clock remains accurate.

## 49. Assignment 1 - Backup Script

Find shell scripts:

```bash
find / -type f -name "*.sh" 2>/dev/null
```

Search for scripts referring to `/data`:

```bash
grep -R "/data" /etc /usr/local /home 2>/dev/null
```

A simple backup using tar:

```bash
tar -czf /backup/data.tar.gz /data
```

For a recurring backup, place the command in a script and schedule it with cron.

Example script:

```bash
#!/bin/bash

mkdir -p /backup
tar -czf /backup/data-$(date +%F).tar.gz /data
```

Make executable:

```bash
chmod +x backup.sh
```

## 50. Assignment 1 - CPU Usage

Use:

```bash
top
```

or:

```bash
htop
```

Check load:

```bash
uptime
```

Analyze:

* CPU utilization
* Load average
* CPU core count
* High CPU processes
* I/O wait
* Memory and swap
* Disk activity

## 51. Assignment 1 - Last Boot

```bash
uptime -s
```

Alternative:

```bash
who -b
```

## 52. Assignment 1 - Check Whether Server Is Up to Date

```bash
sudo apt update
```

Then:

```bash
apt list --upgradable
```

Upgrade:

```bash
sudo apt upgrade
```

## 53. Assignment 1 - `/test` Permissions

Inspect:

```bash
ls -ld /test
```

Inspect contents:

```bash
ls -la /test
```

Check ownership:

```bash
stat /test
```

The answer to "who can do what" depends on:

* Owner
* Group
* Others
* Permission bits
* ACL entries
* SGID/sticky-bit settings

For ACL information:

```bash
getfacl /test
```

## 54. Assignment 1 - Save Revision Document

Create directory:

```bash
mkdir -p /home/ubuntu/Mrinal
```

Create file:

```bash
nano /home/ubuntu/Mrinal/revision.txt
```

Verify:

```bash
ls -l /home/ubuntu/Mrinal/revision.txt
```

## 55. Assignment 2 - Create the `testing` Group

```bash
sudo groupadd testing
```

Verify:

```bash
getent group testing
```

## 56. Assignment 2 - Create Natasha

```bash
sudo useradd -m natasha
sudo passwd natasha
```

Set the requested password when prompted.

Add to testing as a secondary group:

```bash
sudo usermod -aG testing natasha
```

Verify:

```bash
id natasha
```

## 57. Assignment 2 - Create Harry

```bash
sudo useradd -m harry
sudo passwd harry
```

Add to testing:

```bash
sudo usermod -aG testing harry
```

Verify:

```bash
id harry
```

## 58. Assignment 2 - Create Sarah Without Interactive Shell

```bash
sudo useradd -m -s /usr/sbin/nologin sarah
sudo passwd sarah
```

Do not add Sarah to `testing`.

Verify:

```bash
id sarah
getent passwd sarah
```

## 59. Assignment 2 - Collaborative `/home/admins`

{% stepper %}
{% step %}

#### Create

```bash
sudo mkdir -p /home/admins
```

{% endstep %}

{% step %}

#### Set group

```bash
sudo chown root:testing /home/admins
```

{% endstep %}

{% step %}

#### Set permissions and SGID

```bash
sudo chmod 2770 /home/admins
```

{% endstep %}

{% step %}

#### Verify

```bash
ls -ld /home/admins
```

Expected concept:

```
owner: root
group: testing
permissions: rwxrws---
```

The SGID bit causes new files to inherit the `testing` group.
{% endstep %}
{% endstepper %}

## 60. Assignment 2 - `/var/tmp/fstab`

Copy:

```bash
sudo cp /etc/fstab /var/tmp/fstab
```

Set ownership:

```bash
sudo chown root:root /var/tmp/fstab
```

Remove executable permission:

```bash
sudo chmod 644 /var/tmp/fstab
```

This provides:

* root: read/write
* root group: read
* others: read

The requirement that Natasha can read/write while Harry cannot read/write cannot be achieved using only basic owner/group/other permissions while also preserving the "all other users can read" requirement.

An ACL is therefore appropriate.

Give Natasha read/write:

```bash
sudo setfacl -m u:natasha:rw /var/tmp/fstab
```

Deny Harry:

```bash
sudo setfacl -m u:harry:--- /var/tmp/fstab
```

Verify:

```bash
getfacl /var/tmp/fstab
```

{% hint style="info" %}
An explicit ACL entry can override the effective permissions that would otherwise be obtained from the basic owner/group/other model.
{% endhint %}

## 61. Assignment 2 - Natasha Cron Job

Switch to Natasha:

```bash
su - natasha
```

Edit crontab:

```bash
crontab -e
```

Add:

```
23 14 * * * /bin/echo hello
```

Verify:

```bash
crontab -l
```

The job runs every day at 14:23 according to the system's configured local timezone.

## 62. Assignment 2 - Bzip2 Archive

Create:

```bash
sudo tar -cjf /var/tmp/tmp.tar.bz2 /tmp
```

Check file:

```bash
ls -lh /var/tmp/tmp.tar.bz2
```

List contents:

```bash
tar -tjf /var/tmp/tmp.tar.bz2
```

Extract if needed:

```bash
tar -xjf /var/tmp/tmp.tar.bz2
```

## 63. Cron vs `at`

| Feature        | Cron                 | at                 |
| -------------- | -------------------- | ------------------ |
| Recurring jobs | Yes                  | No                 |
| One-time jobs  | Not its main purpose | Yes                |
| Example        | Every 2 minutes      | 3 minutes from now |
| Service        | `cron`               | `atd`              |

Examples:

{% tabs %}
{% tab title="Cron" %}

```
*/2 * * * * /path/script.sh
```

{% endtab %}

{% tab title="at" %}

```bash
echo "/path/script.sh" | at now + 3 minutes
```

{% endtab %}
{% endtabs %}

## 64. Useful Log Files

Authentication log:

```bash
sudo less /var/log/auth.log
```

System logs:

```bash
sudo journalctl
```

Service-specific logs:

```bash
sudo journalctl -u nginx
```

Follow logs:

```bash
sudo journalctl -f
```

## 65. `journalctl`

View recent boot logs:

```bash
journalctl -b
```

View previous boot:

```bash
journalctl -b -1
```

View a service:

```bash
journalctl -u ssh
```

View recent logs:

```bash
journalctl -n 50
```

## 66. Common Package Commands

Search package:

```bash
apt search nginx
```

Install:

```bash
sudo apt install nginx
```

Remove:

```bash
sudo apt remove nginx
```

Show package information:

```bash
apt show nginx
```

## 67. LEMP Stack Context

LEMP generally consists of:

* **Linux**
* **Nginx**
* **MySQL/MariaDB**
* **PHP/PHP-FPM**

Typical architecture:

```
Client
  |
  v
Nginx
  |
  v
PHP-FPM
  |
  v
MySQL/MariaDB
```

WordPress commonly uses:

```
Nginx + PHP-FPM + MySQL/MariaDB
```

phpMyAdmin provides a web interface for managing MySQL/MariaDB databases.

## 68. Nginx Basic Administration

Check version:

```bash
nginx -v
```

Test configuration:

```bash
sudo nginx -t
```

Start:

```bash
sudo systemctl start nginx
```

Enable:

```bash
sudo systemctl enable nginx
```

Restart:

```bash
sudo systemctl restart nginx
```

Reload:

```bash
sudo systemctl reload nginx
```

Check:

```bash
systemctl status nginx
```

## 69. PHP-FPM

Check PHP version:

```bash
php -v
```

Find PHP-FPM service:

```bash
systemctl list-units --type=service | grep php
```

Check installed PHP packages:

```bash
dpkg -l | grep php
```

## 70. MariaDB / MySQL

Check service:

```bash
systemctl status mariadb
```

Check listening port:

```bash
sudo ss -tulnp | grep 3306
```

Port:

```
3306
```

## 71. WordPress Administration Concepts

Typical WordPress components:

```
Nginx
   |
PHP-FPM
   |
WordPress
   |
MariaDB/MySQL
```

WordPress files are commonly placed under a web root such as:

```
/var/www/html/
```

Configuration file:

```
/var/www/html/wordpress/wp-config.php
```

Ownership should be configured carefully so the web server can access required files without giving unnecessary write access.

## 72. Common Troubleshooting Workflow

When a web service does not work, check in this order:

{% stepper %}
{% step %}

#### Is the service running?

```bash
systemctl status nginx
```

{% endstep %}

{% step %}

#### Is the port listening?

```bash
sudo ss -tlnp | grep :80
```

{% endstep %}

{% step %}

#### Does localhost respond?

```bash
curl -I http://127.0.0.1
```

{% endstep %}

{% step %}

#### Is the configuration valid?

```bash
sudo nginx -t
```

{% endstep %}

{% step %}

#### Check logs

```bash
sudo journalctl -u nginx
```

or:

```bash
sudo tail -f /var/log/nginx/error.log
```

{% endstep %}

{% step %}

#### Check firewall

```bash
sudo ufw status
```

{% endstep %}
{% endstepper %}

## 73. Common Permission Troubleshooting

If a user cannot access a file:

{% stepper %}
{% step %}

#### Check ownership

```bash
ls -l file
```

{% endstep %}

{% step %}

#### Check directory permissions

```bash
ls -ld /path/to/directory
```

{% endstep %}

{% step %}

#### Check ACL

```bash
getfacl file
```

{% endstep %}

{% step %}

#### Check user groups

```bash
id username
```

{% endstep %}

{% step %}

#### Check parent directories

A user needs appropriate execute (`x`) permission on directories along the path.
{% endstep %}
{% endstepper %}

## 74. Important Permission Concepts

### Read on a file

Allows reading its contents.

### Write on a file

Allows changing its contents.

### Execute on a file

Allows executing it as a program/script.

### Read on a directory

Allows listing directory entries.

### Write on a directory

Allows creating/deleting/renaming entries, subject to other permissions and special bits.

### Execute on a directory

Allows accessing/traversing entries when the relevant permissions also allow it.

## 75. SGID vs ACL

| Feature                   | SGID              | ACL                            |
| ------------------------- | ----------------- | ------------------------------ |
| Main purpose              | Group inheritance | Fine-grained access            |
| Directory use             | Very common       | Also possible                  |
| New files inherit group   | Yes               | Not the primary purpose        |
| Individual user access    | No                | Yes                            |
| Collaborative directories | Excellent         | Useful                         |
| Example                   | `/home/admins`    | Natasha/Harry file permissions |

They can be used together.

## 76. Sticky Bit

The sticky bit is commonly used on shared directories such as `/tmp`.

Check:

```bash
ls -ld /tmp
```

You may see:

```
drwxrwxrwt
```

The `t` represents the sticky bit.

It restricts users from deleting/renaming files owned by other users in the directory, subject to root and directory ownership rules.

Set it:

```bash
chmod +t directory
```

## 77. Special Permission Summary

| Permission | Numeric | Typical use                                    |
| ---------- | ------- | ---------------------------------------------- |
| SUID       | 4xxx    | Execute file with owner's effective privileges |
| SGID       | 2xxx    | Group inheritance on directories               |
| Sticky     | 1xxx    | Restrict deletion in shared directories        |

Example:

```bash
chmod 2770 /home/admins
```

## 78. File Descriptor and Standard Streams

Linux commands use standard streams:

```
stdin   = 0
stdout  = 1
stderr  = 2
```

Example:

```bash
command > output.txt
```

Redirects stdout.

```bash
command 2> error.txt
```

Redirects stderr.

```bash
command > output.txt 2>&1
```

Redirects both stdout and stderr.

## 79. Useful Command Reference

| Task                | Command                    |
| ------------------- | -------------------------- |
| Current directory   | `pwd`                      |
| List files          | `ls -la`                   |
| CPU cores           | `nproc`                    |
| CPU details         | `lscpu`                    |
| Memory              | `free -h`                  |
| Disk usage          | `df -h`                    |
| Directory size      | `du -sh`                   |
| Partitions          | `lsblk`                    |
| IP address          | `ip addr`                  |
| Routing             | `ip route`                 |
| Hostname            | `hostname`                 |
| OS                  | `cat /etc/os-release`      |
| Kernel              | `uname -r`                 |
| Processes           | `ps aux`                   |
| CPU monitor         | `top`                      |
| Interactive monitor | `htop`                     |
| Listening ports     | `ss -tulnp`                |
| Service status      | `systemctl status SERVICE` |
| Logs                | `journalctl`               |
| Users               | `cat /etc/passwd`          |
| Groups              | `getent group`             |
| User information    | `id USER`                  |
| Password aging      | `chage -l USER`            |
| Find files          | `find`                     |
| Search text         | `grep`                     |
| Cron jobs           | `crontab -l`               |
| Edit cron           | `crontab -e`               |
| One-time jobs       | `at`                       |
| Tar gzip            | `tar -czf`                 |
| Tar bzip2           | `tar -cjf`                 |
| ACL                 | `getfacl`                  |
| Set ACL             | `setfacl`                  |
| Disk swap           | `swapon --show`            |
| Timezone            | `timedatectl`              |

## 80. Recommended Learning Order

For Linux system administration, study these topics in this order:

{% stepper %}
{% step %}

### Stage 1 - Linux Fundamentals

1. Terminal and shell
2. `pwd`
3. `ls`
4. `cd`
5. `mkdir`
6. `touch`
7. `cp`
8. `mv`
9. `rm`
10. `cat`
11. `less`
12. `head`
13. `tail`
14. `echo`
    {% endstep %}

{% step %}

### Stage 2 - Files and Permissions

15. `ls -l`
16. Owner/group/others
17. `chmod`
18. `chown`
19. `chgrp`
20. Numeric permissions
21. SGID
22. Sticky bit
23. ACL
    {% endstep %}

{% step %}

### Stage 3 - Users and Groups

24. `/etc/passwd`
25. `/etc/group`
26. `useradd`
27. `passwd`
28. `usermod`
29. `groupadd`
30. `gpasswd`
31. `id`
32. `groups`
33. `chage`
34. Non-interactive users
    {% endstep %}

{% step %}

### Stage 4 - Processes and Services

35. `ps`
36. `top`
37. `htop`
38. `systemctl`
39. `journalctl`
40. Daemons/services
    {% endstep %}

{% step %}

### Stage 5 - Storage

41. `df`
42. `du`
43. `lsblk`
44. Partitions
45. Filesystems
46. `/etc/fstab`
47. Swap
    {% endstep %}

{% step %}

### Stage 6 - Networking

48. `ip addr`
49. `ip route`
50. DNS
51. Ports
52. `ss`
53. SSH
54. SSH keys
55. UFW
    {% endstep %}

{% step %}

### Stage 7 - Scheduling

56. Cron
57. Crontab
58. `at`
59. `atd`
    {% endstep %}

{% step %}

### Stage 8 - Archives

60. `tar`
61. gzip
62. bzip2
63. Compression
64. Extraction
65. Backup concepts
    {% endstep %}

{% step %}

### Stage 9 - Bash Scripting

66. Variables
67. Input
68. Conditions
69. Loops
70. Arithmetic
71. Exit status
72. Functions
73. Backup scripts
74. Automation
    {% endstep %}

{% step %}

### Stage 10 - System Administration

75. Package management
76. Time synchronization
77. Logs
78. Security
79. Web servers
80. LEMP/LAMP
81. WordPress
82. phpMyAdmin
83. Troubleshooting
    {% endstep %}
    {% endstepper %}

## 81. Final Revision Checklist

Before considering the Linux fundamentals section complete, you should be able to:

* [ ] SSH into a Linux server
* [ ] Use SSH keys
* [ ] Identify CPU cores
* [ ] Check RAM
* [ ] Check disk space
* [ ] Identify partitions
* [ ] Find the IP address
* [ ] Find hostname
* [ ] Identify OS and kernel
* [ ] Create users
* [ ] Create groups
* [ ] Add users to secondary groups
* [ ] Configure a non-interactive user
* [ ] Understand `chmod`
* [ ] Understand `chown`
* [ ] Configure SGID
* [ ] Configure collaborative directories
* [ ] Understand ACLs
* [ ] Check services with `systemctl`
* [ ] Analyze CPU/load
* [ ] Check listening ports
* [ ] Configure/check UFW
* [ ] Configure SSH authentication
* [ ] Use cron
* [ ] Use `at`
* [ ] Configure Nepal timezone
* [ ] Understand NTP/Chrony synchronization
* [ ] Check swap
* [ ] Understand `/etc/fstab`
* [ ] Create and extract tar archives
* [ ] Use gzip
* [ ] Use bzip2
* [ ] Find `.sh` scripts
* [ ] Use `grep`
* [ ] Write basic Bash scripts
* [ ] Check web servers
* [ ] Check logs
* [ ] Update packages
* [ ] Understand basic LEMP/LAMP administration
* [ ] Understand WordPress and phpMyAdmin deployment concepts

## 82. Ownership and Document Information

**Document Owner:** Mrinal Ghimire\
**Created:** August 4, 2026\
**Purpose:** Linux System Administration Learning, Practice, Assignment Revision, and GitBook Documentation

This documentation is intended as a practical revision reference for Linux server administration and the assignments completed during the learning process.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://mrinalghimire.com.np/linux-system-administration/linux-system-administration.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
