PleinIQ
Menu
Language

Choose your language

This page is only in English for now. Choosing a language opens its home page.

Other languages

Your browser can translate any page into almost any language.

Other browsers: Open the browser menu and look for “Translate”.

Your resume, your downloads and lab commands are never translated.

Roadmaps / Linux / Lab 6

Lab 6 of 8 · Networking, SSH and firewalls

Secure SSH and put a firewall in front of your server

Inspect the network, sign in with your own SSH key, turn off password sign-in, and allow only SSH and your status page through a firewall.

Time
60 minutes
Cost
Free.

What you learn

Servers are reached over the network, administered over SSH, and protected by a firewall that allows only what is needed.

  • Addresses, routes, listening ports and DNS from the server side
  • Signing in with your own SSH key, and turning off password sign-in
  • A default-deny firewall with ufw
  • Testing access from another machine

In the AI era: Firewall and SSH changes are where AI-suggested commands lock people out. Always allow SSH before turning a firewall on, check config before reloading, and keep a second session open.

Get set up

Commands use Homebrew in the Terminal app. Already have a tool? Skip it.

  • Install
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

    Use the Terminal app. The command installs Homebrew, which the other macOS commands use.

Before you start

  • Finish labs 1 and 4 (you need the status page on port 8080).

  • Start your server and open your repository: run multipass start sysadmin-lab and multipass shell sysadmin-lab on your computer, then cd linux-admin-lab on the server.

Step by step

  1. 1 See the server from the inside

    ip shows addresses and routes, ss shows which programs listen on which ports, and resolvectl shows which DNS server is used.

    Run this on your lab server
    ip -brief address
    ip route
    sudo ss -tlnp
    resolvectl status | head -n 12
    You should see:

    Your server's address, its default route, sshd on port 22, python3 on port 8080, and a DNS server.

  2. 2 Create an SSH key on your computer

    Open a second terminal on your own computer for this step, not on the server. Press Enter to accept the defaults, then copy the line that starts with ssh-ed25519.

    Run this in Terminal
    ssh-keygen -t ed25519 -f ~/.ssh/sysadmin-lab -C "sysadmin-lab"
    cat ~/.ssh/sysadmin-lab.pub
    You should see:

    One line starting with ssh-ed25519 and ending with sysadmin-lab.

  3. 3 Authorise the key on the server

    Replace the placeholder with the line you copied. Only someone holding the matching private key can now sign in as ubuntu.

    Run this on your lab server
    echo "PASTE-YOUR-PUBLIC-KEY-LINE-HERE" >> ~/.ssh/authorized_keys
    chmod 600 ~/.ssh/authorized_keys
  4. 4 Sign in with your key

    Replace SERVER-IP with the address from the first line. Leave this session open: it is your safety line while you change SSH and the firewall.

    Run this in Terminal
    multipass info sysadmin-lab | grep IPv4
    ssh -i ~/.ssh/sysadmin-lab ubuntu@SERVER-IP
    You should see:

    A shell on sysadmin-lab without any password prompt.

  5. 5 Turn off password sign-in

    Put your settings in a drop-in file, test the whole configuration with sshd -t, and reload only if the test passes.

    Run this on your lab server
    printf 'PasswordAuthentication no\nPermitRootLogin no\n' | sudo tee /etc/ssh/sshd_config.d/10-hardening.conf
    sudo sshd -t && sudo systemctl reload ssh
    sudo sshd -T | grep -E '^(passwordauthentication|permitrootlogin) '
    You should see:

    passwordauthentication no and permitrootlogin no, and your key session still works.

  6. 6 Ask AI for firewall rules, then dry-run them

    Ask your AI assistant: "Write ufw commands for a server that must allow SSH and TCP port 8080 and block all other incoming traffic." Before running anything, check that its answer allows SSH: turning on a firewall without it locks you out. --dry-run shows what ufw would change without changing it.

    Run this on your lab server
    sudo ufw --dry-run allow OpenSSH
    sudo ufw --dry-run allow 8080/tcp
    You should see:

    The rules ufw would add, and nothing changes yet.

  7. 7 Turn the firewall on

    Default deny for incoming traffic, then allow exactly two things.

    Run this on your lab server
    sudo ufw default deny incoming
    sudo ufw default allow outgoing
    sudo ufw allow OpenSSH
    sudo ufw allow 8080/tcp
    sudo ufw --force enable
    sudo ufw status verbose
    You should see:

    Status: active, Default: deny (incoming), and ALLOW rules for OpenSSH and 8080/tcp.

  8. 8 Test from your computer

    The first command should reach the status page through the firewall. The second tries a password sign-in, which must now be refused.

    Run this in Terminal
    curl http://SERVER-IP:8080
    ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password ubuntu@SERVER-IP
    You should see:

    Your <h1> line, then Permission denied (publickey).

  9. 9 Write your notes

    Create the file with nano notes/06-network.md.

    File: notes/06-network.md
    # Networking, SSH and firewall
    
    ## What my server listens on
    | Port | Program | Needed? |
    |------|---------|---------|
    | 22 | sshd | yes, for administration |
    | 8080 | python3 | ... |
    
    ## SSH
    - How I sign in now: ...
    - Why password sign-in is off: ...
    
    ## Firewall
    - Default policy: ...
    - What the AI suggested, and whether it would have locked me out: ...
    
  10. 10 Commit and push

    Keep the SSH settings with your notes.

    Run this on your lab server
    mkdir -p config
    sudo cp /etc/ssh/sshd_config.d/10-hardening.conf config/
    sudo chown ubuntu: config/10-hardening.conf
    git add config notes
    git commit -m "Harden SSH and add a firewall"
    git push

You are done when

  • You signed in with your own SSH key, and password sign-in is refused.

  • ufw is active and denies incoming traffic except SSH and port 8080.

  • Your SSH settings and notes are on GitHub.

Finished? Get your proof page

We check your public GitHub repository and give you a page to share with recruiters, and can add the project to your resume.

Join PleinIQ Updates

What are you interested in?

Read how we handle your information in our privacy notice.