How To Use SSH

SSH is one of those useful tools that can be very overwhelming at first, if only because you may not know where to start. It’s pretty easy for someone to say “well just start at the beginning”, but when you don’t know what the beginning even is, you might become lost before you’ve even started.

In this article we’re going to go through the basics of using SSH, from installation to configuration to creating a tunnel. Is this an exhaustive tutorial? No. There will be much that I do not cover here, but it should give you a really good starting place.

What is SSH and Why Should You Learn It?

To put it in simple terms, SSH is a technology that lets you connect one computer to another, usually on the same Local Network. There’s obviously a lot more to it, but if you are only looking for what SSH is, that’s it.

Now, as to why you should learn it? Well, there could be many reasons. Maybe you have a home server you’d like to service or interact with from your main computer (nothing is more annoying than having to hook up a mouse and keyboard to a server that doesn’t usually need one). Or, perhaps, you just want to move one file from your desktop to your laptop.

SSH opens up a whole world of opportunities for you to interact with all the machines on your network. Is it useful if you don’t own multiple computers? Perhaps. Because SSH can be used outside of your network as well. If you have a VPS (Virtual Private Server), you can use SSH to connect and interact there as well.

Installing SSH Capabilities

In order to use SSH, both of the computers you want to connect together (whether local or remote) need to have the ability to actually use SSH. That means you have to install it.

In order to install SSH, you’ll need your package manager of choice, and the package name your distro uses for openssh. As of writing here are a few:

DistroClientServerNotes
ArchopensshopensshOne package for both
Debianopenssh-clientopenssh-serverClient is usually preinstalled; ssh is a metapackage pulling both
Ubuntuopenssh-clientopenssh-serverSame as Debian
Mintopenssh-clientopenssh-serverSame as Ubuntu
Fedoraopenssh-clientsopenssh-serverNote the plural on the client package; openssh is the shared base
openSUSEopenssh-clientsopenssh-serverSplit out of the old monolithic package; openssh is now a metapackage
NixOSopensshservices.openssh.enable = true;Client ships in the base system

If you’re on NixOS, you don’t really install the server, you turn it on in your config:

services.openssh = {
  enable = true;
  settings.PasswordAuthentication = false;
  settings.PermitRootLogin = "no";
};

As you can see, on several distros, the client and server packages are separate. If you plan on using SSH to and from all your computers, you’ll need both, if you plan on only going out with your SSH, you’ll only need the client package.

Client vs. Server

Client and server aren’t types of machines, they’re roles in a connection. The ssh command you type is the client. The sshd daemon listening on the other end is the server. That means the machine you’re sitting at is the client, and the one you’re connecting to is the server — for that connection.

A single box is frequently both. Your home server runs sshd so you can log into it, but you might also SSH from it to something else, and now it’s acting as a client too. For the rest of this tutorial, when I say client, I mean the machine you’re sitting in front of.

The Services

SSH runs as a Systemd service on distros that use Systemd, which most do. If you’re running a non-Systemd distro, you are probably familiar enough with those init systems to get around this section just fine.

So, in order to actually get SSH up and running after install, you may have to start those services. Some distros will start them for you upon install, some will not. Check:

systemctl status sshd

If that comes up as “running”, then you’re good to go. If not, you’ll need to start the sshd service:

sudo systemctl enable --now sshd

Note: On Debian, Ubuntu, and Mint, the service is called ssh, not sshd. So if the commands above can’t find anything, swap in ssh and try again.

There’s a small chance that you’ll need to log out and log back into your computer if you face connection issues, though this is rare.

Your First Connection

There are many ways to get started once you actually get SSH up and running on both the client and the server. But almost always your first connection is going to require the local (LAN) IP address of the server. To get that, from a terminal:

ip a

This will list out something that looks like this:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute 
       valid_lft forever preferred_lft forever
2: enp6s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether b4:2e:99:f8:2a:4a brd ff:ff:ff:ff:ff:ff
    altname enxb42e99f82a4a
    inet 192.168.68.69/22 brd 192.168.71.255 scope global dynamic noprefixroute enp6s0
       valid_lft 4336sec preferred_lft 4336sec
    inet6 fe80::6a8c:e82e:6a4e:4586/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
