First SSH Connection
Topic
Level: Beginner → Intermediate
This topic focuses on making your first SSH connection and understanding every step that happens before, during, and after login.
By the end of this lesson, you should be able to:
- Connect using IP addresses
- Connect using hostnames
- Understand port selection
- Understand username resolution
- Understand the first login warning
- Understand how
known_hostsis created- Navigate a remote system
- Disconnect properly
- Troubleshoot connection problems
- Debug SSH connections
Table of Contents
- What is an SSH Connection?
- Requirements Before Connecting
- Connecting by IP Address
- Connecting by Hostname
- Port Selection
- Username Resolution
- First Login Warning
- known_hosts Creation
- Successful Login
- Basic Navigation
- Disconnecting
- Connection Errors
- Connection Debugging
- Connection Flow Diagram
- Commands
- Code Examples
- Real-World Examples
- Production Examples
- Common Mistakes
- Troubleshooting
- Security Notes
- Interview Questions
- Hands-On Labs
- Exercises
- Quiz
1. What is an SSH Connection?
An SSH connection is a secure communication session between:
SSH Client
and
SSH Server
Example
ssh [email protected]
Meaning:
SSH Client = Your Computer
SSH Server = Remote Machine
Username = john
IP Address = 192.168.1.100
Visual Diagram
+-----------+
| Your PC |
+-----------+
|
|
SSH
|
|
+-----------+
| Server |
+-----------+
Goal
After connection:
john@server:~$
appears.
You now control the remote system.
2. Requirements Before Connecting
Before SSH can work:
Requirement 1
SSH Server Installed
Verify on server:
which sshd
Requirement 2
SSH Service Running
sudo systemctl status ssh
or
sudo systemctl status sshd
Requirement 3
Network Connectivity
Test:
ping server-ip
Requirement 4
Correct Username
Example:
john
ubuntu
admin
ec2-user
Requirement 5
Port Reachable
Default:
22
Checklist
✓ SSH Installed
✓ SSH Running
✓ Network Working
✓ Correct User
✓ Correct Port
3. Connecting by IP Address
The most direct method.
Syntax
ssh username@ip-address
Example
ssh [email protected]
Meaning:
Connect as john
To server 192.168.1.100
Flow
Client
|
192.168.1.100
|
Server
Another Example
ssh [email protected]
Advantages
Simple
Direct
No DNS Needed
Disadvantages
Hard to Remember
IP May Change
4. Connecting by Hostname
Instead of IP:
ssh username@hostname
Example
ssh [email protected]
What Happens?
SSH performs:
DNS Resolution
before connecting.
Flow
server.example.com
|
DNS Lookup
|
203.0.113.10
|
SSH Connection
Example
ssh [email protected]
Benefits
Readable
Easy to Remember
IP Changes Hidden
Verify DNS
nslookup server.example.com
or
dig server.example.com
5. Port Selection
SSH normally uses:
Port 22
Default Example
ssh user@server
SSH automatically uses:
22
Custom Port
Example:
2222
Syntax
ssh -p PORT user@server
Example
ssh -p 2222 john@server
Diagram
Client
|
Port 2222
|
Server
Verify Server Port
Server:
ss -tlnp | grep ssh
Example Output
LISTEN 0 128 *:2222
6. Username Resolution
SSH must know:
Which user account
to log into.
Example
ssh john@server
SSH tries:
Login as user john
If Username Omitted
ssh server
SSH uses:
Current Local Username
Example
Current user:
alice
Command:
ssh server
Equivalent:
ssh alice@server
Verify Current User
whoami
Example
alice
Cloud Examples
AWS Ubuntu:
ssh ubuntu@server
AWS Amazon Linux:
ssh ec2-user@server
Common Mistake
Wrong username:
ssh root@server
when only:
ubuntu
exists.
Error
Permission denied
7. First Login Warning
First time connecting:
The authenticity of host
cannot be established.
Example Message
The authenticity of host
'192.168.1.100'
can't be established.
ED25519 key fingerprint is:
SHA256:xxxxxxxxxxxxx
Are you sure you want to continue?
Why?
SSH has never seen this server before.
SSH Asks
Do you trust this server?
Options
yes
no
If You Type
yes
SSH stores the server identity.
This Prevents
Man-in-the-Middle Attacks
8. known_hosts Creation
After accepting host verification:
SSH creates:
~/.ssh/known_hosts
Purpose
Stores trusted host keys.
Example
server.example.com ssh-ed25519 AAAA...
View File
cat ~/.ssh/known_hosts
Flow
Server
|
Host Key
|
Client
|
known_hosts
Future Connections
SSH compares:
Stored Key
vs
Presented Key
Match?
Yes → Continue
No → Warning
Host Key Changed
Warning:
REMOTE HOST IDENTIFICATION
HAS CHANGED
Possible Causes
Server Reinstalled
Host Key Rotated
MITM Attack
9. Successful Login
After authentication:
john@server:~$
appears.
Example
ssh [email protected]
Result:
john@server:~$
Verify Location
hostname
Example Output
server
Verify User
whoami
Example
john
Verify Current Directory
pwd
Example
/home/john
10. Basic Navigation
Once logged in:
Show Files
ls
Detailed Listing
ls -la
Current Directory
pwd
Change Directory
cd /etc
Go Home
cd ~
Show OS Information
uname -a
Show Logged-In User
whoami
System Uptime
uptime
Navigation Diagram
Login
|
+-- ls
|
+-- pwd
|
+-- cd
|
+-- cat
|
+-- nano
11. Disconnecting
Safely end session.
Method 1
exit
Method 2
logout
Method 3
Keyboard Shortcut:
Ctrl + D
Flow
SSH Session
|
exit
|
Connection Closed
Example
john@server:~$ exit
logout
Connection closed.
12. Connection Errors
Common errors every administrator encounters.
Error 1
Connection Refused
Meaning:
Server reached
SSH not accepting connections
Causes
sshd stopped
Wrong port
Firewall
Error 2
Connection Timed Out
Causes
Network issue
Firewall
Server offline
Error 3
Permission denied
Causes
Wrong password
Wrong key
Wrong username
Error 4
Host key verification failed
Causes
Host key mismatch
Error 5
Could not resolve hostname
Causes
DNS problem
Typo
Error Summary
| Error | Common Cause |
|---|---|
| Connection Refused | SSH Service Down |
| Timeout | Network Issue |
| Permission Denied | Authentication Failure |
| Host Key Failed | Key Mismatch |
| DNS Error | Name Resolution Failure |
13. Connection Debugging
One of the most important SSH skills.
Verbose Mode
ssh -v user@server
More Detail
ssh -vv user@server
Maximum Detail
ssh -vvv user@server
Example
ssh -vvv john@server
What You Can See
DNS Resolution
Key Exchange
Host Verification
Authentication
Session Creation
Example Debug Output
debug1: Connecting to server
debug1: Connection established
debug1: Authenticating
debug1: Authentication succeeded
Server-Side Logs
Ubuntu:
sudo tail -f /var/log/auth.log
RHEL
sudo tail -f /var/log/secure
14. Connection Flow Diagram
ssh user@server
|
DNS Resolution
|
TCP Connection
|
Host Verification
|
Authentication
|
known_hosts Update
|
Session Created
|
Shell Started
15. Commands
Connect:
ssh user@server
Connect by IP:
ssh [email protected]
Custom Port:
ssh -p 2222 user@server
Debug:
ssh -vvv user@server
Exit:
exit
View Known Hosts:
cat ~/.ssh/known_hosts
Current User:
whoami
16. Code Examples
Remote uptime:
ssh server "uptime"
Show hostname:
ssh server "hostname"
List files remotely:
ssh server "ls -la"
Use custom port:
ssh -p 2200 admin@server
17. Real-World Examples
System Administrator:
ssh admin@server
Checks:
uptime
df -h
free -m
Developer:
ssh dev@staging-server
Deploys application.
18. Production Examples
AWS EC2:
ssh -i key.pem ubuntu@ec2-ip
Database Server:
ssh dba@db-server
Enterprise Network:
ssh admin@core-switch
19. Common Mistakes
Mistake 1
Wrong username.
Mistake 2
Wrong port.
Mistake 3
Ignoring host key warnings.
Mistake 4
Using IP when DNS exists.
Mistake 5
Not checking verbose output.
20. Troubleshooting
Verify SSH Service
systemctl status ssh
Verify Port
ss -tlnp | grep ssh
Verify Network
ping server
Verify DNS
nslookup server
Debug Connection
ssh -vvv user@server
Check Logs
tail -f /var/log/auth.log
21. Security Notes
Always verify first-login fingerprints.
Never blindly trust:
Are you sure you want to continue?
on critical systems.
Prefer:
Public Key Authentication
over passwords.
Monitor:
known_hosts
for unexpected changes.
22. Interview Questions
Q1
How do you connect to a server using SSH?
Answer:
ssh user@server
Q2
What is known_hosts?
Answer:
File containing trusted host keys.
Q3
What happens during first login?
Answer:
Host key verification occurs.
Q4
How do you use a custom SSH port?
Answer:
ssh -p PORT user@server
Q5
How do you debug SSH connections?
Answer:
ssh -vvv user@server
23. Hands-On Labs
Lab 1
Connect to localhost.
ssh localhost
Lab 2
Observe first-login warning.
Connect to a new VM.
Lab 3
Inspect known_hosts.
cat ~/.ssh/known_hosts
Lab 4
Use verbose mode.
ssh -vvv localhost
Lab 5
Login and navigate.
pwd
ls -la
hostname
whoami
Lab 6
Disconnect using all methods.
exit
logout
Ctrl+D
24. Exercises
Exercise 1
Explain the difference between
connecting by IP and hostname.
Exercise 2
Describe the purpose of known_hosts.
Exercise 3
List common SSH connection errors.
Exercise 4
Explain username resolution.
Exercise 5
Describe the first-login verification process.
25. Quiz
Question 1
Default SSH port?
A. 21
B. 22
C. 23
D. 80
Answer:
B
Question 2
File storing trusted host keys?
A. authorized_keys
B. passwd
C. known_hosts
D. sshd_config
Answer:
C
Question 3
Command for maximum debugging?
A. ssh -v
B. ssh -vv
C. ssh -vvv
D. ssh --debug
Answer:
C
Question 4
Command to disconnect?
A. quit
B. close
C. exit
D. stop
Answer:
C
Question 5
What causes "Could not resolve hostname"?
A. SSH key issue
B. DNS problem
C. Password failure
D. Shell issue
Answer:
B
Summary
You should now understand:
✓ Connecting by IP
✓ Connecting by Hostname
✓ Port Selection
✓ Username Resolution
✓ First Login Warning
✓ known_hosts Creation
✓ Basic Navigation
✓ Disconnecting
✓ Connection Errors
✓ Connection Debugging
✓ First SSH Login Workflow
Next Topic:
Topic #6 — Password Authentication
Password Login Process
PAM
Authentication Flow
Password Storage
/etc/shadow
Authentication Logs
Password Policies
Password Security
Brute Force Attacks
Lockout Mechanisms