34
loading...
This website collects cookies to deliver better user experience
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
.
servers
Windows/Linux
devices to access these servers
servers
, so they will become less vulnerable to brute-force attacksservers
servers
server
is very straightforward. Look for a create droplet option there. Here we have some settings suggestions for the new server
to be created:142.93.169.195
, and yours will be a different one.http://ifconfig.co/?ip=142.93.169.195
(use your IP, not this one)root
: user142.93.169.195
: server IPOnce 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.
server
was created). Paste it here.root@ubuntu-one
, you are connected to the remote server
!server
. Run on the server
:apt update
apt upgrade
servers
. The SSH key is a 2-part key. Here's how they differ:server
that you will connect to.client
computer and access the servers
the key is intended for! ⚠️
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.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. 😱
$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: costa
(private, with no extension)costa.pub
(public)cat costa.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIN7ioJG5Axxcksw47AujdY/Lke8ZJoWRPSDsV6pc/reK costa
server
, then:cd /root/.ssh
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:nano authorized_keys
>>
command: echo <your_public_key_content> >> authorized_keys
If you use this second option, make sure you use >>
(append) and not >
(overwrite) ⚠️
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.public
is on the server
, we should be all set for accessing the server
with the SSH key! server
, exit from it using the exit
command.client
, access the server
with the following command. This time you will not be prompted for the password.ssh -i <private_key_file> <user>@<ip>
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.
client
, let us create a sample file in the current directory you are in:touch hello.txt
server
:scp -i <private_key> hello.txt [email protected]:
:
: the directory where hello.txt
will be saved on the server
. In this case, the main/root folder.server
and verify that hello.txt
is there.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
.
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
:rm hello.txt
hello.txt
:scp -i <private_key> [email protected]:hello.txt .
:
: 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
.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. ⚠️client
, send create-user.sh
to the main folder on the server
:scp create-user.sh [email protected]:
server
, you will see that create-user.sh
is there. Run it to create a new user:. create-user.sh
<new_user>@ubuntu-one
. Now check the contents of the authorized_keys
of the <new_user>
:cat /home/<new_user>/.ssh/authorized_keys
public
key that you added to the root
user. That means that now you can SSH connect to this server
as <new_user>
, instead of root
, by using the same private
key. root
with <new_user>
when connecting to the server
.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.
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.systemctl restart sshd
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.
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
! ⚠️root
access, go back to:sudo nano /etc/ssh/sshd_config
PermitRootLogin
to no
. Now restart the SSH Daemon service again: systemctl restart sshd
server
as the root
user. You are not supposed to be able to log in.servers
without needing to type their IP address and user. It is very helpful, especially if you plan to connect to multiple servers
.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 theHosts
ssh <custom_name>
Apache
as a HTTP server. Install it on the server
:sudo apt install apache2 -y
client
, go to your browser
and visit the IP address of the server
. You will see a page similar to this one:server
and replace it with our custom website content. server
, go to this directory:cd /var/www/html
index.html
file here. If you inspect its contents, you will notice that this is the file you are seeing on the page above.client
, download it here as a .zip
file, and send it to the server
(main folder):scp portfolio-master.zip <custom_name>:
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/
cd /var/www/html/
unzip
to unzip this file:sudo apt install unzip
unzip portfolio-master.zip
index.html
file, as we will use a different one:rm index.html
/portfolio-master
folder. We do not need the folder, only its contents. So we will move them into the current folder (/var/www/html/
): sudo mv portfolio-master/* .
*
: all the files in /portfolio-master
folder.
: the current directory/var/www/html/
, including a brand new index.html
file.client
side, visit the IP address again on the browser
and you will see my portfolio page!/portfolio-master
folder andportfolio-master.zip
file, as we do not need them anymore:sudo rm -r portfolio-master/
sudo rm portfolio-master.zip
scp
can be used to transfer files back and forth from client
to server
. However, this approach has some limitations:remote
and local
machines, prior to issuing the command.server
directories.config
file to manage your SSH connections, so in order to connect using SFTP from the local
:sftp <custom_connection_name>
sftp>
. ⚠️ get <filename>
put <filename>
reget <filename>
reput <filename>
echo 'put <local_file_path_to_upload>'| sftp -i <key> <user>@<ip>
echo 'get <remote_file_path_to_download>'| sftp -i <key> <user>@<ip>
server
.df
lls
!
#when you are done, exit
help
.journactlctl
works as an auditing tool for that. In our scenario, let's say I want to know about SSH connections for today on my server
.journalctl -u ssh -S today
authorized_keys
. Actually, it is the hash code for the public key. Now, to inspect the hash codes for the keys in authorized_keys
:ssh-keygen -l -f .ssh/authorized_keys
journalctl
entries. In our case: SHA256:KcZdwz9jSWutRXORHXH995E2ThQn9zci1yaeSaaPKxc You can inspect the hash code for a SSH key with ssh-keygen -l -f <public_or_private_key_file>
. You will notice that both public and private key share the same hash!