3: wlp5s0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 06:7a:c5:40:ef:36 brd ff:ff:ff:ff:ff:ff permaddr 40:ec:99:62:7c:1c
    altname wlx40ec99627c1c

That looks like a bunch of nonsense to the non-techy person, so you’ll be relieved to know you can ignore most of it. What you need is the inet line of the connection your server is using. So if it’s connected via ethernet, it will likely be enp6s0 or something that looks like that. If it is connected via WiFi, it will look like something like wlp5s0. These may be different on your machines, but they will look similar to this.

The actual number you need is the 192.xxx.xx.xx number. Again, this may not be the same for your network. Some routers use different LAN IP ranges, but the vast majority use 192 numbers.

In the example above, my IP address for the server would be 192.168.68.69.

Let’s Talk About Ports for A moment.

For the most part I’m going to tell you that you do not need to worry about the SSH port. SSH defaults to port 22, so don’t use that for something else, but otherwise you should be able to completely ignore the SSH port.

But…

You may come across wonky interference or perhaps you’re on a shared VPS and they have to worry about port allocation. Then you may find that the port has been changed to something else. A lot of times you’ll see it as port 2222, sometimes something else.

I leave this here, because if you have trouble connecting, and it’s not your firewall, the port may be the issue. Check your ssh config on both machines.

You may have noticed that in the ip a output above, my ethernet connection said 192.168.68.69/22. Don’t let that fool you. That 22 isn’t the port, it’s part of how your network is divided up (the subnet), and it has nothing to do with SSH. ip a won’t tell you what port SSH is using. That’s set on the server, in a file called /etc/ssh/sshd_config, and we’ll get to that file later on. If there’s no Port line in there, or it’s commented out, you’re on 22 and you’re good to go.

If your server is on a different port, you’ll need to tell your client about it. The quickest way is right on the command line:

ssh -p 2222 user@ipaddress

But if you don’t want to type that every time, open (or create) your SSH config at ~/.ssh/config on your client and put this in there, changing the number of the port to whatever you need it to be:

Host web
    HostName 192.168.68.69
    Port 2222

Note: web is just a nickname for the server. It can be anything you want, and it’s what you’ll type when you connect. HostName is the actual address of the server.

Finally Connecting

The two main commands you can use to connect to your server are:

ssh user@hostname or ssh user@ipaddress

Now, the thing is, the first one is very likely not to work. Often using just the hostname of the server won’t resolve, meaning you’ll not be able to connect.

Whether it works or not really depends on your network. Some routers will hand out names for the devices on your network, and some distros come with something called Avahi, which lets you reach other machines with hostname.local. If either of those are set up, great. If not, don’t sweat it. Just use the IP address. Later on, we’re going to make it so you never have to type that IP address again anyway.

So, for me, that’s:

ssh [email protected]

The matt part is the username on the server, not on the machine you’re sitting at. If those happen to be the same, you can actually leave it off entirely:

ssh 192.168.68.69

SSH will just assume you want the same username you’re logged in with.

The Scary Fingerprint Message

The very first time you connect to a server, you’re going to get something like this:

The authenticity of host '192.168.68.69 (192.168.68.69)' can't be established.
ED25519 key fingerprint is SHA256:Xq3cR1Fh0kLmTq9yE2vB7nP4sWdA6uJxZ8oKiG5tHcM.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])?

This freaks a lot of people out the first time they see it. It shouldn’t. Every SSH server has its own key, kind of like an ID card, and this is just your computer saying “Hey, I’ve never seen this machine before. Are you sure it’s the one you think it is?”

On your home network, the answer is almost always yes. Type out yes (the whole word, y won’t cut it), hit enter, and then put in the password for your user on the server.

Once you say yes, your computer saves that key in a file at ~/.ssh/known_hosts. Every time you connect after that, it checks the server against what it saved. If it matches, you’ll never hear about it again.

If it doesn’t match, you’re going to get a very loud warning that says something like WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! and SSH will flat out refuse to connect.

Is somebody trying to hack you? Probably not. Nine times out of ten, this happens because you reinstalled the OS on your server, or your router gave that IP address to a different machine. Both of those mean a new key. If you know that’s what happened, you can tell your computer to forget the old one:

ssh-keygen -R 192.168.68.69

