Accessing a Linux server from another computer running macOS is many ways. You can use remote desktop protocol (RDP) to connect Mac to Linux easily. However, as a more secure instance, many people prefer to connect macOS to Linux via SSH (Secure Shell).

SSH creates a secure network where you can connect two devices easily. As many users prefer a lightweight OS, like Linux, to host their servers, SSH protocol is often developed and used. 

So, if you are struggling with the same question and want to learn how to quickly SSH to Linux on Mac, stay tuned till the end!

How can I SSH to Linux on a Mac?

You don’t need third-party apps like PuTTY on Mac to be able to use SSH protocol. You can use a Mac Terminal, run some commands, enter your credentials, and connect to a remote Linux server pretty easily.

Go through the following steps to SSH from Mac.

Find and open Terminal 

  • Click on the search bar or hit cmd + space to open the apps list 
  • Enter Terminal to find the app
  • Click the Terminal app software that pops up on the search list

Run SSH command

After opening the Terminal app, type in the following SSH command:

# ssh [email protected]

Replace (yourusername) with the username you’ve set for the Linux server. Then, type the server’s domain name after the @ mark. At last, fill the rest of the command with the required credentials before hitting the Enter key.

If you don’t have any domain, fill in the specified part with your server’s IP address before hitting Enter.

Specify the SSH port number.

Most servers’ SSH port number is 22. However, if your server’s port number differs, specify it in the previous command. So, in that case, use the following command instead:

ssh [username]@[ip-address] -p [port-number]

Change credentials as explained in the previous step and replace [port-number] with the port number considered for your remote SSH server. Then, move on to the next step.

If you don’t know your port number, run the following command in Mac Terminal to find out:

grep Port /etc/ssh/sshd_config

Enter the server’s password

If you enter your credentials correctly, as explained in the previous step, you’ll receive an output which requires a password. Simply type in your server’s SSH password to move on.

Next, if all settings are correct, you’ll have successfully connected your Mac to a remote Linux server via SSH. The confirmation command will be something like this:

[[email protected] ~] #

Now, your Terminal environment will change to a remote server’s shell environment, which allows you to run Linux commands from then on. 

Can’t access Linux server on Mac through SSH

You may face an error even after carefully performing all the previous steps. The problem is usually because the Linux server doesn’t have an SSH server installed.

Although most Linux distributions, such as Ubuntu, have an SSH server preinstalled, double-check on the server or ask your host. If the Linux server doesn’t have any SSH protocol installed, first, you’ll have to set one on your server before trying to connect to it.

If you don’t know where to start, try installing SSH servers, such as OpenSSH, in Linux or use web-based versions.

How to exit from an SSH session

Ending an SSH session is an easy task. All you have to do is type (Exit) in the Linux shell environment, and press Enter. That’s it!

You’ll get logged out of the SSH session, followed by showing the below output:

Logout Connection to [IP address] closed.

? Elevate your online ventures with our Linux VPS hosting - offering top-notch resources and a user-friendly interface perfect for beginners and experts. ???

How to run commands on remote server leaving local shell environment

Sometimes, starting an SSH session and going through all those steps is pointless if you want to send a single command. You must run those commands, enter your server’s credentials, send one command, and exit again.

In such cases, a simple shortcut prevents wasting all that time. You can stay in your local Terminal environment by sending SSH commands while changing your Linux server. This method is especially useful to run commands locally and remotely simultaneously.

Running a single command

Use the following command in your local Mac Terminal app to send single commands to Linux through SSH:

ssh [username]@[ip-address] [command]

Replace [username] with the username of the remote server, [ip-address] with the IP address or domain of your server, and at last, [command] with the command you would like to run.

For instance, if you would like to see the list of files available in the directory of your Linux server, run the following command:

ssh [username]@[ip-address] ls

Running multiple commands

Sending multiple commands syntax is almost like sending the single command. All you have to do is separate commands by a semi-colon mark and put them in double quotation. 

Here’s the syntax:

ssh [username]@[ip-address or domain-name] "[command-1]; [command-2]; [command-3]"

For example, if you want to send [ls] and [uptime] commands at the same time, use the following command:

ssh [username]@[ip-address or domain-name] "ls; uptime"

By running these commands, you can see your server’s list of files and simultaneously find the server's uptime. 

Wrapping up

Terminal app on Mac allows users to Open an SSH session and access a Linux server through a secure network. Although you can also use third-party apps to RDP from Mac to Linux, it’s unnecessary. In this article, we provided some commands to start an SSH session.

SSH sessions provide secure access to any remote server. To make it even more secure, you can disable root access over SSH and specify a few user accounts that can log in to your server. 

If you want to use commands that require root privileges while using another user account, you can use [sudo] before the command. It’ll allocate privileges to the user account so the commands can be accomplished without root access. 

People also read: