Effectively Handle Distant SSH Connections With These Linux Instructions

Typically, when you run one or more remote servers, you spend time logging on constantly all day.

That does not have to be that way. It's time you learned how to efficiently log in via SSH with just one alias, transfer individual files or directories, run remote SSH commands, and easily provision remote servers to local directories.

SSH configuration file

A big time saver is the SSH configuration file under ~ / .ssh / config. First, create a directory to store all of the SSH keys that you will use to log into servers with the following command:

mkdir -m 0600 $ HOME / .ssh_keys

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

nano ~ / .ssh / config

Below is a sample entry used to SSH connection to a remote server:

Linux SSH Secure

Host client a

Host name 124.58.2276.80

Ubuntu user

IdentityFile ~ / .ssh_keys / clienta.pem

Add sections to the section as above ~ / .ssh / config File, one for each desired server. Save and close the file by pressing Ctrl + X. and follow the prompt. After saving, you can now log into any server via SSH from any directory in the terminal with the simple command:

ssh clienta

This will immediately log you in with the information under the server clienta Host you specified in the ~ / .ssh / config File.

Transfer files with Scp / Rcp

Without creating a permanent login session, you can use the file to easily upload individual files or directories to a remote server scp Command like:

scp report.pdf clienta: ~ /

The above command will upload the report.pdf file from your local computer to the home directory of clienta Server that you defined in the section above. You can upload to a directory other than the home directory, e.g.

scp about.html clienta: / home / clienta / public_html

Above will upload the about.html file to the file / home / client / public_html Directory on the remote control clienta Server. It is also possible to upload entire directories with the -r Option like:

scp -r documents clienta: ~ / docs

This will upload the whole ~ / Documents Directory from your local computer to the ~ / docs Remote server directory.

download files

You can also download files or directories to your local computer without creating a permanent login session with the rcp Command like:

rcp clienta: ~ / public_html / about.html myproject / about.html

The above will download that public_html / about.html File from the remote control clienta Server and place it in the projects / about.html File on your local computer.

Run remote SSH commands

Another quick tip is that you can run individual Linux commands on a remote server without a permanent login session, e.g.

ssh clienta ls

The above will do that ls Command on the remote client> server and list all files / directories without staying logged on to the server. For example, if you want to restart a server, you can use:

ssh clienta / sbin / shutdown -rf now

Local / bin directory

Let's extend this by making it easy to deploy to remote servers by creating a / bin / directory that is local to our user account. Open the Terminal on your computer and create a / bin / directory by running the following command:

mkdir -m 0755 ~ / bin

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

nano $ HOME / .profile

Scroll to the bottom of the file and add the following lines by copying them to your clipboard and then pressing from within the terminal 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 that will check the newly created local directory / bin / for commands you want to run.

Adding remote mount commands

First of all, check whether or not sshfs is installed on your computer with the following command:

sshfs –version

If this prints the current version of sshfsThen you are done. Otherwise, if you get the command not found error, you can install sshfs with the following command:

sudo apt-get -y install sshfs

Now create a directory / mnt / that contains all the mounted directories for our remote servers. In the terminal, run the following commands:

mkdir -m 0755 ~ / mnt

mkdir -m 0755 ~ / mnt / clienta

mkdir -m 0755 ~ / mnt / clientb

Also, create a subdirectory for each remote server that you may have access to. Next we 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 following line as needed with the correct server information, copy and paste it by pressing in the blank text editor within the terminal 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.Follow the instructions to close the file. Finally, change the file's permissions so that it is executable by running the following command:

chmod 0755 ~ / bin / mount_clienta

Now you can assemble at any time Customers Remote server to transfer files from / to it, from any directory within the terminal, you can just run the following command:

mount_clienta

The directory on your local computer under ~ / mnt / clienta is now mounted on the / var / www Remote server directory. You can start copying files to and from the directory as you would any local directory, and the required operations will be performed on the remote server.

More efficient connection management

Hopefully, the tips above have helped streamline the management of your connections to remote servers and make them more efficient. In this article, you have all about that ~ / .ssh / config File with which you can log in via SSH with just an alias, transfer individual files / directories, execute remote SSH commands and easily provide a local directory on remote servers.

Popular apps Free discount

No Need To Pirate: 9 Popular Apps To Use For Free Or Cheap

Here's how to get access to frequently pirated apps for free, at a discount, or through alternative software.

About the author

Matt Dizak
(5 articles published)

More
By Matt Dizak

Subscribe to our newsletter

Subscribe to our newsletter for tech tips, reviews, free e-books, and exclusive deals!

One more step …!

Please confirm your email address in the email we just sent you.

Leave a Reply

Your email address will not be published. Required fields are marked *