SSH (Secure Shell) is a secure protocol for accessing remote servers. It allows administrators to control and manage servers remotely. In this guide, we’ll explore how to connect to SSH from a Linux or Ubuntu desktop in three scenarios:
- Without specifying an SSH port (default port 22).
- With a custom SSH port.
- Using a custom port along with
.ppk
or.pem
files. - Steps to Save SSH Connections in the SSH Config File
1. Connect to SSH Without Specifying a Port
The default SSH port is 22
, so if the server uses the default port, no additional configuration is needed.
Steps to Connect
- Open the Terminal on your Linux/Ubuntu desktop.
- Use the following command:ReplaceÂ
ssh username@server_ip
username
with the SSH username andserver_ip
with the IP address of the remote server.
Example:
ssh root@192.168.1.100
What Happens:
- The SSH client connects to port
22
by default. - You’ll be prompted to enter the password for the user.
2. Connect to SSH with a Custom Port
If the server is configured to use a non-default SSH port, you need to specify it in the command.
Steps to Connect
- Determine the custom port number configured on the server (e.g.,
2222
). - Use the
-p
option in the SSH command to specify the port:Âssh username@server_ip -p port_number
Example:
ssh root@192.168.1.100 -p 2222
What Happens:
- The SSH client connects to the specified custom port.
- Enter the password when prompted.
3. Connect to SSH with a Custom Port Using .ppk
or .pem
Files
Private keys (.ppk
or .pem
) are often used for passwordless SSH authentication.
For .pem
File:
Ensure the
.pem
file is on your local system and has the correct permissions:chmod 600 keyfile.pem
Use the
-i
option with the SSH command to specify the private key:ssh -i /path/to/keyfile.pem username@server_ip -p port_number
Example:
ssh -i ~/keys/TokyoKey.pem ubuntu@192.168.1.100 -p 2222
For .ppk
File:
Linux SSH clients typically use .pem
files. To use a .ppk
file, convert it to .pem
format using
puttygen
:
- Install
putty-tools
if not already installed:Âsudo apt install putty-tools
- Convert the
.ppk
file to.pem
:Âputtygen keyfile.ppk -O private-openssh -o keyfile.pem
- Use the converted
.pem
file as shown in the previous example.
4. Steps to Save SSH Connections in the SSH Config File
Locate or Create the SSH Config File
- Open a terminal and navigate to the
.ssh
directory in your home folder:Âcd ~/.ssh
- If the
config
file doesn’t exist, create it:Âtouch config
- Open a terminal and navigate to the
Edit the Config File
- Open the
config
file in a text editor, such asnano
orvim
:Ânano config
- Open the
Add SSH Connection Details Add the details of your SSH connection to the file. Below are examples for each scenario:
Example 1: Default SSH Port
ÂHost myserver HostName 192.168.1.100 User root
Example 2: Custom SSH Port
ÂHost mycustomserver HostName 192.168.1.100 User root Port 2222
Example 3: Custom Port with
.pem
or.ppk
FileÂHost mykeyserver HostName 192.168.1.100 User ubuntu Port 2222 IdentityFile ~/keys/TokyoKey.pem
Save and Exit
- For
nano
, pressCtrl + O
, thenEnter
to save, andCtrl + X
to exit.
- For
Connect Using the Shortcut
- Use the
Host
name defined in the config file as a shortcut:orÂssh myserver
orÂssh mycustomserver
Âssh mykeyserver
- Use the
Benefits of Using the SSH Config File
- Shortcuts: No need to remember IP addresses, usernames, ports, or key file paths.
- Automation: Simplifies scripts or cron jobs that require SSH connections.
- Organization: Manage multiple server connections easily in one file.
Advanced Tip: Test Your Connection
To verify that your configuration works, use the -v
flag (verbose mode):
ssh -v myserver
Now, you can connect to your servers effortlessly with saved configurations!