Tai Hoang

How to Connect to a Server via SSH

SSH (Secure Shell) allows you to securely log in to a remote server, execute commands, and transfer files. In this article, I’ll walk you through how to connect to an Ubuntu server from a macOS, Linux, and Windows computer, using SSH key.

What You Need Before Connecting

  • IP address of the remote server, e.g., 203.10.109.25.
  • Username on the server: Often ubuntu, root, or a user you created.
  • SSH credentials: Either a password or a private key file (like .pem, .ppk, or .key).
  • SSH client: You can use Terminal (macOS / Linux), or PowerShell(Windows).

Preparing the Private Key

When creating a new Instance (server) on cloud platforms, like AWS or OCI, you will have an option to generate a private key (*.key or *.pem) that you can download to your computer. You’ll need this key to connect to that server via SSH later. Keep it in private because anyone having this key can log in to your server.

Let’s say my private key file is blog.key.

Creating an alias

Next, I need to create a SSH alias, or a shortcut, to connect to my remote server. For example, to connect to my blog server, I can use this command:

ssh blogCode language: plaintext (plaintext)

‘blog’ is the alias that I assigned to my remote server. If I don’t use alias, I have to type a longer command, like this:

ssh -i ~/.ssh/blog.key [email protected]Code language: plaintext (plaintext)

To create alias:

On macOS/Linux:

Copy your private key to /Users/your-username/.ssh/ folder.

Open Terminal and use this command to open the config file:

sudo nano ~/.ssh/configCode language: plaintext (plaintext)

Type your login password if prompted, then hit Enter. The text editor will open that file.

Copy this text to the start or the end of the file:

Host blog
    HostName 203.10.109.25
    User ubuntu
    IdentityFile /Users/your-username/.ssh/blog.key
  • blog: The alias.
  • 203.10.109.25: The remote server’s IP address, change this to your server’s IP.
  • ubuntu: Default username on the server, change this if you changed your user.
  • /Users/your-username/.ssh/blog.key: Location of the private key.

Press Ctrl + X to close the text editor, then press Y (Confirm) to Save the file.

On Windows:

Open PowerShell and use this command to open or create the config file with Notepad:

notepad $HOME\.ssh\configCode language: plaintext (plaintext)

Copy this text to the start or the end of the file:

Host blog
    HostName 203.10.109.25
    User ubuntu
    IdentityFile C:\Users\your-username\.ssh\blog.keyCode language: plaintext (plaintext)

Press Ctrl + S to save the file, then close it.

Connect to a Remote Server via SSH

On macOS / Linux:

Open Terminal and use this command to make the private key Read-only to the file owner:

chmod 400 /Users/your-username/.ssh/blog.key

Then use this command to start the connection:

ssh blogCode language: plaintext (plaintext)

On Windows:

Open PowerShell and use this command:

ssh blogCode language: plaintext (plaintext)

If the connection is successful, you will see a welcome message like “Welcome to Ubuntu…” Now you can start exploring.

Reply via email