Difference between revisions of "Access server"

From Anarchaserver
Line 1: Line 1:
<div style="font-family:monospace; font-size:14px; background-color:#E9F8A8; padding:10px;">


====== SSH ======
<div style="float:right; border:none; background:none; padding-right:3%">
__TOC__
</div>
<br clear="all">
<br>
<br>
===›››››››››››››› SSH===


:: '''SSH keys''' provide '''a more secure way''' of logging into a virtual private server with SSH than using a password alone. While a password can eventually be cracked with a brute force attack, SSH keys are nearly impossible to decipher by brute force alone. Generating a key pair provides you with two long string of characters: a public and a private key. You can place the public key on any server, and then unlock it by connecting to it with a client that already has the private key. When the two match up, the system unlocks without the need for a password. You can increase security even more by protecting the private key with a passphrase.
<br>
<br>
===›››››››››››››› Making changes in ssh===


:: Remember to:
:: <pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%"> sudo service ssh restart</pre>


SSH keys provide **a more secure way** of logging into a virtual private server with SSH than using a password alone. Generating a key pair provides you with two long string of characters: a public and a private key. You can place the public key on the server, and then unlock it by connecting to it with a client that already has the private key. When the two match up, the system unlocks without the need for a password. You can increase security even more by protecting the private key with a passphrase.
::'''Tip''': I can check all available services in /etc/init.d
::'''Attention''': if there are problems, grep to see if it actually stops the service Like this:
:: <pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%"> sudo service ssh stop</pre>
:: <pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%"> ps aux | grep ssh</pre>
:: <pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%"> sudo service ssh start</pre>
:: <pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%"> ps aux | grep ssh</pre>
<br>
<br>


'''We definitely also need to disable Password authentication,
===›››››››››››››› Debug===
so only the public key authentication is used:'''


$ nano /etc/ssh/sshd_config
::Debug with a <code style="border-color:#DDE3DB; color:red;">-v</code> flag. max 3 "v"s


<code>
:: <pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%"> ssh <user>@<server> -vvv </pre>
# Change to no to disable tunnelled clear text passwords
<br>
# PasswordAuthentication yes
<br>
PasswordAuthentication no
===›››››››››››››› Create RSA Key Pair===
</code>


'''Reload ssh server configuration:'''
::You create your '''RSA key pair''' in the computer from which you want to log to the server.


$ /etc/init.d/
::I can use this key pair for several servers, but if I want to enter a same server from different computers, I create different rsa key pairs.  
$ ./ssh reload


or an newer systems :
::Lo voy a crear a nivel local [en mi compu] y esa va a ser la llave rsa ssh que voy a usar para entrar en mis vps. NO genero una para cada vps sino que esa misma me sirve para todas. Si voy a usar varios ordenadores para entrar en un vps, sí necesitaré crear una llave ssh por máquina desde la cual estoy accediendo.
$ sudo service ssh restart


::So each admin in a server is going to have at least one rsa key pair.


[ ok ] Reloading OpenBSD Secure Shell server's configuration: sshd
::Be careful to pay attention from which user you create this in your local machine [computer]. Since the rsa key pairs are saved in a hidden directory in my user home directory [<code style="border-color:#DDE3DB; color:red;">~/.ssh</code>], if Im trying to login from a different user, it will confuse home directories since each user has a different home directory.


===== Create RSA Key Pair =====
::So, from the user in my localmachine that I wish to use to access the server, I will create my rsa key pair:
You create your rsa key pair in the computer you want to log to the server


Be careful to pay attention from which user you create the rsa key pair in your local machine (computer). Since the rsa key pairs are saved in a hidden directory in my user home directory (~/.ssh), if trying to login from a different user, it will not work
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">ssh-keygen -t rsa -b 4096</pre>


So, from the user in my localmachine that I wish to use to access the server, I will create my rsa key pair. When generating the key, you will need to introduce a **"passphrase".**
::By default it will generate two keys in <code style="border-color:#DDE3DB; color:red;">~/.ssh</code>, a public key and a private key. We are going to copy our public key to the server. If we don't have access to this server, we will send it to an admin of Anarcha server so that she can do it.  


::When generating the key, you will need to introduce a '''passphrase'''.
<br>
<br>
===›››››››››››››› Copy public key to the server===


$ ssh-keygen -t rsa -b 4096
::After generating an SSH key pair, you will want to copy your public key to your new server.


By default it will generate two keys in ~/.ssh, a public key and a private key. The public key we will send to an admin of Anarcha server.
::To securely copy the public key, we shall enable password authentication on the remote server, by editing the <code style="border-color:#DDE3DB; color:red;">/etc/ssh/sshd_config</code> file, and disable it after having copied the public key
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">sudo nano /etc/ssh/sshd_config</pre>


===== Send public key to the server =====
::uncomment and set <code style="border-color:#DDE3DB; color:red;">PasswordAuthentication</code> to <code style="border-color:#DDE3DB; color:red;">yes</code> and then to <code style="border-color:#DDE3DB; color:red;">no</code> after copying is done.


After generating an SSH key pair, you will want to copy your public key to your new server.
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">PasswordAuthentication yes</pre>


Assuming you generated an SSH key pair using the previous step, use the following command at the terminal of your local machine to print your public key (id_rsa.pub):
::To copy your public key, run the following command  


  cat ~/.ssh/id_rsa.pub
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">ssh-copy-id <user>@<server></pre>
   
::To enable the use of SSH key to authenticate as the new remote user, you must add the public key to a special file in the <code style="border-color:#DDE3DB; color:red;">.ssh</code> directory in the user's home directory. Assuming that we are going to create superuser accounts for each new admin we will do the following:
<br>
<br>


This should print your public SSH key, which should look something like the following:
===›››››››››››››› Creating new sysadmin accounts===
   
::Changing to root user       
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">sudo su</pre>
::Check the sudo users
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%"> grep '^sudo:.*$' /etc/group | cut -d: -f4</pre>


  ssh-rsa AAAB3NzaC1yc2EAAAADAQABAAABAQDBGTO0tsVejssuaYR5R3Y/i73SppJAhme1dH7W2c47d4gOqB4izP0+fRLfvbz/tnXFz4iOP/H6eCV05hqUhF+KYRxt9Y8tVMrpDZR2l75o6+xSbUOMu6xN+uVF0T9XzKcxmzTmnV7Na5up3QM3DoSRYX/EP3utr2+zAqpJIfKPLdA74w7g56oYWI9blpnpzxkEd3edVJOivUkpZ4JoenWManvIaSdMTJXMy3MtlQhva+j9CgguyVbUkdzK9KKEuah+pFZvaugtebsU+bllPTB0nlXGIJk98Ie9ZtxuY3nCKneB+KjKiXrAvXUPCI9mWkYS/1rggpFmu3HbXBnWSUdf localuser@machine.local
::If you are not yet, we create a ''superuser'' ['''note''': it will ask me to introduce a passwd]     
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">adduser <superusername></pre>
   
::Then we will add the ''superuser'' to sudoers:       
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">usermod -aG sudo <superuser></pre>
   
::We now change to ''superuser'':       
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">su <superuser></pre>


Select the public key, and copy it to your clipboard or send it by email.
::When we change user, we will be by default in the user's home directory [<code style="border-color:#DDE3DB; color:red;">~</code>]. You can do <code style="border-color:#DDE3DB; color:red;">cd ~</code> just in case.  


=== creating new sysadmin accounts ===
::It is also possible to create a user [<code style="border-color:#DDE3DB; color:red;">waters</code> for example] and give it a limited privilege [to execute a certain command[s] as ''root'']. To do this, create a new file [<code style="border-color:#DDE3DB; color:red;">waters</code> for instance] in the <code style="border-color:#DDE3DB; color:red;">/etc/sudoers.d/</code> directory


::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">touch /etc/sudoers.d/waters</pre>


'''Changing to root user'''
::and edit it with <code style="border-color:#DDE3DB; color:red;">visudo</code> ['''this is very important''' as it alerts you regarding any syntax errors]
$ sudo su


'''Check the sudo users'''
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">sudo visudo /etc/sudoers.d/waters</pre>
$ grep '^sudo:.*$' /etc/group | cut -d: -f4


''' If you are not yet - create a superuser'''
::and add the following line to allow the user <code style="border-color:#DDE3DB; color:red;">waters</code> to use a <code style="border-color:#DDE3DB; color:red;">chmod</code> command for example
$ adduser superusername
'''add a superuser to the group sudoers'''
$ sudo addgroup (username) sudo


=== creating .ssh directory and authorized_keys document in the superuser home directory ===
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">waters ALL=(ALL) /usr/bin/chmod</pre>


::To allow user <code style="border-color:#DDE3DB; color:red;">waters</code> to run a <code style="border-color:#DDE3DB; color:red;">chmod</code> command without entering a password edit the file this way


As the superuser to whom we want to give ssh access to the server, create a new directory called .ssh and restrict its permissions with the following commands:
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">waters ALL= NOPASSWD: /usr/bin/chmod</pre>


$ mkdir .ssh
<br>
$ chmod 700 .ssh
<br>


===›››››››››››››› .ssh directory and authorized_keys===


Now open a file in .ssh called authorized_keys with a text editor. We will use nano to edit the file:
::'''Creating <code style="border-color:#DDE3DB; color:red;">.ssh</code> directory and <code style="border-color:#DDE3DB; color:red;">authorized_keys</code> document in the superuser home directory'''.


$ nano .ssh/authorized_keys
::As the superuser to whom we want to give ssh access to the server, create a new directory called <code style="border-color:#DDE3DB; color:red;">.ssh</code> and restrict its permissions with the following commands:


Now copy-paste the public key by pasting it into the editor.
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">mkdir .ssh</pre>
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">chmod 700 .ssh</pre>
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">chown superuser:group</pre>
     
::Now open a file in <code style="border-color:#DDE3DB; color:red;">.ssh</code> called <code style="border-color:#DDE3DB; color:red;">authorized_keys</code> with a text editor. We will use nano to edit the file:


Save and close.
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">nano .ssh/authorized_keys</pre>
       
::Now copy-paste the public key [which should be in your clipboard] by pasting it into the editor. Save and close.


::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">ctrl + s</pre>
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">ctrl + x</pre>
::Now restrict the permissions of the <code style="border-color:#DDE3DB; color:red;">authorized_keys</code> file with this command:


Now restrict the permissions of the authorized_keys file with this command:
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">chmod 600 .ssh/authorized_keys</pre>
$ chmod 600 .ssh/authorized_keys
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">chown superuser:group</pre>
     
::Type this command once to return to the root user:
   
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">exit</pre>


::As we are creating a new user + authentication as root, we have to change the owner of the <code style="border-color:#DDE3DB; color:red;">.ssh</code> directory to the new user [recursively <code style="border-color:#DDE3DB; color:red;">- R</code>]


As we are creating a new user + authentication as root, we have to change the owner of the .ssh directory to the new user (recursively -R)
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">chown <user>:<user> .ssh -R</pre>
$ chown (user):(user) .ssh -R


::Now you [the new sysadmin] may SSH login as your new user, using the private key as authentication.
<br>
<br>
====. . . . . . › After reboot====


Now the new sysadmin may SSH login as your new user, using the private key as authentication.
::Access physical mainframe to reboot the encrypted virtual machine. Ask permissions to the syteradmins, by posting on the mailingslist <code style="border-color:#DDE3DB; color:red;">anarchaserver@lists.systerserver.net</code>
<br>
<br>
====. . . . . . › SSH Reverse DNS Lookup Disable====


===== After reboot  =====
::The invalid logins are normal, since there are bots that try to bruteforce servers.
Access physical mainframe to reboot the encrypted virtual machine. Ask permissions to the syteradmins, by posting on the mailingslist
::As for the "possible break-in attempt" message, The system is trying to do a reverse DNS lookup to match the connecting IP with the ''hostname'' that is trying to connect and fails to do so.
anarchaserver@lists.systerserver.net
::The setting that controls that is <code style="border-color:#DDE3DB; color:red;">UseDNS</code> in <code style="border-color:#DDE3DB; color:red;">/etc/ssh/sshd_config</code>
 
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">sudo nano /etc/ssh/sshd_config</pre>
 