Then connect again, and you’ll get the fingerprint question like it’s the first time.

If you didn’t reinstall anything, and nothing changed on your network? That’s when you stop and figure out why before you go typing yes.

You’re In. Now What?

Once you’re connected, your terminal is now the server’s terminal. Anything you type runs on the server, not on your computer. Your prompt will usually change to show the server’s hostname, and that is something you want to keep an eye on. You do not want to run sudo reboot on your desktop thinking you’re on the server, or the other way around.

When you’re done, type exit or hit Ctrl + D. That’ll drop you right back on your own machine.

Key-Based Authentication

If you only take one thing away from this article, make it this section.

Typing in a password every time you connect gets old fast. It’s also not the most secure way to do it. Passwords can be guessed, and if your server is out on the internet, there are bots out there trying to guess them all day, every day. Keys fix both of those problems.

The way it works is you make a pair of keys. One is private. It stays on your computer, and you never give it to anyone. The other is public. That’s the one you put on any server you want to log into. When you connect, the server uses your public key to check that you have the private one, without the private key ever leaving your machine. No password needed.

Making Your Key

On your client:

ssh-keygen -t ed25519 -C "matt@desktop"

The -t ed25519 is the type of key. You’ll see a lot of older tutorials tell you to use RSA. RSA still works, but ed25519 is newer, the keys are a lot shorter, it’s faster, and there’s less for you to get wrong. Newer versions of OpenSSH actually use it by default. Unless you’re connecting to something ancient, use ed25519.

The -C is just a comment. It gets stuck onto the end of your public key so you can tell which key is which later on. I like to use something that tells me which machine the key came from. It doesn’t do anything else, so put whatever you want there.

It’s going to ask you where to save the key. Just hit enter. The default is ~/.ssh/id_ed25519, and that’s where SSH is going to look for it.

Passphrase or No Passphrase?

Next, it’s going to ask you for a passphrase. You can leave this blank, and a lot of people do.

Here’s the thing though. Without a passphrase, anyone who gets a copy of your private key file can get into every server that key unlocks. With a passphrase, that file is useless without it.

So, should you use one? If it’s a laptop that leaves the house, yes. Absolutely. If it’s a desktop that never leaves your office and only talks to the server in your closet, it’s much less of a concern. That’s your call. And if typing a passphrase sounds just as annoying as typing a password, hang on, because we’ll fix that in a minute.

Once it’s done, you’ll have two files:

  • ~/.ssh/id_ed25519 — your private key. This never leaves your computer. Don’t share it, don’t post it, don’t email it to yourself.
  • ~/.ssh/id_ed25519.pub — your public key. This is the one you hand out.

Getting Your Key to the Server

The easy way:

ssh-copy-id [email protected]

It’ll ask for your password one last time, copy your public key over, and put it right where it needs to go. Now try connecting again:

ssh [email protected]

No password. (If you set a passphrase, it’ll ask you for that instead. That’s normal. That’s your computer unlocking your key, not the server asking for a password.)

When ssh-copy-id Isn’t There

Every now and then you’ll run into a machine that doesn’t have ssh-copy-id. You can do the same thing by hand:

cat ~/.ssh/id_ed25519.pub | ssh [email protected] 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'

That looks like a lot, but all it’s doing is reading your public key, connecting to the server, making the .ssh folder if it isn’t there already, and adding your key to the end of a file called authorized_keys. That file is just a list of every public key that’s allowed to log in as that user, one key per line.

Do note, that’s >>, two arrows. One arrow would wipe out any keys that were already in there and replace them with yours. Don’t do that.

Permissions, AKA “Why Isn’t This Working?”

This is the one that gets everybody, and it gets everybody because SSH doesn’t tell you what’s wrong.

SSH is very picky about who can read your key files. If the permissions are too open, it’ll just quietly ignore your key and ask for a password like you never set anything up at all. No error. Nothing. You’ll sit there wondering what you did wrong.

So, on the server:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

And on your client:

chmod 600 ~/.ssh/id_ed25519

700 means only you can get into the folder. 600 means only you can read and write the file. ssh-keygen and ssh-copy-id set these for you, so if you went that route, you’re probably fine. But if you did it by hand, or you copied your keys over from another machine, check this first. Your home directory also can’t be writable by anyone but you, so if you’ve gotten creative with the permissions there, that’ll break it too.

