Key-based authentication in Linux CLI - Time & Space Complexity
We want to understand how the time needed to connect using key-based authentication changes as we try to connect to more servers.
How does the process time grow when the number of servers increases?
Analyze the time complexity of the following code snippet.
for server in $server_list; do
ssh -i ~/.ssh/id_rsa user@${server} "uptime"
done
This script connects to each server in a list using key-based authentication and runs a simple command.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The ssh connection command inside the loop.
- How many times: Once for each server in the list.
Each additional server adds one more ssh connection and command execution.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 ssh connections and commands |
| 100 | 100 ssh connections and commands |
| 1000 | 1000 ssh connections and commands |
Pattern observation: The total time grows directly with the number of servers.
Time Complexity: O(n)
This means the time to complete all connections grows in a straight line as you add more servers.
[X] Wrong: "Connecting to multiple servers at once will take the same time as connecting to one."
[OK] Correct: Each connection takes its own time, so more servers mean more total time.
Understanding how connection time grows helps you plan scripts that manage many servers efficiently and shows you think about scaling real tasks.
"What if we run ssh connections in parallel instead of one after another? How would the time complexity change?"
Practice
Solution
Step 1: Understand key-based authentication
Key-based authentication uses a pair of keys to allow login without passwords.Step 2: Identify the main benefit
This method lets users connect securely without typing passwords each time.Final Answer:
To allow secure login without typing a password every time -> Option BQuick Check:
Key-based authentication = passwordless secure login [OK]
- Confusing key-based auth with file encryption
- Thinking it speeds up internet
- Believing it backs up data
Solution
Step 1: Identify the key generation command
The commandssh-keygencreates a new SSH key pair.Step 2: Differentiate from other commands
ssh-copy-idcopies keys,ssh-addadds keys to agent,ssh-connectis invalid.Final Answer:
ssh-keygen -> Option AQuick Check:
Generate keys = ssh-keygen [OK]
- Using ssh-copy-id to generate keys
- Confusing ssh-add with key creation
- Typing a non-existent ssh-connect command
ssh-copy-id user@server after generating your SSH keys?Solution
Step 1: Understand ssh-copy-id purpose
This command copies your public key to the server's authorized keys file.Step 2: Identify what is copied
Only the public key is copied, enabling passwordless login; private keys stay local.Final Answer:
Your public key is copied to the server for passwordless login -> Option DQuick Check:
ssh-copy-id copies public key to server [OK]
- Thinking private key is copied
- Believing server keys copy locally
- Assuming ssh-copy-id restarts SSH
Solution
Step 1: Check key placement
If the public key is not on the server, SSH cannot authenticate without a password.Step 2: Understand private key location
The private key stays on the client, so missing it on the server is irrelevant.Final Answer:
The public key was not copied to the server -> Option AQuick Check:
Missing public key on server = password prompt [OK]
- Looking for private key on server
- Assuming SSH server off causes password prompt
- Blaming wrong username without checking keys
Solution
Step 1: Generate SSH keys first
Usessh-keygento create your key pair locally.Step 2: Copy public key to server
Usessh-copy-id user@serverto add your public key to the server.Step 3: Connect without password
Nowssh user@serverlogs in using the key without asking for a password.Final Answer:
ssh-keygen -> ssh-copy-id user@server -> ssh user@server -> Option CQuick Check:
Generate keys, copy key, then connect [OK]
- Copying keys before generating them
- Trying to connect before copying keys
- Mixing command order
Ready to prove it?
Pass the test and this pattern moves unknown
