Sftp linux with private key

Table of contents

Among the benefits to using SSH to manage remote machines, we can list:

Many people, specially for Windows , tend to recommend a GUI client called PuTTY to set SSH connections. As long as you are on Windows 10 , OpenSSH is already native on you computer, so do not bother installing PuTTY .

By the end of this article you will have learned how to:

  • Create remote servers
  • Create SSH connection from Windows/Linux devices to access these servers
  • Disable password authentication on the servers , so they will become less vulnerable to brute-force attacks
  • Transfer files back and forth between local devices and these servers
  • Host your custom website on remote servers

Creating remote servers

We will use Digital Ocean for this part. Creating a server is very straightforward. Look for a create droplet option there. Here we have some settings suggestions for the new server to be created:

  • Choose a image/OS: Ubuntu
  • Choose plan: The cheapest one
  • Choose a datacenter: Whichever is closer to you
  • Authentication method: Password. We will set SSH manually and then disable password authentication (save this password to a text file for now, though)
  • Number of droplets (servers): 1
  • Choose a hostname: ubuntu-one

Now hit the «create droplet» button

Some reference screenshots:

image

image

Once the server is created, you will have access to its IP address. Mine was 142.93.169.195 , and yours will be a different one.

Check that the machine is «real» by verifying its IP address on this website:

http://ifconfig.co/?ip=142.93.169.195 (use your IP, not this one)

Accessing the remote server via cmd terminal (using password)

Where:
root : user
142.93.169.195 : server IP

You will be prompted for a message regarding ECDSA key fingerprint. Type «yes».

Once you say yes to this, notice that in $HOME/.ssh folder there will be a known_hosts file. This file «remembers» the servers that the current client device has previously connected to. Every time you connect to a new server , its fingerprint will be saved here (and you will be notified of that). This is a mechanism to verify that the server you are connecting to is actually the one you think it is.

You will be prompted for the password (the one you set when the server was created). Paste it here.

And there it is. If your terminal changed to root@ubuntu-one , you are connected to the remote server !

Читайте также:  Linux headers red hat

Updating packages on the new server

This is optional, but recommended, as you problably will install additional softwares on this server . Run on the server :

About the SSH key

SSH is the authentication method that we will use as an alternative to using passwords when accessing remote servers . The SSH key is a 2-part key. Here’s how they differ:

  • Public: It is intented to be shared, and placed on the server that you will connect to.
  • Private: It is not supposed to be shared or even seen by anyone else. If this part gets to the wrong hands, someone might be able to use it on their client computer and access the servers the key is intended for! ⚠️

You can have multiple SSH keys stored on your device. Once you have one, send the public part to the server . There, it will be stored in a dedicated place. To establish a connection, you will select a private key and the server will check for a match with their pre-existing public keys.

image

Creating SSH keys

To make it easier, download and run one of the following scripts. They use the ed25519 algorithm to create the keys, since it provides a more sophisticated encryption and generates a smaller key as well.

When creating a SSH key, you will be prompted for an optional passphrase that will be requested when you try to use it. It is highly recommended that you set a passphrase , because if your private key gets leaked and it has no passphrase , someone else can actually it on their client computer and very likely will be able to access the servers that the key has access to. 😱

Windows (PowerShell)

Linux (bash)

Tipically, the SSH keys are stored under the $HOME/.ssh directory (the . means that .ssh is a hidden folder), but you can have them elsewhere. I created a SSH key named costa , so here I can expect to see the 2-part key in 2 files:

Accessing the public key

Or using a text editor, and you wil get something like this: ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIN7ioJG5Axxcksw47AujdY/Lke8ZJoWRPSDsV6pc/reK costa

Sending the public key to a remote server

Log in to the server , then:

There you will find the authorized_keys file, which is where the public keys must be inserted into. Paste the public key here manually by opening the file using a text editor:

Or directly using the >> command:

If you use this second option, make sure you use >> (append) and not > (overwrite) ⚠️

Think of the authorized_keys file as a vault that holds all the public keys from all devices authorized to SSH connect into this server using a SSH key.

Accessing the remote server via cmd terminal (using SSH key)

Now that the public is on the server , we should be all set for accessing the server with the SSH key!
First, if you are on the server , exit from it using the exit command.
Back on the client , access the server with the following command. This time you will not be prompted for the password.

