Setup & Environment: Practice Exercises
These exercises let you practise using a Linux environment — whether that’s VirtualBox, WSL2, macOS Terminal, or a cloud shell. All exercises work in any of these environments.
Exercise 1 — Connect and Explore
Difficulty: ⭐ Easy
Connect to a Linux environment (Cloud Shell, WSL2, VirtualBox, or macOS Terminal) and run the following commands. Record the output for each:
whoami— what is your username?hostname— what is the machine name?uname -a— what kernel and OS are you running?uptime— how long has the system been running?pwd— what directory are you in when you first log in?echo $HOME— what is your home directory path?echo $SHELL— which shell are you using?cat /etc/os-release— what Linux distribution is this?
💡 Show Example Output (Ubuntu 22.04)
whoami
# ubuntu
hostname
# ip-10-0-1-23
uname -a
# Linux ip-10-0-1-23 5.15.0-1057-aws #63~20.04.1-Ubuntu SMP Tue Nov 14 ... x86_64 x86_64 x86_64 GNU/Linux
uptime
# 10:01:23 up 5 days, 14:32, 1 user, load average: 0.00, 0.00, 0.00
pwd
# /home/ubuntu
echo $HOME
# /home/ubuntu
echo $SHELL
# /bin/bash
cat /etc/os-release
# NAME="Ubuntu"
# VERSION="22.04.3 LTS (Jammy Jellyfish)"
# ID=ubuntu
# ID_LIKE=debian
# PRETTY_NAME="Ubuntu 22.04.3 LTS"
# VERSION_ID="22.04"
# HOME_URL="https://www.ubuntu.com/"
Your output will differ based on your environment. The key things to note:
- Cloud shells (AWS, GCP) run Amazon Linux or Debian
- WSL2 runs Ubuntu or whichever distro you installed
- macOS Terminal runs macOS (Darwin) — not Linux
- VirtualBox Ubuntu runs Ubuntu
Exercise 2 — Prompt Customisation
Difficulty: ⭐⭐ Medium
Customise your shell prompt to show useful information:
- Open
~/.bashrc(or~/.zshrcon macOS/zsh) - Set a PS1 prompt that shows:
- Your username in green
- The hostname
- The current directory in blue
- The
$symbol
- Add an alias:
ll→ls -alh --color=auto - Add an alias:
..→cd .. - Set
HISTSIZE=10000 - Apply the changes without restarting the terminal
- Test: type
lland verify it works; type..and verify you go up one directory
💡 Show Answer
# Open .bashrc
nano ~/.bashrc
# Add these lines at the bottom:
# ── Custom Prompt ──────────────────────────────────────────
export PS1='\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ '
# ── Aliases ────────────────────────────────────────────────
alias ll='ls -alh --color=auto'
alias ..='cd ..'
# ── History ────────────────────────────────────────────────
export HISTSIZE=10000
# Save the file and apply:
source ~/.bashrc
# Test the aliases:
ll
# total 40
# drwxr-x--- 5 ubuntu ubuntu 4096 Apr 1 10:00 .
# drwxr-xr-x 4 root root 4096 Mar 15 08:00 ..
# -rw-r--r-- 1 ubuntu ubuntu 220 Mar 15 08:00 .bash_logout
# -rw-r--r-- 1 ubuntu ubuntu 3526 Mar 15 08:00 .bashrc
# ...
cd /var/log
..
pwd
# /var
Exercise 3 — User Management
Difficulty: ⭐⭐ Medium
Practise creating and managing users (requires sudo access):
- Create a new user named
devuser - Set a password for
devuser - Add
devuserto thesudogroup - Verify
devuseris in the sudo group usinggroups devuser - Switch to
devuser’s account usingsudo su - devuser - Run
whoamito confirm you are nowdevuser - Exit back to your original user
- Delete the user
devuserwhen done (clean up)
💡 Show Answer
# 1. Create the user
sudo adduser devuser
# Adding user 'devuser' ...
# Adding new group 'devuser' (1002) ...
# Adding new user 'devuser' (1002) with group 'devuser' ...
# New password: ••••••••
# Retype new password: ••••••••
# passwd: password updated successfully
# Full Name []: Dev User
# Is the information correct? [Y/n] Y
# 2. Password was set in adduser (step above)
# 3. Add to sudo group
sudo usermod -aG sudo devuser
# 4. Verify groups
groups devuser
# devuser : devuser sudo
# 5. Switch to devuser
sudo su - devuser
# 6. Verify identity
whoami
# devuser
# 7. Go back
exit
# ubuntu@server:~$
# 8. Clean up — delete the user and home directory
sudo deluser --remove-home devuser
# Removing user 'devuser' ...
# Removing files owned by 'devuser'...
# Done.
# Verify deletion
id devuser
# id: 'devuser': no such user
Exercise 4 — SSH Key Setup
Difficulty: ⭐⭐ Medium
Generate an SSH key pair and understand how SSH authentication works:
- Check if you already have an SSH key:
ls ~/.ssh/ - Generate a new SSH key pair (use
ed25519algorithm) - Display your public key
- Understand what each file is
- (Optional) Add your public key to a second Linux system’s
~/.ssh/authorized_keys
💡 Show Answer
# 1. Check for existing keys
ls -la ~/.ssh/
# If .ssh doesn't exist or is empty, generate a new key pair
# 2. Generate an ed25519 SSH key pair
ssh-keygen -t ed25519 -C "your-email@example.com"
# Generating public/private ed25519 key pair.
# Enter file in which to save the key (/home/ubuntu/.ssh/id_ed25519): [press Enter]
# Enter passphrase (empty for no passphrase): [press Enter or type a passphrase]
# Your identification has been saved in /home/ubuntu/.ssh/id_ed25519
# Your public key has been saved in /home/ubuntu/.ssh/id_ed25519.pub
# 3. Display the public key (safe to share)
cat ~/.ssh/id_ed25519.pub
# ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAI... your-email@example.com
# 4. Understand the files:
ls -la ~/.ssh/
# -rw------- 1 ubuntu ubuntu 399 Apr 1 10:00 id_ed25519 ← PRIVATE key (never share)
# -rw-r--r-- 1 ubuntu ubuntu 101 Apr 1 10:00 id_ed25519.pub ← PUBLIC key (safe to share)
# id_ed25519 — your private key. Keep this secret. Never copy it anywhere.
# id_ed25519.pub — your public key. Copy this to servers you want to access.
# 5. To add to another server's authorized_keys:
# On the TARGET server:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "ssh-ed25519 AAAAC3Nz... your-email@example.com" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
# Now SSH from your machine works without a password:
ssh ubuntu@target-server-ip
# (no password prompt — authenticated with key)
Key permissions are critical:
# Private key MUST be 600 (owner read/write only)
chmod 600 ~/.ssh/id_ed25519
# .ssh directory MUST be 700 (owner rwx only)
chmod 700 ~/.ssh
# authorized_keys MUST be 600
chmod 600 ~/.ssh/authorized_keys
# If permissions are wrong, SSH refuses to use the key:
# WARNING: UNPROTECTED PRIVATE KEY FILE!
Exercise 5 — Environment Variable Exploration
Difficulty: ⭐⭐⭐ Challenging
Explore and understand the Linux environment:
- Print all environment variables using
env— count how many there are withenv | wc -l - Find the value of
PATH— how many directories are in it? - Create a temporary environment variable
MY_NAME="YourName"and print it - Add a directory
~/binto yourPATHtemporarily, then make it permanent in~/.bashrc - Create a script
~/bin/greet.shthat prints “Hello from greet.sh!” and verify it runs from anywhere
💡 Show Answer
# 1. Print and count all environment variables
env
env | wc -l
# 42 (number varies by system)
# 2. PATH — how many directories?
echo $PATH
# /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
# Count the colon-separated entries
echo $PATH | tr ':' '\n' | wc -l
# 7
# List each directory on its own line
echo $PATH | tr ':' '\n'
# /usr/local/sbin
# /usr/local/bin
# /usr/sbin
# /usr/bin
# /sbin
# /bin
# /usr/games
# 3. Create a temporary environment variable
MY_NAME="YourName"
export MY_NAME
echo $MY_NAME
# YourName
# (disappears when you close the terminal)
# 4. Add ~/bin to PATH temporarily
mkdir -p ~/bin
export PATH="$HOME/bin:$PATH"
echo $PATH | head -c 50
# /home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:...
# Make it permanent
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
# 5. Create a script in ~/bin
cat > ~/bin/greet.sh << 'EOF'
#!/usr/bin/env bash
echo "Hello from greet.sh!"
echo "Called from: $(pwd)"
echo "By user: $(whoami)"
EOF
chmod +x ~/bin/greet.sh
# Test from different directories
greet.sh
# Hello from greet.sh!
# Called from: /home/ubuntu
# By user: ubuntu
cd /tmp
greet.sh
# Hello from greet.sh!
# Called from: /tmp
# By user: ubuntu
cd ~
Chapter Summary
After completing these exercises you should be able to:
- ✅ Connect to a Linux environment and identify the OS, kernel, and user
- ✅ Customise your shell prompt and persist settings in
~/.bashrc - ✅ Create, modify, and delete Linux users with
adduser,usermod,deluser - ✅ Generate SSH key pairs and understand public vs private keys
- ✅ Explore and modify environment variables, including
PATH
| ← Back to First Login and Prompt | Next Chapter → Common Commands |