📋Overview
This comprehensive guide will walk you through the essential steps of logging into your Ubuntu server for the first time and implementing critical security hardening measures. Server hardening is the process of securing your system by reducing its attack surface and implementing security best practices.
🎯Prerequisites:
- A running Ubuntu server instance (Ubuntu 24.04 recommended)
- Server IP address and root credentials
- Terminal access (Terminal for Mac/Linux, Git Bash or Windows Terminal for Windows)
- Basic understanding of SSH protocol
🔐Understanding SSH (Secure Shell)
SSH is a cryptographic network protocol that provides a secure channel over an unsecured network. It enables secure remote login, command execution, and file transfers between computers. SSH uses public-key cryptography to authenticate the remote computer and allow it to authenticate the user.
SSH Connection Flow
🚀Step-by-Step Initial Login Process
Step 1: Verify SSH Directory
Before connecting to your server, ensure the SSH directory exists in your home directory. This directory stores SSH configuration files, known hosts, and authentication keys.
The
.ssh directory is hidden (starts with a dot). Use the -a flag with
ls to view hidden files and directories.
Step 2: First SSH Connection
Connect to your server using the SSH command. The syntax follows the pattern:
ssh username@server-ip-address
Step 3: Accept Server Fingerprint
On first connection, you'll be prompted to verify the server's authenticity through its fingerprint. This is a crucial security measure to prevent man-in-the-middle attacks.
🔍What is a Server Fingerprint?
A server fingerprint is a unique identifier for your server's SSH key. It ensures you're connecting
to the correct server and not an imposter. The fingerprint is stored locally in the
~/.ssh/known_hosts file.
📁Understanding the known_hosts File
File Structure
The known_hosts file stores fingerprints of servers you've connected to. Each entry contains
three sections:
| Section | Description | Example |
|---|---|---|
| Hostname/IP | Server identifier (hashed for security) | |1|base64hash== |
| Key Type | Encryption algorithm used | ecdsa-sha2-nistp256 |
| Public Key | Base64 encoded server public key | AAAAE2VjZHNh... |
Viewing and Managing known_hosts
~/.ssh Directory Structure:
🛡️Server Hardening Process
Hardening involves securing the default settings of the operating system and individual software packages. Many services come with default configurations optimized for functionality rather than security. The hardening process addresses these vulnerabilities.
Hardening Strategy Overview
Replace default credentials with a strong, unique password
Add a standard user for daily operations
Enable administrative access for the new user
Prevent direct root access via SSH
Why Hardening Matters
🎯Security Risks of Default Configurations:
- Web Servers: Apache and Nginx may expose server information in error messages
- Database Systems: MariaDB/MySQL default configurations might allow remote root access
- SSH Service: Root login enabled by default increases attack surface
- Default Ports: Well-known ports are primary targets for automated attacks
Example: Apache Hardening
Example: MariaDB Hardening
📝Hardening Steps as Root User
Step 1: Change Root Password
The first security measure is to change the root password from the default or temporary password provided by your hosting provider.
💡Password Best Practices:
- Minimum 12 characters length
- Mix of uppercase, lowercase, numbers, and special characters
- Avoid dictionary words or personal information
- Use a password manager to generate and store strong passwords
Step 2: Create Non-Root User
Creating a non-root user for daily operations follows the principle of least privilege, reducing potential damage from compromised credentials.
Step 3: Grant Sudo Privileges
The new user needs sudo (superuser do) privileges to perform administrative tasks. This allows controlled elevation of privileges when needed.
Step 4: Disable Root Login (SSH Configuration)
Disabling direct root login via SSH is a critical security measure. Attackers often target the root account, and this configuration forces the use of individual user accounts.
⚠️Critical Warning:
Before disabling root login, ensure you have:
- Created a non-root user successfully
- Granted sudo privileges to that user
- Tested logging in with the new user account
- Verified sudo access works correctly
Failure to do so may lock you out of your server!
🔄Command History Navigation
Terminal efficiency tip: Use arrow keys to navigate through command history instead of retyping commands.
📊Summary Workflow Diagram
Complete Initial Setup Process
🎓Key Takeaways
Essential Security Principles:
- Defense in Depth: Multiple layers of security protect against various attack vectors
- Least Privilege: Users should have only the minimum permissions necessary
- Secure Defaults: Never rely on default configurations in production
- Regular Updates: Keep your system and software packages up to date
- Audit Trail: Using individual user accounts creates accountability
🔧Next Steps
After completing this initial hardening, additional security measures should include:
- Configure firewall (UFW - Uncomplicated Firewall)
- Set up SSH key-based authentication
- Install and configure fail2ban for intrusion prevention
- Enable automatic security updates
- Implement system monitoring and logging
- Regular security audits and vulnerability scanning
📚Additional Resources:
- Ubuntu Server Documentation: https://ubuntu.com/server/docs
- SSH Protocol Specification: RFC 4251-4254
- Center for Internet Security (CIS) Ubuntu Benchmarks
- NIST Cybersecurity Framework