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:
mkdir -m 0600 $ HOME/.ssh_keys
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:
nano ~/.ssh/config
Below is an example entry that will establish an SSH connection with a remote server:
host clienta
hostname 124.58.2276.80
user ubuntu
IdentityFile ~/.ssh_keys/clienta.pem
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:
ssh clienta
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:
scp report.pdf clienta:~/
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:
scp about.html clienta:/home/clienta/public_html
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:
scp -r Documents clienta:~/docs
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:
rcp clienta:~/public_html/about.html myproject/about.html
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:
ssh clienta ls
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:
ssh clienta /sbin/shutdown -rf now
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:
mkdir -m 0755 ~/bin
Next, open the ~/.profile file in a text editor with the command:
nano $ HOME/.profile
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:
if [ -d "$ HOME/bin" ] ; then
PATH="$ HOME/bin:$ PATH"
fi
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:
sshfs --version
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:
sudo apt-get -y install sshfs
Now create a /mnt/ directory that will contain all the mounted directories to our remote servers. Within terminal run the commands such as:
mkdir -m 0755 ~/mnt
mkdir -m 0755 ~/mnt/clienta
mkdir -m 0755 ~/mnt/clientb
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:
nano ~/bin/mount_clienta
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:
#!/bin/bash
sshfs -o IdentityFile=~/.ssh_keys/clienta.pem ubuntu@192.168.0.24:/var/www ~/mnt/clienta
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:
chmod 0755 ~/bin/mount_clienta
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:
mount_clienta
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.