ssh-agent: Typing Your Passphrase Once

If you set a passphrase, you’ve probably noticed that it asks for it every single time. That gets old too. That’s what ssh-agent is for. It holds onto your unlocked key so you only have to type the passphrase once.

Some desktops, GNOME in particular, already run one for you, and you’ll get a little window asking for your passphrase the first time. If yours doesn’t, you can start one yourself:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519

The first line starts the agent, the second adds your key to it. Type your passphrase once, and you’re set until you log out.

You can also put AddKeysToAgent yes in your SSH config, and SSH will hand your key to the agent automatically the first time you use it. Which brings us to…

The SSH Config File

Remember that ~/.ssh/config file from earlier? I only showed you a tiny piece of it. This file is, hands down, the biggest quality of life improvement you can make with SSH, and for some reason most tutorials bury it at the very end, if they mention it at all.

Here’s what a full entry looks like:

Host web
    HostName 192.168.68.69
    User matt
    Port 2222
    IdentityFile ~/.ssh/id_ed25519

Now, instead of typing ssh -p 2222 [email protected], you type:

ssh web

That’s it. That’s the whole thing. No IP address to remember, no username, no port.

Let’s break that down:

  • Host is the nickname. This is what you type.
  • HostName is the real address. An IP address or a domain name both work.
  • User is your username on the server.
  • Port is the port. If it’s 22, you can leave this line out.
  • IdentityFile is which key to use. If you only have the one key and it’s in the default spot, SSH will find it on its own, but it doesn’t hurt to be specific. And if you ever end up with more than one key, you’ll need it.

You can add as many of these as you want. Every machine on your network can have its own entry.

And here’s the best part. Everything else that uses SSH reads this file too. scp, sftp, rsync, sshfs, all of it. So once web works for ssh, it works everywhere.

Defaults For Everything

Host * matches every host, so anything you put under it applies to every connection you make:

Host *
    ServerAliveInterval 60
    ServerAliveCountMax 3
    AddKeysToAgent yes

Do note, SSH reads this file top to bottom, and the first value it finds for a setting is the one that wins. So Host * goes at the very bottom of the file. If you put it at the top, it’ll override all of your specific entries below it, and you’ll be very confused.

Wildcards

That * works in other places too. Say all of your home machines are on the same network:

Host 192.168.68.*
    User matt

Now any IP address in that range will log in as matt by default. Keep in mind, this only matches what you actually type after ssh. So ssh 192.168.68.70 will match it, but ssh web won’t, even though web points to an address in that range.

Connections That Keep Dropping

If you’ve ever walked away from an SSH session for a while and come back to a frozen terminal, that’s usually your router, or a firewall somewhere, deciding the connection was dead and dropping it.

That’s what ServerAliveInterval 60 up there is for. It has your client send a tiny “still here” message every 60 seconds, so the connection never looks idle. ServerAliveCountMax 3 means that if the server misses three of those in a row, SSH gives up and disconnects you, instead of leaving you staring at a terminal that doesn’t do anything.

And if you do end up with a frozen session? Hit Enter, then ~, then .. That’s SSH’s secret escape hatch. It’ll kill the connection and give you your terminal back. Very few people know about this one.

Moving Files Around

Logging into another computer is great, but a lot of the time, what you really want is to get a file from here to there. SSH has you covered there too.

scp

scp is the simplest. It works just like cp, except one side of it is on another machine:

scp file.txt web:/home/matt/

That copies file.txt from your computer to your home folder on the server. To go the other way:

scp web:/home/matt/file.txt .

The . just means “right here”, the folder you’re currently in. The way to remember it is it’s always scp from to, and whichever side has the colon in it is the server.

For a whole folder, add -r:

scp -r Documents web:/home/matt/

If you’re not using the config file and your server is on a different port, it’s -P, capital P. Not lowercase like ssh. Because of course it is.

Now, you may see people say that scp is deprecated. That’s sort of true. The old scp protocol is outdated, and newer versions of OpenSSH have actually switched the scp command to use SFTP behind the scenes. The command itself isn’t going anywhere any time soon, so use it if you like it. But it’s worth knowing the other two.

