If you run one or more remote servers, you’re typically spending time constantly logging in throughout the day.

But it doesn’t have to be this way. It’s time you learned how to efficiently login via SSH with only an alias, transfer single files or directories, execute remote SSH commands, and effortlessly mount remote servers to local directories.

SSH Config File

One huge time saver is the SSH config file located at ~/.ssh/config. Start by creating a directory to store all the SSH keys you use to login to servers with the command:

Now copy all of your SSH key files into this directory (eg. clienta.pem, clientb.pem, etc.). Next, open up the ~/.ssh/config file in a text editor by running the command:

Below is an example entry that will establish an SSH connection with a remote server:

Add sections of lines such as above to the ~/.ssh/config file, one for each server you desire. Then save and close the file by pressing Ctrl+X and follow the prompt. Once saved, you can now login to any server via SSH from any directory within terminal with the simple command:

This will instantly log you into the server with the information under the clienta host you specified within the ~/.ssh/config file.

Transfer Files With Scp / Rcp

Without creating a persistent login session, you can easily upload single files or directories to a remote server with the scp command, such as:

The above command will upload the report.pdf file from your local computer to the home directory of the clienta server you defined in the above section. You may upload to a directory other than the home directory such as:

The above will upload the about.html file to the /home/client/public_html directory on the remote clienta server. It is also possible to upload entire directories using the -r option such as:

This will upload the entire ~/Documents directory from your local computer to the ~/docs directory of the remote server.

Downloading Files

Similarly, you can download files or directories to your local computer without creating a persistent login session using the rcp command such as:

The above will download the public_html/about.html file from the remote clienta server, and place it into the projects/about.html file on your local computer.

Execute Remote SSH Commands

Another quick tip is you can execute single Linux commands on a remote server without a persistent login session, such as:

The above will execute the ls command on the remote clienta> server, and list all files / directories without keeping you logged into the server. For example, if you wanted to restart a server you could use:

Local /bin Directory

Let’s expand on this by allowing easy mounting to remote servers by creating a /bin/ directory that’s local to our user account. Open terminal on your computer, and create a /bin/ directory by running the command:

Next, open the ~/.profile file in a text editor with the command:

Scroll down to the very bottom of the file, and add the following lines by copying them to your clipboard, then within terminal by pressing Ctrl+Shift+V:

Save and close the file by pressing Ctrl+X, and follow the prompt. This will save the .profile file, which will check the newly created local /bin/ directory for any commands you try to run.

Adding Remote Mount Commands

First, check and see whether or not sshfs is installed on your computer with the command:

If this prints out the current version of sshfs, then you’re all set. Otherwise if you receive a “command not found” error, you may install sshfs with the following command:

Now create a /mnt/ directory that will contain all the mounted directories to our remote servers. Within terminal run the commands such as:

Continue creating one sub-directory for each remote server you may potentially mount to. Next, let’s create the shell commands that we will run, and for example, for the clienta server open a file by running the following command in terminal:

Modify the below line as necessary with the proper server information, then copy and paste it into the blank text editor within terminal by pressing Ctrl+Shift+V:

Save and close the file by pressing Ctrl+X, and follow the prompts to close the file. Finally, change permissions of the file so it’s executable by running the command:

Now any time you need to mount to clienta’s remote server to transfer files to / from it, from any directory within terminal you can simply run the command:

The directory on your local computer at ~/mnt/clienta will now be mounted to the /var/www directory of the remote server. You can begin copying files to and from the directory just as you would any local directory, and the necessary operations will occur on the remote server.

More Efficient connection Management

Hopefully the above tips have helped streamline and made more efficient the management of your connections to remote servers. In this article you have learned all about the ~/.ssh/config file allowing you to login via SSH with only an alias, transfer single files / directories, execute remote SSH commands, and how to easily mount a local directory to remote servers.