Securing Raspberry Pi (SSH keys, firewall) - Time & Space Complexity
When securing a Raspberry Pi using SSH keys and firewall rules, it is important to understand how the time to apply these security steps grows as the number of devices or rules increases.
We want to know how the work needed changes when we add more keys or firewall rules.
Analyze the time complexity of the following code snippet.
import os
# Add multiple SSH public keys to authorized_keys
for key in ssh_keys:
with open('/home/pi/.ssh/authorized_keys', 'a') as file:
file.write(key + '\n')
# Apply firewall rules from a list
for rule in firewall_rules:
os.system(f"sudo ufw allow {rule}")
This code adds each SSH key to the authorized keys file and applies each firewall rule one by one.
- Primary operation: Two loops that each run through lists of keys and firewall rules.
- How many times: Each loop runs once for every item in its list.
As the number of SSH keys or firewall rules grows, the time to add or apply them grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations (10 keys + 10 rules) |
| 100 | About 200 operations |
| 1000 | About 2000 operations |
Pattern observation: Doubling the number of keys and rules roughly doubles the work needed.
Time Complexity: O(n)
This means the time to secure the Raspberry Pi grows directly with the number of keys and firewall rules you add.
[X] Wrong: "Adding more SSH keys or firewall rules won't affect the time much because each step is quick."
[OK] Correct: Even if each step is fast, doing many steps adds up, so more keys or rules mean more total time.
Understanding how security setup time grows helps you plan and explain your approach clearly in real projects or interviews.
"What if we batch all firewall rules into one command instead of running one command per rule? How would the time complexity change?"
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