sftp

sftp is more interactive. Instead of a one-shot copy, you connect, and you get a little prompt where you can look around and grab what you need:

sftp web

From there, the commands you’ll actually use are:

  • ls and cd move around on the server
  • lls and lcd do the same thing on your computer (the l is for local)
  • get file.txt downloads a file
  • put file.txt uploads a file
  • exit gets you out

It’s handy when you don’t remember exactly where a file is on the server, and you need to poke around a bit first.

rsync

This is the one most people end up using. Myself included.

rsync -avz -e ssh Documents/ web:/home/matt/Documents/

The -a is archive mode, which keeps your permissions, timestamps, and all of that intact, and goes through folders. -v is verbose, so you can see what it’s doing. -z compresses things on the way over. The -e ssh tells rsync to use SSH. These days rsync uses SSH by default, so you can actually leave that part off, but you’ll see it in a lot of tutorials, so I wanted you to know what it was.

The big reason to use rsync is that it only copies what changed. Run that same command again tomorrow, and it’ll only send the files that are new or different. If a big transfer gets interrupted, run it again and it picks up where it left off, more or less. That’s why it’s great for backups.

Two things to note:

  1. rsync has to be installed on both machines. It’s not part of SSH.
  2. The trailing slash matters. Documents/ means “the stuff inside Documents.” Documents with no slash means “the Documents folder itself.” Get that wrong, and you’ll end up with Documents/Documents on the server. Not that I’ve ever done that.

If you’re not sure what a command is going to do, add --dry-run and rsync will tell you what it would have done, without actually doing anything.

Running Commands Without Logging In

You don’t actually have to log in to run something on your server. You can just tack the command on the end:

ssh web 'uptime'

That connects, runs uptime on the server, shows you the output, and disconnects. You never get a shell at all.

This works with sudo too, but you’ll want to add -t:

ssh -t web 'sudo systemctl restart nginx'

The -t gives the command a proper terminal so sudo can ask you for your password. Without it, sudo will usually just complain and quit.

You can also pipe things, and this is where it gets fun:

ssh web 'cat /var/log/syslog' | grep error

The part in quotes runs on the server. The grep runs on your computer. So this pulls the log over to you and searches it locally. You could also put the whole thing in the quotes, ssh web 'grep error /var/log/syslog', and then the searching happens on the server, and only the results come back. For a big log file, that’s the better idea.

(Note that a lot of distros don’t have /var/log/syslog anymore, because Systemd keeps logs in the journal. If that’s you, ssh web 'journalctl -p err -b' will get you the errors since the last boot.)

Tunneling and Port Forwarding

This is where SSH goes from “handy” to “how did I ever live without this?”

SSH doesn’t just carry your terminal. It can carry other traffic through that same encrypted connection. That’s what tunneling is. It sounds complicated, and the commands look complicated, but once you get the idea, it’s really not that bad.

Local Forwarding

Let’s say your server has a web admin page on port 80, but it’s only listening on the server itself, or your firewall blocks it. You can’t get to it from your computer. Well, you can with this:

ssh -L 8080:localhost:80 web

Now open your browser and go to localhost:8080. You’re looking at the admin page on your server.

The way to read that -L part is “take port 8080 on my computer, and send it to localhost:80, as seen from the server.” That’s the tricky bit. That localhost in the middle is the server’s localhost, not yours. You can even put a different machine’s address in there, as long as the server can reach it.

Remote Forwarding

This is the same thing, but backwards:

ssh -R 8080:localhost:3000 web

Let’s say you’ve got something running on your computer on port 3000, like a website you’re working on. This makes it show up on the server at port 8080. By default, it only shows up on the server itself, not to the whole world, which is probably what you want anyway.

I’ll be honest, most people won’t need this one very often. But when you need it, you really need it.

Dynamic Forwarding (SOCKS)

ssh -D 1080 web

This one turns your SSH connection into a proxy. Set your browser to use a SOCKS5 proxy at localhost, port 1080 (in Firefox, that’s under Settings, then Network Settings), and all of your browsing goes out through your server instead of the network you’re on.

Think of it as a poor man’s VPN. Sitting on coffee shop WiFi and want your browsing to come out of your home connection instead? This is how you do it.

