Mac’s Terminal application provides a straightforward way to establish an SSH connection to a Linux server. If you’re wondering how to connect to linux server from mac, you’ve come to the right place. This guide walks you through every step, from finding your server’s IP address to troubleshooting common issues.
You don’t need to be a command-line expert. With a few simple commands, you’ll be managing your Linux server remotely in no time. Let’s get started.
Prerequisites For Connecting
Before you can connect, you need a few things ready. First, make sure your Mac is connected to the internet. Second, your Linux server must be running and accessible over the network.
You’ll also need the server’s IP address or hostname. If you don’t have it, check with your hosting provider or system administrator. You’ll also need a username and password for the server, or an SSH key pair.
Check Your Network Connection
Open System Settings on your Mac and go to Network. Ensure Wi-Fi or Ethernet is active. A stable connection prevents timeouts during the SSH handshake.
Gather Server Information
- Server IP address (e.g., 192.168.1.100 or a domain like example.com)
- SSH port (default is 22, but it might be different)
- Username (e.g., root or a non-root user)
- Authentication method: password or SSH key
How To Connect To Linux Server From Mac
Now let’s dive into the actual process. The most common method is using SSH (Secure Shell) through the Terminal app. Here’s the step-by-step guide.
Step 1: Open Terminal On Your Mac
Go to Applications > Utilities > Terminal. You can also use Spotlight (Cmd+Space) and type “Terminal”. A black or white window will appear with a command prompt.
Step 2: Use The SSH Command
Type the following command, replacing the placeholders with your actual information:
ssh username@server_ip_address
For example, if your username is “admin” and the IP is “192.168.1.100”, type:
ssh admin@192.168.1.100
If the server uses a different port (say 2222), add the -p flag:
ssh -p 2222 admin@192.168.1.100
Step 3: Accept The Host Key
The first time you connect, Terminal will show a warning about the host’s authenticity. Type “yes” and press Enter. This adds the server to your known hosts file, so you won’t see it again unless the server changes.
Step 4: Enter Your Password
You’ll be prompted for the password. Type it carefully—nothing will appear on screen as you type. Press Enter after typing. If the password is correct, you’ll see a welcome message and a command prompt from the Linux server.
You’re now connected! You can run Linux commands like ls, cd, or sudo as needed.
Using SSH Keys For Passwordless Login
Passwords are fine, but SSH keys are more secure and convenient. They eliminate the need to type a password every time. Here’s how to set them up.
Generate An SSH Key Pair On Your Mac
Open Terminal and run:
ssh-keygen -t rsa -b 4096
Press Enter to accept the default file location (~/.ssh/id_rsa). You can add a passphrase for extra security, or leave it empty for passwordless login. The command creates two files: a private key (id_rsa) and a public key (id_rsa.pub).
Copy The Public Key To Your Linux Server
Use the ssh-copy-id command to transfer the public key:
ssh-copy-id username@server_ip_address
You’ll be prompted for your server password one last time. After that, the key is installed. Now you can connect without a password:
ssh username@server_ip_address
Manual Key Installation (If Ssh-copy-id Fails)
If ssh-copy-id isn’t available, do it manually. First, display your public key:
cat ~/.ssh/id_rsa.pub
Copy the output. Then SSH into your server (with password) and run:
echo "paste_your_public_key_here" >> ~/.ssh/authorized_keys
Make sure the .ssh directory and authorized_keys file have correct permissions:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Connecting With A Different Username Or Port
Sometimes your server uses a non-default username or port. Here’s how to handle those cases.
Specify A Custom Port
Use the -p flag as shown earlier. For example:
ssh -p 2222 john@192.168.1.100
Connect Using A Hostname Instead Of IP
If your server has a domain name (like server.example.com), use it directly:
ssh john@server.example.com
Use A Different Private Key File
If you have multiple SSH keys, specify which one to use with the -i flag:
ssh -i ~/.ssh/my_custom_key john@192.168.1.100
Using The Terminal Profile For Quick Access
You can save server connection details in your Mac’s Terminal profile. This is handy if you connect to the same server often.
Create A New Profile
Open Terminal > Settings > Profiles. Click the “+” button to create a new profile. Name it something like “My Linux Server”.
Configure The SSH Command
In the “Shell” tab, check “Run command” and enter your SSH command, e.g., ssh admin@192.168.1.100. Check “Run inside shell” to keep the window open after disconnecting.
Now, whenever you select this profile from Terminal’s menu, it automatically connects to your server.
Alternative Methods: Third-Party SSH Clients
While Terminal is great, some people prefer graphical tools. Here are a few popular options for Mac.
Termius
Termius is a cross-platform SSH client with a clean interface. It supports saved connections, SFTP, and even mobile sync. The free version is sufficient for basic use.
Royal TSX
Royal TSX is a powerful connection manager. It supports SSH, RDP, VNC, and more. You can organize servers into folders and use shared credentials.
Cyberduck
Cyberduck is mainly an FTP/SFTP client, but it also supports SSH terminal sessions. It’s great for file transfers alongside command-line access.
Transferring Files Between Mac And Linux Server
Once connected, you might need to copy files. Here are two common methods.
Using SCP (Secure Copy)
From your Mac’s Terminal (not the SSH session), use:
scp /path/to/local/file username@server_ip:/path/to/remote/directory
To copy a file from the server to your Mac:
scp username@server_ip:/path/to/remote/file /path/to/local/directory
Using SFTP
SFTP is like FTP over SSH. Start an SFTP session:
sftp username@server_ip
Then use commands like get (download) and put (upload). For example:
get remote_file.txt
put local_file.txt
Troubleshooting Common Connection Issues
Even with the right steps, things can go wrong. Here are solutions to frequent problems.
Connection Timed Out
This usually means the server is unreachable. Check if the server is running and if the IP address is correct. Also, verify that port 22 (or your custom port) is open in the server’s firewall.
Permission Denied (Publickey)
This error means SSH key authentication failed. Ensure your public key is correctly added to the server’s authorized_keys file. Also, check that the private key file on your Mac has correct permissions (600).
Host Key Verification Failed
This happens when the server’s host key changes. It could be a security issue or a legitimate server update. To fix, remove the old key from your known_hosts file:
ssh-keygen -R server_ip_address
Then reconnect and accept the new key.
SSH Command Not Found
If Terminal says “command not found”, SSH might not be installed. On modern Macs, SSH is pre-installed. If missing, install Xcode Command Line Tools:
xcode-select --install
Security Best Practices
Connecting to a Linux server over the internet requires caution. Follow these tips to stay safe.
Disable Password Authentication
Once SSH keys work, disable password login on the server. Edit the SSH config file:
sudo nano /etc/ssh/sshd_config
Set PasswordAuthentication no. Then restart SSH:
sudo systemctl restart sshd
Use A Non-Root User
Avoid logging in as root directly. Create a regular user with sudo privileges. This limits damage if your credentials are compromised.
Change The Default SSH Port
Changing port 22 to something else reduces automated attacks. Edit the same config file and set Port 2222 (or any number above 1024). Remember to update your connection commands.
Enable Two-Factor Authentication
For extra security, set up 2FA for SSH. Tools like Google Authenticator can be integrated with PAM on the server.
Automating Connections With SSH Config File
You can create a config file to simplify connections. This is great if you manage multiple servers.
Create Or Edit ~/.Ssh/config
Open Terminal and run:
nano ~/.ssh/config
Add an entry like this:
Host myserver
HostName 192.168.1.100
User admin
Port 22
IdentityFile ~/.ssh/id_rsa
Now you can connect by just typing:
ssh myserver
Multiple Servers
Add as many entries as you need. Each “Host” can be a nickname you choose. This saves time and reduces typos.
Using The Mac Keychain For SSH Passwords
If you must use passwords, you can store them in your Mac’s keychain. This avoids retyping them.
Add Password To Keychain
Use the ssh-add command with the -K flag (on older macOS) or just ssh-add with a passphrase-protected key. For password-based SSH, you’ll need a third-party tool like sshpass, but that’s less secure.
A better approach is to use SSH keys with a passphrase and add them to the keychain:
ssh-add -K ~/.ssh/id_rsa
On newer macOS versions, use:
ssh-add --apple-use-keychain ~/.ssh/id_rsa
Connecting To A Server Behind A Firewall
If your Linux server is behind a NAT or firewall, you might need extra steps.
Port Forwarding
Configure your router to forward port 22 (or your custom port) to the server’s local IP. This allows external connections.
Use A VPN
For added security, connect to a VPN before SSH-ing. This encrypts all traffic and bypasses some firewall restrictions.
SSH Tunneling
You can create a tunnel through a jump server. For example:
ssh -J user@jump_server user@target_server
Monitoring Your SSH Sessions
Once connected, you might want to see who else is logged in. Use the who or w command on the server. To list active SSH sessions, run:
ss -tn | grep :22
Disconnecting From The Server
To end your session, type exit or press Ctrl+D. The Terminal will return to your Mac’s local prompt.
Frequently Asked Questions
Can I Connect To A Linux Server From Mac Without Terminal?
Yes, you can use third-party apps like Termius or Royal TSX. They provide a graphical interface for SSH connections.
What If I Forget My Server’s IP Address?
Check your hosting provider’s dashboard or use a service like “What is my IP” from the server itself. If you’re on the same network, use arp -a on your Mac to find local devices.
Is SSH The Only Way To Connect To A Linux Server From Mac?
No, you can also use VNC for graphical remote desktop, or FTP/SFTP for file transfers. But SSH is the most common and secure method for command-line access.
How Do I Fix “Connection Refused” Error?
This usually means the SSH service isn’t running on the server, or a firewall is blocking the port. Check if sshd is active and if the port is open.
Can I Use The Same SSH Key For Multiple Servers?
Yes, you can copy your public key to multiple servers. Just use ssh-copy-id for each one, or manually add the key to each server’s authorized_keys file.
Now you have all the knowledge needed to connect your Mac to a Linux server. Practice these steps, and soon it’ll become second nature. Whether you’re managing a web server, a development environment, or a home lab, SSH is your reliable gateway.