Securing your Raspberry Pi helps keep it safe from hackers. Using SSH keys and a firewall protects your device when you connect to it remotely.
Securing Raspberry Pi (SSH keys, firewall)
1. Generate SSH keys on your computer: ssh-keygen -t rsa -b 4096 2. Copy your public key to Raspberry Pi: ssh-copy-id pi@raspberrypi.local 3. Enable firewall (ufw) on Raspberry Pi: sudo apt install ufw sudo ufw allow ssh sudo ufw enable 4. Check firewall status: sudo ufw status
SSH keys replace passwords with a pair of secret and public keys for safer login.
Firewall (ufw) controls which connections are allowed to your Raspberry Pi.
ssh-keygen -t rsa -b 4096ssh-copy-id pi@raspberrypi.local
sudo ufw allow ssh
sudo ufw enable
This script shows the main steps to secure your Raspberry Pi: create SSH keys, copy them, connect using keys, and set up a firewall.
#!/bin/bash # Step 1: Generate SSH key pair (run on your computer) ssh-keygen -t rsa -b 4096 -f ~/.ssh/id_rpi_key -N "" # Step 2: Copy public key to Raspberry Pi ssh-copy-id -i ~/.ssh/id_rpi_key.pub pi@raspberrypi.local # Step 3: Connect to Raspberry Pi using the new key and set up firewall ssh -i ~/.ssh/id_rpi_key pi@raspberrypi.local 'sudo apt update && sudo apt install -y ufw && sudo ufw allow ssh && sudo ufw enable && sudo ufw status'
Always keep your private SSH key safe and never share it.
Use strong passphrases for SSH keys if you want extra security.
Check firewall rules regularly to avoid blocking needed connections.
Use SSH keys to log in without passwords and improve security.
Set up a firewall to control network access to your Raspberry Pi.
These steps help protect your Raspberry Pi from unauthorized access.
Practice
Solution
Step 1: Understand SSH key authentication
SSH keys use a pair of cryptographic keys to authenticate users without sending passwords over the network.Step 2: Compare with password authentication
Password authentication can be vulnerable to guessing or brute force attacks, while SSH keys are much harder to crack.Final Answer:
It improves security by avoiding password guessing attacks. -> Option AQuick Check:
SSH keys = better security [OK]
- Thinking SSH keys make the device faster
- Believing SSH keys share passwords
- Assuming SSH keys disable firewall
Solution
Step 1: Identify the SSH key generation command
The standard command to create SSH keys isssh-keygenwith the-toption to specify the type, e.g., rsa.Step 2: Check options for correctness
-pchanges the passphrase,--firewallandfirewall-cmdare unrelated to SSH keys.Final Answer:
ssh-keygen -t rsa -> Option CQuick Check:
ssh-keygen -t rsa = generate SSH keys [OK]
- Using -p instead of -t for key generation
- Confusing firewall commands with SSH key commands
- Trying to generate keys with firewall tools
sudo ufw status?
sudo ufw enable sudo ufw allow ssh sudo ufw deny 80
Solution
Step 1: Analyze the firewall commands
sudo ufw enableactivates the firewall.sudo ufw allow sshopens port 22 (SSH).sudo ufw deny 80blocks HTTP port 80.Step 2: Understand the status output
The status shows active firewall with rules allowing SSH (port 22) and denying port 80.Final Answer:
Status: active To Action From -- ------ ---- 22/tcp ALLOW Anywhere 80 DENY Anywhere -> Option DQuick Check:
Firewall enabled with SSH allow and port 80 deny = Status: active To Action From -- ------ ---- 22/tcp ALLOW Anywhere 80 DENY Anywhere [OK]
- Assuming firewall is inactive after enabling
- Expecting no rules after adding allow/deny
- Confusing port numbers with service names
~/.ssh/authorized_keys but still get password prompts. What is the most likely cause?Solution
Step 1: Check SSH key authentication requirements
SSH requires strict permissions on~/.sshandauthorized_keysfiles to allow key login.Step 2: Identify common permission issues
If permissions are too open, SSH ignores the keys and falls back to password login.Final Answer:
The authorized_keys file has incorrect permissions. -> Option AQuick Check:
Wrong permissions block SSH key login [OK]
- Thinking firewall affects SSH key login
- Restarting SSH service unnecessarily
- Assuming wrong key type without checking permissions
ufw commands achieve this?Solution
Step 1: Allow SSH only from the specific IP
Usesudo ufw allow from 192.168.1.100 to any port 22to permit SSH from that IP.Step 2: Deny all other SSH connections
Usesudo ufw deny 22to block SSH from all other IPs.Final Answer:
sudo ufw allow from 192.168.1.100 to any port 22 sudo ufw deny 22 -> Option BQuick Check:
Allow specific IP then deny all others = sudo ufw allow from 192.168.1.100 to any port 22 sudo ufw deny 22 [OK]
- Allowing SSH globally before denying specific IP
- Denying the allowed IP instead of others
- Using wrong port numbers or commands
Ready to prove it?
Pass the test and this pattern moves unknown