Running Tunnels in the Background

All three of those open a normal shell on the server too, which you probably don’t need if all you want is the tunnel. That’s what these are for:

ssh -N -f -L 8080:localhost:80 web

-N means don’t open a shell or run anything, just make the tunnel. -f sends it to the background so you get your terminal back. Just remember it’s running. To stop it, you’ll have to find it with ps aux | grep ssh and kill it. Honestly, if you only need the tunnel for a little while, leave off the -f, let it sit in a terminal, and hit Ctrl + C when you’re done.

And yes, these can go in your config file too:

Host web-admin
    HostName 192.168.68.69
    User matt
    LocalForward 8080 localhost:80

Jumping Through Another Machine

Sometimes you can’t get to a machine directly. You have to go through another machine first. In the business world, that middle machine is called a bastion, or a jump host. At home, it might be the one machine you’ve exposed to the internet, with everything else hidden behind it.

You could SSH into the first one, and then SSH again from there. Or you could do it in one shot:

ssh -J bastion matt@internal

SSH connects to bastion, then hops right on through to internal, and your key never has to be copied to the middle machine. In the config file:

Host internal
    HostName 10.0.0.5
    User matt
    ProxyJump bastion

Now ssh internal just works, and you never have to think about the hop again.

Hardening Your Server

If your server never leaves your home network, honestly, a lot of this is optional. It’s still a good idea, but it’s optional.

If your server is on the internet, like a VPS, it is not optional. The second a server with SSH open shows up on the internet, the bots start knocking. Not in days. In minutes.

Before you change anything: make sure key login works. Seriously. Log in with your key and make sure it doesn’t ask for a password. And keep that session open while you work. If you mess something up, that open session is how you fix it. Otherwise, it’s time to find a keyboard and monitor, or call your VPS provider.

All of the server settings live in one file:

sudo nvim /etc/ssh/sshd_config

Note the d in sshd_config. That’s the server file. There’s also an ssh_config without the d, and that’s for the client. Don’t mix those up.

One more thing to check. On a lot of distros, there’s a line near the top of that file that pulls in extra files from /etc/ssh/sshd_config.d/. Remember how in the client config, the first value wins? Same thing here. So if something in that folder sets a value, it beats whatever you put in the main file. VPS images especially like to drop a file in there that turns password logins right back on. Take a look in that folder before you go any further.

Turn Off Passwords

PasswordAuthentication no

This is the big one. Once this is set, the only way in is with a key. All those bots guessing passwords? They can guess all day. It doesn’t matter anymore.

On some distros, you’ll also want:

KbdInteractiveAuthentication no

That closes a side door that can still end up asking for a password even with the first setting turned off.

Turn Off Root Login

PermitRootLogin no

Every Linux machine has a root user, so it’s the first username every bot tries. There’s no reason to let anyone log in as root directly. Log in as yourself, and use sudo.

Changing the Port

Port 2222

I’m going to be honest with you here. This isn’t really security. Anybody who actually wants to find your SSH port can find it in about thirty seconds. This is obscurity.

What it does do is cut way down on the junk in your logs, because most of the bots only ever try port 22. That’s nice. But it doesn’t replace anything else in this section.

If you do change it, remember to open the new port in your firewall before you restart. And on Fedora, SELinux needs to be told about the new port as well:

sudo semanage port -a -t ssh_port_t -p tcp 2222

On newer versions of Ubuntu, you’ll also need to run sudo systemctl daemon-reload and sudo systemctl restart ssh.socket before the new port actually takes.

Only Let Certain Users In

AllowUsers matt

With this set, only the users you list can log in over SSH. Everybody else gets turned away, even if they have a valid key. If you’d rather work with groups, there’s AllowGroups as well.

Test Before You Restart

Before you restart anything, run:

sudo sshd -t

This checks your config for mistakes. If it prints nothing, you’re good. If it prints something, fix it first. A typo in this file can stop sshd from starting at all, and that means you’re locked out.

Once it’s clean:

sudo systemctl restart sshd

(Or ssh on Debian, Ubuntu, and Mint, same as before.)

Then, and this is important, open a new terminal and try to connect. Don’t close your old session until you know the new one works.