Notice that you don’t actually need to provide the private key path using -i , as the keys stored under /.ssh are picked automatically. But as you begin to add more keys, chances are that at some point you will start to get errors for unmatching keys. To avoid this kind of problem, I suggest that you always specify the key.

Sending files from the client to the server

Back on the client , let us create a sample file in the current directory you are in:

Читайте также:  What is make modules in linux

Now let us send it to the server :

scp -i hello.txt root@142.93.169.195: 

Where:
: : the directory where hello.txt will be saved on the server . In this case, the main/root folder.

Go back to the server and verify that hello.txt is there.

Sending files from the server to the client

This procedure is a little counter-intuitive, because you need to be on the client side. It is as if you are fetching a file previously stored on the server .

Being on client , I would like to have hello.txt (the one already saved on the server ) sent to the client . First, delete hello.txt we just created on the client :

scp -i root@142.93.169.195:hello.txt . 

Where:
: : indicates the file path on the server
. (at the end): the path I want the file to be saved on the client . Notice the space before the . . Now, make sure you can see hello.txt on the client .

Creating a user on the server

The following script will create a new user, grant it sudo permissions and grab a copy of the root user’s authorized_keys file. Keep in mind that each user has its own authorized_keys file. ⚠️

From the client , send create-user.sh to the main folder on the server :

scp create-user.sh root@142.93.169.195: 

Back on the server , you will see that create-user.sh is there. Run it to create a new user:

Notice that your terminal changed to @ubuntu-one . Now check the contents of the authorized_keys of the :

cat /home//.ssh/authorized_keys 

You will see here the same public key that you added to the root user. That means that now you can SSH connect to this server as , instead of root , by using the same private key.

Now you can replace root with when connecting to the server .

Disabling password access on the server

This is not required, but notice that if you try to connect to the server without a SSH key, you will still be prompted for the password. We will disable that so the server will be protected against brute-force attacks using passwords. On the server , run:

sudo nano /etc/ssh/sshd_config 

Notice that in order to change this file, you need sudo permissions, hence the sudo command.

This file contains settings regarding SSH. In its contents, look for PasswordAuthentication . It is problably set to yes , so change it to no . Also, make sure it is not commented as well ( # ). Save the changes and close this file.
Now you need to restart the SSH service for the changes to take effect: ⚠️

Читайте также:  Инициализация базы данных postgresql linux

From this moment on, you will no longer be prompted for the password when trying to SSH connect to the server !😎

The settings in the /etc/ssh/sshd_config file apply to all the users, as the /etc folder in Linux has a global scope.

Disabling root access on the server

This is not required either, but keep in mind that there are many security concerns about remote connecting to a server as the root user. This user is like a God mode on Linux machines, so a lot of damage can be done by this user on the server ! ⚠️

To disable root access, go back to:

sudo nano /etc/ssh/sshd_config 

Set PermitRootLogin to no . Now restart the SSH Daemon service again:

Now go ahead and try to access the server as the root user. You are not supposed to be able to log in.

Create a SSH config file (very handy)

This file makes it a lot easier to SSH connect to remote servers without needing to type their IP address and user. It is very helpful, especially if you plan to connect to multiple servers .

On the client , use the following template to create a file named config , and place it in your /.ssh folder:

In case you are wondering about the Port , 22 is the default port for SSH connections. It can be changed on the /etc/ssh/sshd_config file on the server . Also, the settings under Host * are broadcasted to all the Hosts

Once this file is set, the SSH connections can be made as such:

In short, now you can replace the user + IP address + private key + port with a simple custom alias. Very convenient, right? 😎

A lightweight client to manage SSH servers

SHECRET is one of my projects on GitHub. It is a lightweight command line SSH/SFTP client . You can also use it to create SSH keys using the ed25519 algorithm!

Image description

Hosting a website on the server

We will use Apache as a HTTP server. Install it on the server :

sudo apt install apache2 -y 

On the client , go to your browser and visit the IP address of the server . You will see a page similar to this one:

image

The next step is finding this page on the server and replace it with our custom website content.
Back on the server , go to this directory:

You will find a index.html file here. If you inspect its contents, you will notice that this is the file you are seeing on the page above.

We will use my personal portfolio as an example for our custom website.
Back on the client , download it here as a .zip file, and send it to the server (main folder):

Back on the server and , having the portfolio-master.zip file there, move this file to the /var/www/html/ directory:

sudo mv portfolio-master.zip /var/www/html/ 

Источник

Оцените статью
Adblock
detector