Set Up An SSH Server
Last updated: February 27th 2025
Introduction
Ever wondered what SSH is and why it is present everywhere, oh well, almost on any server? Read this article, and you'll know why. You'll also learn how to set up an SSH server.
So, let's get started!
Secure Shell: SSH
SSH, or Secure Socket Shell, stands as a cornerstone of secure network communication, acting as a protocol designed to establish encrypted channels between two networked devices. In essence, SSH ensures that data transmitted across a network remains confidential and protected from unauthorized access. This is achieved through robust encryption techniques, which scramble the information into an unreadable format during transit, and unscramble it only upon reaching its intended recipient.
Before the advent of SSH, network communication often relied on insecure protocols such as RSH (Remote Shell) and RLogin (Remote Login). These older protocols lacked encryption entirely, transmitting data in plain text. This fundamental weakness rendered them highly vulnerable to sniffing. Imagine a scenario where sensitive information, like usernames, passwords, or confidential documents, is sent using RSH or RLogin. Anyone with network sniffing tools within the communication path could intercept this plain text data, effectively gaining unauthorized access to sensitive credentials and information.
The lack of encryption in protocols like RSH and RLogin created a significant security risk. Malicious actors could easily monitor network traffic, passively collecting sensitive data or actively injecting malicious code into unsecure connections. This made them unsuitable for any environment where security and confidentiality were paramount.
SSH emerged as a direct response to these critical security flaws. By implementing strong encryption, SSH provides a secure alternative, safeguarding communication from eavesdropping, data tampering, and other forms of network-based attacks. This secure foundation makes SSH indispensable for a wide range of applications, including remote server administration, secure file transfer, and creating secure tunnels for other network services. In today's interconnected world, SSH's role in maintaining secure and trustworthy digital interactions is more critical than ever.
SSH Protocol vs. OpenSSH Implementation
To fully grasp SSH, it's crucial to distinguish between the protocol itself and its practical implementations. SSH, in its essence, is the theoretical blueprint – the defined set of rules and procedures that dictate secure communication. Think of it as the established grammar of a language. OpenSSH, conversely, stands as a prominent and widely adopted implementation of this SSH theory. Much like various dictionaries and style guides embody the grammar of English, OpenSSH is the SSH protocol in software form.
To leverage the power of SSH, a dual-component architecture is necessary: a server and a client. The SSH server software, with the OpenSSH server being a leading example, diligently listens for incoming connection requests. Upon a client initiating contact, the server undertakes a critical task – client authentication. This secure verification process confirms the client's identity, ensuring only authorized users gain access. Following successful authentication, the server traditionally grants access to a command-line shell, enabling remote control over the server system.
However, limiting SSH's utility solely to shell access overlooks its broader capabilities. The SSH protocol is remarkably versatile, extending its secure reach beyond mere shell sessions to encompass a diverse range of software applications. It adeptly establishes secure tunnels, creating encrypted pathways for other protocols. This versatility facilitates secure file transfers, protected database interactions, and much more. OpenSSH, as a robust and comprehensive implementation, mirrors this inherent flexibility, offering a suite of tools that transcend simple secure shell access and solidify SSH's position as a cornerstone of secure digital infrastructure.
Setup your SSH server
NOW, let's keep this SSH conversation rolling, shall we? We've talked about the magic of secure shells, how they're basically the digital tunnels you use to whisper sweet nothings (or, you know, critical commands) to your servers from afar. But just like any tunnel, you want to make sure it's well-built, secure, and doesn't lead to any unexpected… detours.
Today, we're diving deep into the nitty-gritty of SSH configuration, specifically zeroing in on where all the knobs and dials are located and how to crank up the security. Think of this as fortifying your digital castle. We're gonna explore the sacred grounds of SSH configuration files, peek into the super-secret .ssh
directory, and understand why permissions are more than just random numbers – they're the gatekeepers of your system.
So, grab your digital toolkit, and let's get our hands dirty with SSH configuration files and bulletproof security, shall we?
The Holy Trinity of SSH Configuration: Server, Client, and User
When we talk about SSH configuration, we're not just talking about one big settings panel to rule them all. Oh no, it's a bit more nuanced than that, which is actually a good thing because it gives you layers of control and customization. Think of it as an organizational chart, where different configurations apply at different levels. We’ve got three main players in this configuration game:
-
The Server Configuration (
/etc/ssh/sshd_config
): The Fortress BlueprintThis is the granddaddy of them all. The server configuration file, typically found at
/etc/ssh/sshd_config
(notice the extra 'd' insshd_config
– that 'd' stands for daemon, the background service that is the SSH server). This file is like the master blueprint for your SSH server. It dictates how the SSH server behaves globally for everyone who tries to connect to it.Inside this file, you'll find directives that control everything from which ports the server listens on, to which authentication methods are allowed (passwords? keys?), to connection timeouts, and a whole host of security settings. Tweaking this file is like setting the global rules for your digital fortress. Any changes you make here affect every single user attempting to SSH into your server. So, wield this power wisely!
-
The Client Configuration (
/etc/ssh/ssh_config
): Your Personal SSH RulebookNow, let's talk about the client side. Your machine, the one initiating the SSH connection, also has its own configuration file, typically located at
/etc/ssh/ssh_config
(no 'd' this time, just plainssh_config
for the SSH client itself). This file is like your personal SSH rulebook, but for your machine as an SSH client.This client configuration is used when you are the one initiating an SSH connection out to another server. It allows you to set default behaviors for your SSH client. For example, if you always connect to a certain server using a specific username or key, you can define that here so you don't have to type it out every single time. It's about making your SSH client connections smoother and more customized, acting as default instructions when you are the one doing the SSH-ing.
-
The User-Specific Client Configuration (
~/.ssh/config
): Your Custom SSH OverridesFinally, we have the user-specific client configuration, located in your home directory at
~/.ssh/config
. Notice the~
which is shorthand for your home directory. This file is your personal, custom SSH override zone. Think of it as your personal set of tweaks that sit on top of both the server's global rules and the system-wide client defaults.This
~/.ssh/config
file is where you, as a user, can define very specific settings for connecting to particular servers. It lets you create shortcuts, specify keys for certain hosts, and even override some of the global or system-wide client settings for your own connections. It's about a personalized SSH experience tailored just for you.For example, you might have different keys for different servers or prefer to use a specific port for one server but the default port for others. This user-level configuration is where you make those granular, user-specific adjustments.
Think of it in layers: The server config sets the overall server policy. The system-wide client config sets the default client behavior. And your user-specific configuration lets you personalize and override those defaults for your own connections. It's a beautifully layered system that provides both global control and individual flexibility.
Securing Your .ssh
Directory – Digital Fort Knox
Now, let's talk about the absolutely, positively, non-negotiably crucial .ssh
directory, usually found in your home directory at ~/.ssh
. This directory is the digital equivalent of Fort Knox. Inside, you'll find your private keys, your public keys, and your user-specific SSH configuration files. In short, it’s the heart of your SSH security. If this directory is compromised, you’re in deep digital trouble.
Therefore, securing this directory is not just a good idea – it's absolutely essential. The primary tool for securing directories (and files in general) in the Linux/Unix world is chmod
, the command to change file mode bits – essentially, setting permissions.
Let's look at the command we use to lock down the .ssh
directory tighter than a drum:
chmod 700 ~/.ssh
Now, those numbers – 700
– they might look like random digits, but they are a coded language of permissions. Let's decode this permission magic:
When you run ls -ld ~/.ssh
, and see something like:
drwx------ 3 ahmad ahmad 4096 Feb 16 16:51 /home/ahmad/.ssh
Let's break down each part of drwx------
:
-
d
– As our output clearly shows, the very first character,d
, is a dead giveaway: Directory! This just confirms we are indeed looking at directory permissions, not file permissions. -
rwx------
- This is the permission string itself, and it's divided into three sections, each representing a category of user:-
rwx
(Owner Permissions): The first set of three characters (rwx
) applies to the owner of the directory, which in this case, is the userahmad
.r
(Read): The owner can list the contents of the directory (see the files and sub-directories inside).w
(Write): The owner can create new files or directories within this directory, and also rename or delete existing files and directories within it.x
(Execute): For directories, "execute" permission means the ability to enter the directory – tocd
into it, to access files and directories within it if you have permission to do so.
-
------
(Group Permissions): The next three characters (---
) apply to the group associated with the directory. In this case, the group is alsoahmad
. All dashes (-
) mean no permissions whatsoever for the group. No read, no write, no execute. Nada. -
------
(Others Permissions): The final three characters (---
) apply to everyone else, anyone who is not the owner and not in the group associated with the directory. Again, all dashes mean absolutely no permissions.
-
-
3
– This number,3
, indicates the number of hard links to this directory. For most practical purposes in this context, you can often ignore this number. It's more of a file system detail. -
ahmad ahmad
– These are the owner and group names. In this case, both the owner and the group areahmad
.
Deciphering chmod 700 ~/.ssh
:
So, when you run chmod 700 ~/.ssh
, you are essentially telling the system: "Set the permissions of the .ssh
directory to be rwx
for the owner, and no permissions for group and others."
The number 700
is just a shorthand way of representing these permissions in octal. Each digit corresponds to owner, group, and others, and each digit is a sum of the following values:
4
for read (r
)2
for write (w
)1
for execute (x
)0
for no permission
So, 7
(owner) is 4 + 2 + 1
(read + write + execute). 0
(group) and 0
(others) mean no permissions at all.
What This Means in Plain English:
chmod 700 ~/.ssh
makes your .ssh
directory completely private to you, and only you. No other user on the system, and no process running under a different user account, can even look inside this directory, let alone modify anything within it. It's like locking up your most precious digital secrets in a vault where only you have the key. And in the world of SSH, your private keys are indeed some of your most precious digital secrets!
Key-Based Authentication: The authorized_keys
File – Ditch the Passwords, Embrace the Keys!
Finally, let’s talk about the powerhouse of SSH security: key-based authentication, and the file that makes it all happen – authorized_keys
.
Located in /home/:user/.ssh/authorized_keys
(where :user
is the username on the server), this file is where you store the public keys of users who are allowed to log in to this server account.
The magic of key-based authentication is in the public-private key pair. You generate a pair: a private key (which you must keep absolutely secret and secure, ideally protected by a passphrase) and a public key (which you can freely share).
Here’s how it works in a nutshell:
- Key Generation: You generate a key pair on your client machine (using
ssh-keygen
). This creates a private key and a public key. - Public Key Distribution: You copy your public key to the
authorized_keys
file on the server in the user’s.ssh
directory (e.g., usingssh-copy-id
or manually copying the contents). - Authentication Dance: When you try to SSH into the server using key-based authentication:
- The SSH client sends a request to authenticate using a key.
- The SSH server checks the
authorized_keys
file for a matching public key for the user you are trying to log in as. - If it finds a match, the server challenges your client to prove you possess the corresponding private key (without the client actually revealing the private key itself).
- Your SSH client uses your private key to perform cryptographic magic that proves you have the private key that matches the public key in
authorized_keys
. - If the proof is valid, BAM! You are authenticated and logged in without ever needing to type a password.
Adding a Key to authorized_keys
:
The simplest way to add your public key to the authorized_keys
file on a server (assuming you already have SSH password authentication enabled temporarily) is using the ssh-copy-id
command:
ssh-copy-id user@your_server_ip
This command will:
- SSH into
your_server_ip
asuser
. - Prompt you for the password (initially).
- Append your public key (typically
~/.ssh/id_rsa.pub
or~/.ssh/id_ed25519.pub
) to theauthorized_keys
file inuser
's.ssh
directory on the server.
Once you've done this, you should be able to SSH into the server without a password, using key-based authentication.
Why Key-Based Authentication is King:
- Security: Much more secure than passwords. Passwords can be guessed, brute-forced, or phished. Key-based authentication relies on cryptographic key pairs that are extremely difficult to crack.
- Convenience: Once set up, you no longer have to type passwords every time you SSH. Especially useful for automated scripts and frequent server access.
- Disabling Password Authentication: For maximum security, you can disable password authentication entirely in your server's
sshd_config
file (usingPasswordAuthentication no
). This forces everyone to use key-based authentication, significantly reducing the risk of brute-force password attacks.
Conclusion
This article touched upon what SSH is and how to set up a server with the recommended settings. And let's remind both of us: always use SSH keys over passwords!
This article was written by Ahmad Adel. Ahmad is a freelance writer and also a backend developer.