Save remote server connections in the OS X Terminal

The OS X Terminal is commonly used for establishing remote connections, and has some options for facilitating this.

If you are familiar with the Unix command line, then the OS X Terminal is a great resource for accessing systems remotely. This can be done with several different protocols, including SSH and Telnet, FTP, and SFTP, as well as several others. Often people who use the Terminal for remote access will have several servers they regularly access, which makes typing the commands and URLs to establish the connection rather redundant.

To make this easier, you have several approaches you can take. The first is to set up a script or shell function that will store the connection information for you. In doing so you can create a menu system similar to the one mentioned in this post to establish a connection to one of a number of servers using different protocols. If you don't need a menu system, you can simply set up a script to contain your ssh command and its arguments with the following method:

  1. Open the Terminal and type the following command.

    pico ~/.bashrc

  2. In the editor that opens, enter a function similar to the following:

    function myssh() {
         ssh -p 22 username@hostname
    }


    In this command, change the name of the "myssh" function to that of your choice (the server hostname is a good option to use), and then edit the ssh command itself to be appropriate for your server's requirements.
  3. Save the changes and quit the editor by pressing Control-O followed by Control-X.

When this is done, create a new Terminal session and then type the name of your function (tab-completion will work to facilitate this), followed by pressing Enter and your connection should be established without having to enter the entire command string every time.

New Remote Connection options in the OS X Terminal
The New Remote Connection option in the OS X Terminal can be used to store commonly accessed servers, and also be used to discover available servers on the local network. Screenshot by Topher Kessler/CNET

While this is a convenient option to have, it does take some setting up before it can be used. An alternative to doing this is to use Apple's New Remote Connection feature in the Terminal to establish a list of desired protocols and servers. To do this, press Shift-Command-K in the Terminal or choose New Remote Connection from the Shell menu. In the window that appears you can choose from the four most common connection protocols (SSH, SFTP, FTP, and Telnet), or you can create your own by clicking the plus button at the bottom of this list.

With the desired protocol selected, click the plus button in the Server list and type the appropriate host name or IP address of the desired server. At this point when handling the username, you have two options. The remote connection window simply builds a command string that is run in the Terminal, so you can enter the username and other arguments before the URL when adding it to the server list, or you can simply enter the URL and then provide your username in the User field at the bottom of the window.

Once the server is set up in the connection window, selecting it will show the final command string at the bottom of the window. You can make further edits to this string if needed, and then click Connect to establish the connection. This connection string is also a drop-down menu where the last-used command strings are listed, so you can quickly access them if needed.

In addition to creating your own connection commands, the New Remote Connection window offers access to Apple's Bonjour auto-discovery services. In the server list you will see a Discovered Servers section where you will find the computer names of systems both on the local network and using Apple's Back to My Mac iCloud service that are actively hosting the selected protocol. Choosing one of these servers and then supplying a username is a quick option for logging into these servers, instead of needing to remember their specific hostnames or IP addresses. As a result, connecting to a server on the local network simply takes the following steps:

  1. Open the Terminal (Spotlight is a great launcher for this) and type Shift-Command-K
  2. Select your protocol and the discovered server
  3. Provide a username and edit the connection string if needed
  4. Connect to the server


Questions? Comments? Have a fix? Post them below or e-mail us!
Be sure to check us out on Twitter and the CNET Mac forums.

Tags:
Computers
About the author

    Topher, an avid Mac user for the past 15 years, has been a contributing author to MacFixIt since the spring of 2008. One of his passions is troubleshooting Mac problems and making the best use of Macs and Apple hardware at home and in the workplace.

     

    Join the discussion

    Conversation powered by Livefyre

    Don't Miss
    Hot Products
    Trending on CNET

    HOT ON CNET

    Find Your Tech Type

    Take our tech personality quiz and enter for a chance to win* high-tech specs!