44

It sucks to open Terminal each time and write a complete SSH command. How can I create an SSH connection Terminal shortcut on Mac OS X 10.6.8 (Snow Leopard)?

I also have a custom port to connect to.

11 Answers 11

108

The most *NIX-y answer is to use SSH's features to your advantage.

Create a file named config in ~/.ssh/ (a folder named .ssh in your home folder). Add an entry for each computer you want to connect to, like this:

Host compy
    HostName 98.256.211.12
    Port 90
    User sidney
    IdentityFile ~/.ssh/my_rsa_key
  • HostName can be either an IP address or an actual hostname.
  • Port is not mandatory if using default SSH port
  • IdentityFile is not mandatory if not using a key.

    Then, to connect, just type

ssh compy

If you use key-based authentication and store your key's password in the Keychain, you won't even need to enter a password.

In addition, you can create a .command file (a plain text file with the extension .command) containing the command line you use to connect to the server (ssh compy or ssh -i ~/.ssh/my_rsa_key -p 90 [email protected]). It will open in Terminal and run that command.

You can also use the New Remote Connection… menu item in Terminal to connect. Just add your host under SSH by clicking the + button in the right column.

Read more HERE

2
  • 1
    I used this in combination with adding my ssh key to the server. You can push your public key like this 'ssh-copy-id -i .ssh/id_rsa.pub username:password@remotehost'
    – jspooner
    Jan 11, 2013 at 20:08
  • 🥴 I regret the hours I wasted while looking up the ip addresses of my servers and typing them in...
    – Hemant
    Feb 19, 2022 at 4:32
15

You have a couple of options that I can think of:

Create a .command file

In your favorite text editor create to save a plain text file with your ssh command, eg.

ssh -p 23 [email protected]

Name the file WHATEVERYOUWANT.command. You then need to set the .command file with execute privileges (chmod 755 /PATH/TO/WHATEVERYOUWANT.command) but you now have a file that when double-clicked will open up Terminal and run that command.

Create aliases for your shell

Assuming you're using bash (the default shell in Mac OS X) you can add aliases to your .bashrc in your home folder.

eg. alias sshserver='ssh -p 23 [email protected]'

You can find more about aliases at [Wikipedia](http://en.wikipedia.org/wiki/Alias_(command) or by reading the documentation.

9

There are many ways to do this. Here is what I do:

Open Terminal Preferences

Duplicate your profile on the Settings tab with the gear icon. This changes your default setting file, so change your old default back to what it was before the duplication.

In your new custom settings click on Shell type in your ssh command in the Run Command at Startup section.

Rename your new setting from Custom to whatever you want.

You can now start your session from the New Window or New Tab menu items, and of course a keyboard shortcut can be assigned in Keyboard & Mouse in System Preferences.

7

For those who use a private key with their ssh and want to use the ~/.ssh/config method you can add an 'IdentityFile' attribute to your host followed with the key path. I.e:

Host SomeServer
    User ubuntu
    HostName someserver.com
    IdentityFile ~/.ssh/private.key

I would've added this bit as a comment to one of the answers, but my reputation is still low and I'm not allowed to do so.

1
  • Upping your reputation
    – TheLebDev
    Oct 6, 2022 at 7:09
4

To quickly open an ssh connection from nearly anywhere on your Mac, do the following:

  • Open TextEdit and type in your connection string in the form of: ssh://[email protected]
  • Select the whole line and drag and drop it on the Desktop. The result will be a *.inetloc file.
  • Move the file to any folder you like. I made one named ~/connections that holds all of my *.inetloc files. You can rename this file to whatever you like, and even change its icon.
  • Now you can drag and drop the *.inetloc file to the Dock, the Finder's Sidebar or Tool bar, or to anywhere else. (It can even be selected with Spotlight)
  • If you use LaunchBar, open the Launchbar Configuration screen (Command-Y) and add another rule for your connections folder. Now your SSH connections can be opened with LaunchBar. I believe you can do similar with LaunchBar and Quicksilver, enabling quick launching of your SSH shortcuts.

Create quick-access ssh shortcuts

1
  • 1
    This is certainly a nice and quick way of opening a terminal on a remote host!
    – cmroanirgo
    Mar 16, 2014 at 6:09
3

You can create an AppleScript that will run a command for you:

tell application "Terminal"
    do script "ssh server.example.com"
end tell
3

If you want a file you can open to launch a session, you can make an .inetloc easily. In TextEdit type an SSH URL to your host, e.g.:

ssh://username@hostname/path

TextEdit will recognize that as a URL. Drag the URL to your desktop and it will create an .inetloc as so:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>URL</key>
    <string>ssh://username@hostname/path</string>
</dict>
</plist>

You can then place that file anywhere, like your Dock, or open it with a launcher such as Quicksilver.

I would have recommended the SSH Plugin for Quicksilver, but the latest I can find, v.84, crashes QS newer than β54 (3815).

3
  • Can you also do that with a port? E.g. hostname:1234?
    – Daniel Beck
    Jan 31, 2011 at 17:50
  • Yes. ssh://username@hostname:port also works.
    – Turadg
    Jan 31, 2011 at 23:56
  • This method no longer seems to work in macOS 12.5 (21G72) Sep 16, 2022 at 6:30
1

If you are using Quicksilver, there is a great add-on called RemoteHosts which will automatically scan a ~/.hosts file or even your ~/.ssh/known_hosts file to autopopulate.

Connecting to a new host is as quick as opening Quicksilver (CMD-Space) and then typing in enough characters of the host to be unique, and finally pressing enter.

1

You can create a file in your local bin directory

/usr/local/bin/dev

With the following contents:

#!/bin/bash
ssh -p 12345 [email protected] 

Make it executable

chmod a+x /usr/local/bin/dev

Which then you can call from the terminal using

dev

And you then create an automator task to open it

tell application "Finder" to set theSel to selection

tell application "Terminal"
    set theFol to POSIX path of ((item 1 of theSel) as text)
    if (count of windows) is not 0 then
        do script "/usr/local/bin/dev2" in window 1
    else
        do script "/usr/local/bin/dev2"
    end if
    activate
end tell

You can also create a shortcut from launcher or hotkey on your keyboard

Launcher: Drag icon from automator save into the dockbar

Keyboard shortcut:

create as service in automator
open system services -> keyboard -> shortcuts -> app shortcuts -> enter title of automator and key shortcut 
0

If the part that sucks for you is typing in a long and complex ssh command, but you don't mind opening Terminal, and for some reason you don't want to use any of the previous answers' approaches, then you can also use aliases or functions. Add something like this to shell dotfile (.profile or .bash_profile or .bashrc or .cshrc or .zshrc or whatever):

alias connect.dev='ssh -p 12345 [email protected]'

The only advantage I can see over other approaches is that (in zsh at least, and I think bash) you can use command completion to see all your servers to connect to. For instance, let's say you have 25 different servers you connect to, and you don't want to remember all the names/aliases you gave them in the .ssh/config file. If all of the aliases start with the same thing, like 'connect.', then you just type connect.[TAB] at the command prompt, and you'll see all the possible completions. (Disclaimer: you might need to do some shell customization to get that working, but I believe it will work by default in zsh and probably in bash as well.)

If the part that sucks for you is opening Terminal, then you could try using something like DuckBill, although the only reasons I can think of to run ssh OUTSIDE of Terminal might possibly be for running X Windows server programs or an ssh tunnel.

0

You can try creating new remote connection for ssh.

You can go to -> Terminal menu bar >> Shell >> Service(Secure Shell) >> Server

Create new server connection.

You can setup passwordless ssh on target server.

This way you can save as many remote connection you want.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .