Setup & Environment: Practice Exercises

6 minute read

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:

  1. whoami — what is your username?
  2. hostname — what is the machine name?
  3. uname -a — what kernel and OS are you running?
  4. uptime — how long has the system been running?
  5. pwd — what directory are you in when you first log in?
  6. echo $HOME — what is your home directory path?
  7. echo $SHELL — which shell are you using?
  8. 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:

  1. Open ~/.bashrc (or ~/.zshrc on macOS/zsh)
  2. Set a PS1 prompt that shows:
    • Your username in green
    • The hostname
    • The current directory in blue
    • The $ symbol
  3. Add an alias: ll → ls -alh --color=auto
  4. Add an alias: .. → cd ..
  5. Set HISTSIZE=10000
  6. Apply the changes without restarting the terminal
  7. Test: type ll and 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):

  1. Create a new user named devuser
  2. Set a password for devuser
  3. Add devuser to the sudo group
  4. Verify devuser is in the sudo group using groups devuser
  5. Switch to devuser’s account using sudo su - devuser
  6. Run whoami to confirm you are now devuser
  7. Exit back to your original user
  8. Delete the user devuser when 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:

  1. Check if you already have an SSH key: ls ~/.ssh/
  2. Generate a new SSH key pair (use ed25519 algorithm)
  3. Display your public key
  4. Understand what each file is
  5. (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:

  1. Print all environment variables using env — count how many there are with env | wc -l
  2. Find the value of PATH — how many directories are in it?
  3. Create a temporary environment variable MY_NAME="YourName" and print it
  4. Add a directory ~/bin to your PATH temporarily, then make it permanent in ~/.bashrc
  5. Create a script ~/bin/greet.sh that 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

Updated: