ssh Command: How to Work Remotely Using SSH
While you’ll do most of your programming on your local machine, there may be times when you need to access another computer remotely. Once you’ve written a web application, you may want to upload it to a web server and you may want to sign in to the server to manage a database.
That’s where the ssh command comes in. ssh is a command that allows you to securely log in to remote machines. It’s the most common method used to access remote Unix servers.
In this guide, we will teach you how to connect to a remote server with ssh, configure ssh and sign in using an SSH key.
Getting Started with SSH
SSH stands for Secure SHell. It is a protocol used to connect to remote Unix-based servers such as a Debian- or Ubuntu-based server, or a macOS computer. The
command uses the SSH protocol to securely log in to a remote server from the command line.
As a handy reminder, SSH (with capitals) is the protocol, whereas ssh (without capitals) is the command.
The most common usage of the command is to specify the name of the host to which you want to connect as well as the user who will be accessing the host:
When you first log in to a system, you may be asked if you are sure you want to connect to a host. This is a security measure designed to ensure you don’t log in to a system without knowing about it. Once you type “yes” and accept that you want to connect to the server, you will be asked to enter your password.
As long as your password is correct, a Linux command line shell will open up which you can use to navigate around the remote server.
By default, SSH uses the port 22 on a computer. However, some systems host SSH tunnels on a separate port. To change the host port number you are using to connect to a computer, you can use the
ssh username@hostname -p 2222
Alternatively, you can specify the entire URL of an SSH session:
When you use this approach, you need to add “ssh://” before the “username” section of your ssh command. This ensures that your computer knows which protocol you want to use. If you don’t specify the “ssh://” part, an error will be returned.
How to Configure SSH
The ssh command is very versatile and supports configuration files. This means that you can create files in which particular commands are stored. By using configuration files, you can save time because you won’t have to write out the long-form ssh command every time you want to access a particular resource.
The structure of these SSH config files looks like this:
Host host User user Hostname hostname Port 2222
The “Host” value refers to the label you want to use when referencing the configuration. So, if you want to type “ssh server” to sign in to a server, you’d set this value to “server”. The “Hostname” value, on the other hand, refers to the address of the server to which you want to connect. This can be an IP address or a URL.
"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"
Venus, Software Engineer at Rockbot
Let’s say we want to create an SSH configuration file for an ssh command to a hypothetical Career Karma server. We could do so using this code:
Host web User careerkarma Hostname 192.168.1.99 Port 2222
To use our configuration, we could run this command:
This command will sign us in to the server 255.255.255.255 using the username “careerkarma” and the port 2222. It’s worth noting that every IP between the range of 192.168.1.1 and 192.168.1.255 is reserved for your local network. So, this IP will not take you anywhere.
How to Log In to SSH Using Keys
In the above examples, we’ve been relying on password authentication to sign in to our servers. While this is secure, it’s quite inconvenient to type in your password every time you want to sign in to a server, especially if your password is long.
That’s where key-based authentication comes in. This method of authentication relies on creating a pair of keys:
- Private key: This is located on your local computer (the client) and kept secret.
- Public key: This key can be uploaded to any server you want to access.
When you sign in to a server using a private key, the server will use your public key to send a message to your computer. If this message can be read by your computer, it means the private and public keys match. At this point, you’ll be allowed to log in to the server.
already have a public and private key, you can generate them on your local computer using ssh-keygen:
ssh-keygen -t rsa
Press enter as many times as it asks you to and accept the default values. You can skip this step if you already have a key, otherwise a new key will be generated that will replace your existing one. Once you have a key, you’re ready to copy it to a server.
To copy your keys to a server, you can use the
command. Here’s the syntax for this command:
ssh-copy-id -i ~/.ssh/id_rsa.pub hostname
This command will copy the key
from the .ssh folder in the home directory on your computer. This is where your public key is stored by default.
If you want to upload another public key to a server, you can change the value set using the
You can still use your password to sign in to the server, unless you disable password authentication.
ssh is a useful command to know, even if you’re not working with servers yet. For instance, if you have a Raspberry Pi, you could use ssh to access it from your main computer.
SSH is a secure protocol you can use to work with remote hosts based on Unix, like a macOS computer or a Linux server.