::y agrega la línea:
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">UseDNS no</pre>
 
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">sudo service ssh restart</pre>
 
::Force ssh login to server
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">sudo nano /etc/ssh/sshd_config</pre>
 
::and change & uncomment:
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">ChallengeResponseAuthentication no</pre>
 
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">PasswordAuthentication no</pre>
 
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">UsePAM yes</pre>
 
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">sudo service ssh restart</pre>
<br>
<br>
 
====. . . . . . › Change ssh port access====
 
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">sudo nano /etc/ssh/sshd_config</pre>
 
::The first option that you may want to change is the port that SSH runs on. Find the line that looks like this: <code style="border-color:#DDE3DB; color:red;">Port 22</code>
 
::If we change this number to something in '''between 1025 and 65536''', the SSH service on our server will look for connections on a different port. This is sometimes helpful because unauthorized users sometimes try to break into servers by attacking SSH. If you change the location, they will need to complete the extra step of sniffing it out.
::If you change this value, you will need to keep in mind that your server is running on the new port.
 
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">service ssh restart</pre>
 
::So now you would have to access to the server like this:
 
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">ssh <superuser>@<server> -p <portnumber></pre>
<br>
<br>
===›››››››››››››› References===
 
::_ [http://ubuntuforums.org/showthread.php?t=1773227  Thread: log full of "POSSIBLE BREAK-IN ATTEMPT!" ]
::_ [https://www.simplified.guide/ssh/restart-service How to start, restart and stop SSH service]
::_ [https://www.simplified.guide/ssh/copy-public-key How to add SSH key to server]
::_ [https://www.golinuxcloud.com/add-user-to-sudoers/ How to add user to sudoers with best practices & examples]
::_ [https://www.cyberciti.biz/faq/linux-unix-running-sudo-command-without-a-password/ How to run sudo command without a password on a Linux or Unix]
</div>

Revision as of 00:51, 24 June 2021




›››››››››››››› SSH

SSH keys provide a more secure way of logging into a virtual private server with SSH than using a password alone. While a password can eventually be cracked with a brute force attack, SSH keys are nearly impossible to decipher by brute force alone. Generating a key pair provides you with two long string of characters: a public and a private key. You can place the public key on any server, and then unlock it by connecting to it with a client that already has the private key. When the two match up, the system unlocks without the need for a password. You can increase security even more by protecting the private key with a passphrase.



›››››››››››››› Making changes in ssh

Remember to:
 sudo service ssh restart
Tip: I can check all available services in /etc/init.d
Attention: if there are problems, grep to see if it actually stops the service Like this:
 sudo service ssh stop
 ps aux | grep ssh
 sudo service ssh start
 ps aux | grep ssh



›››››››››››››› Debug

Debug with a -v flag. max 3 "v"s
 ssh <user>@<server> -vvv 



›››››››››››››› Create RSA Key Pair

You create your RSA key pair in the computer from which you want to log to the server.
I can use this key pair for several servers, but if I want to enter a same server from different computers, I create different rsa key pairs.
Lo voy a crear a nivel local [en mi compu] y esa va a ser la llave rsa ssh que voy a usar para entrar en mis vps. NO genero una para cada vps sino que esa misma me sirve para todas. Si voy a usar varios ordenadores para entrar en un vps, sí necesitaré crear una llave ssh por máquina desde la cual estoy accediendo.
So each admin in a server is going to have at least one rsa key pair.
Be careful to pay attention from which user you create this in your local machine [computer]. Since the rsa key pairs are saved in a hidden directory in my user home directory [~/.ssh], if Im trying to login from a different user, it will confuse home directories since each user has a different home directory.
So, from the user in my localmachine that I wish to use to access the server, I will create my rsa key pair:
ssh-keygen -t rsa -b 4096
By default it will generate two keys in ~/.ssh, a public key and a private key. We are going to copy our public key to the server. If we don't have access to this server, we will send it to an admin of Anarcha server so that she can do it.
When generating the key, you will need to introduce a passphrase.



›››››››››››››› Copy public key to the server

After generating an SSH key pair, you will want to copy your public key to your new server.
To securely copy the public key, we shall enable password authentication on the remote server, by editing the /etc/ssh/sshd_config file, and disable it after having copied the public key
sudo nano /etc/ssh/sshd_config
uncomment and set PasswordAuthentication to yes and then to no after copying is done.
PasswordAuthentication yes
To copy your public key, run the following command
ssh-copy-id <user>@<server>
To enable the use of SSH key to authenticate as the new remote user, you must add the public key to a special file in the .ssh directory in the user's home directory. Assuming that we are going to create superuser accounts for each new admin we will do the following:



›››››››››››››› Creating new sysadmin accounts

Changing to root user
sudo su
Check the sudo users
 grep '^sudo:.*$' /etc/group | cut -d: -f4
If you are not yet, we create a superuser [note: it will ask me to introduce a passwd]
adduser <superusername>
Then we will add the superuser to sudoers:
usermod -aG sudo <superuser>
We now change to superuser:
su <superuser>
When we change user, we will be by default in the user's home directory [~]. You can do cd ~ just in case.
It is also possible to create a user [waters for example] and give it a limited privilege [to execute a certain command[s] as root]. To do this, create a new file [waters for instance] in the /etc/sudoers.d/ directory
touch /etc/sudoers.d/waters
and edit it with visudo [this is very important as it alerts you regarding any syntax errors]
sudo visudo /etc/sudoers.d/waters
and add the following line to allow the user waters to use a chmod command for example
waters ALL=(ALL) /usr/bin/chmod
To allow user waters to run a chmod command without entering a password edit the file this way
waters ALL= NOPASSWD: /usr/bin/chmod



›››››››››››››› .ssh directory and authorized_keys

Creating .ssh directory and authorized_keys document in the superuser home directory.
As the superuser to whom we want to give ssh access to the server, create a new directory called .ssh and restrict its permissions with the following commands:
mkdir .ssh
chmod 700 .ssh
chown superuser:group
Now open a file in .ssh called authorized_keys with a text editor. We will use nano to edit the file:
nano .ssh/authorized_keys
Now copy-paste the public key [which should be in your clipboard] by pasting it into the editor. Save and close.
ctrl + s
ctrl + x
Now restrict the permissions of the authorized_keys file with this command:
chmod 600 .ssh/authorized_keys
chown superuser:group
Type this command once to return to the root user:
exit
As we are creating a new user + authentication as root, we have to change the owner of the .ssh directory to the new user [recursively - R]
chown <user>:<user> .ssh -R
Now you [the new sysadmin] may SSH login as your new user, using the private key as authentication.



. . . . . . › After reboot

Access physical mainframe to reboot the encrypted virtual machine. Ask permissions to the syteradmins, by posting on the mailingslist anarchaserver@lists.systerserver.net



. . . . . . › SSH Reverse DNS Lookup Disable

The invalid logins are normal, since there are bots that try to bruteforce servers.
As for the "possible break-in attempt" message, The system is trying to do a reverse DNS lookup to match the connecting IP with the hostname that is trying to connect and fails to do so.
The setting that controls that is UseDNS in /etc/ssh/sshd_config
sudo nano /etc/ssh/sshd_config
y agrega la línea:
UseDNS no
sudo service ssh restart
Force ssh login to server
sudo nano /etc/ssh/sshd_config
and change & uncomment:
ChallengeResponseAuthentication no
PasswordAuthentication no
UsePAM yes
sudo service ssh restart



. . . . . . › Change ssh port access

sudo nano /etc/ssh/sshd_config
The first option that you may want to change is the port that SSH runs on. Find the line that looks like this: Port 22
If we change this number to something in between 1025 and 65536, the SSH service on our server will look for connections on a different port. This is sometimes helpful because unauthorized users sometimes try to break into servers by attacking SSH. If you change the location, they will need to complete the extra step of sniffing it out.
If you change this value, you will need to keep in mind that your server is running on the new port.
service ssh restart
So now you would have to access to the server like this:
ssh <superuser>@<server> -p <portnumber>



›››››››››››››› References

_ Thread: log full of "POSSIBLE BREAK-IN ATTEMPT!"
_ How to start, restart and stop SSH service
_ How to add SSH key to server
_ How to add user to sudoers with best practices & examples
_ How to run sudo command without a password on a Linux or Unix