If you’re on NixOS, all of this goes in that services.openssh block from the beginning of this article:

services.openssh = {
  enable = true;
  ports = [ 2222 ];
  settings = {
    PasswordAuthentication = false;
    KbdInteractiveAuthentication = false;
    PermitRootLogin = "no";
    AllowUsers = [ "matt" ];
  };
};

fail2ban

Finally, if your server is on the internet, look into fail2ban. It watches your logs, and if an IP address fails to log in too many times, it bans it for a while. With passwords turned off, it’s not doing as much heavy lifting as it used to, but it cuts down on the noise, and it’s nice peace of mind. Setting it up is an article on its own, so I’ll leave it there for now.

Odds and Ends

These didn’t really fit anywhere else, but they’re all worth knowing about.

When Things Go Wrong

If a connection isn’t working and you have no idea why, add -v:

ssh -v web

That’ll print out everything SSH is doing as it connects. It’s a lot, but if you look through it, you’ll usually find your answer. Which key it tried, whether the server accepted it, and where it gave up. Need more? -vvv gives you even more detail. It’s a firehose, but sometimes that’s what you need.

You can check the server’s side too, with journalctl -u sshd (or -u ssh on Debian and friends).

Connection Multiplexing

Every time you SSH into something, it has to go through the whole connection and login process. It’s fast, but it’s not instant. Multiplexing lets you reuse one connection for everything:

Host *
    ControlMaster auto
    ControlPath ~/.ssh/cm-%r@%h:%p
    ControlPersist 10m

Now, the first time you connect to a server, SSH opens a connection and keeps it around. Every connection after that, whether it’s another terminal, an scp, or an rsync, rides on that same connection, and it’s instant. ControlPersist 10m keeps it open for ten minutes after you close your last session. The %r@%h:%p part just names the connection after your username, the host, and the port, so each server gets its own.

If it ever gets stuck, ssh -O exit web will close it down.

sshfs

sshfs lets you mount a folder from your server like it’s a drive on your computer:

mkdir ~/web
sshfs web:/home/matt ~/web

Now ~/web is your home folder on the server, and you can open it in your file manager, edit files, whatever you want. It’s not as fast as a real network share like NFS, but it takes about ten seconds to set up and you need nothing on the server except SSH. You’ll need to install the sshfs package. To unmount it:

fusermount -u ~/web

(On some distros, that’s fusermount3.)

tmux and screen

Here’s a fun one. You start a big job on your server, like a large download or a system update, and then your WiFi hiccups. Your SSH connection drops, and your job dies right along with it.

The fix is to run that job inside tmux (or screen, if you’re old school) on the server. SSH in, run tmux, and start your job. If your connection drops, the job keeps right on going. When you get back, SSH in and run:

tmux attach

And you’re right back where you left off. You can also leave on purpose with Ctrl + b, then d. Once you get used to this, you’ll do it for anything that’s going to take more than a couple of minutes.

X11 Forwarding

ssh -X web

This lets you run a graphical app on the server and have its window show up on your computer. It needs X11Forwarding yes in the server’s sshd_config. It’s slow, Wayland makes it more complicated, and these days it’s pretty niche. But it’s there, and every now and then it really comes in handy.

Wrapping Up

So, that was a lot. Let’s recap:

  • Install the client and server, and get the service running.
  • Connect with ssh user@ipaddress, and don’t panic about the fingerprint.
  • Make a key with ssh-keygen -t ed25519, send it over with ssh-copy-id, and check your permissions when things don’t work.
  • Use ~/.ssh/config. Seriously. Use it.
  • Move files with scp, sftp, or rsync.
  • Tunnel things with -L, -R, and -D.
  • If your server is on the internet, turn off passwords and root login.

Is that everything SSH can do? Not even close. But it’s more than enough to get you going, and it’s most of what I use day to day.

If you want to dig deeper, the man pages are actually really good for this. man ssh_config covers everything you can put in your config file, and man sshd_config covers everything on the server side. They’re long, but now that you know the basics, they’ll actually make sense.


Do you use SSH? What’s your favorite trick that I missed? Let me know in the comments below!

Support My Work

If you enjoy this content, consider supporting me on Patreon to help keep it going!

Become a Patron
← Previous Trackball Vs Mouse: Which Is Better