Difference between revisions of "Access server"
From Anarchaserver
(→SSH) |
|||
(55 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
<div style="font-family:monospace; font-size:14px; background-color:#E9F8A8;"> | |||
===›››››››››››››› SSH=== | |||
SSH keys provide | :: '''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> | |||
::'''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> | |||
===›››››››››››››› Debug=== | |||
::Debug with a <code style="border-color:#DDE3DB; color:red;">-v</code> flag. max 3 "v"s | |||
:: <pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%"> ssh <user>@<server> -vvv </pre> | |||
<br> | |||
<br> | |||
===›››››››››››››› 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. | |||
Lo voy a crear a nivel local | |||
So each admin in a server is going to have at least one rsa key pair. | ::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 | ::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. | ||
So, from the user in my localmachine that I wish to use to access the server, I will create my 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: | ||
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">ssh-keygen -t rsa -b 4096</pre> | |||
By default it will generate two keys in | ::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 | ::When generating the key, you will need to introduce a '''passphrase'''. | ||
<br> | |||
<br> | |||
===›››››››››››››› 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 <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> | |||
::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. | |||
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">PasswordAuthentication yes</pre> | |||
::To copy your public key, run the following command | |||
ssh- | ::<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> | |||
===›››››››››››››› 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> | |||
::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> | |||
::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. | |||
::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> | |||
::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] | |||
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">sudo visudo /etc/sudoers.d/waters</pre> | |||
::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 | |||
::<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 | ||
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">waters ALL= NOPASSWD: /usr/bin/chmod</pre> | |||
<br> | |||
<br> | |||
===›››››››››››››› .ssh directory and authorized_keys=== | |||
::'''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'''. | |||
::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: | |||
::<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: | |||
Now | ::<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: | |||
Type this command once to return to the root user: | ::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">chmod 600 .ssh/authorized_keys</pre> | ||
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">chown superuser:superuser</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>] | |||
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%">chown <user>:<user> .ssh -R</pre> | |||
::Now you [the new sysadmin] may SSH login as your new user, using the private key as authentication. | |||
<br> | |||
==== | ====. . . . . . › How to login with a Host name ==== | ||
::You can more smoothly login by ssh with a Host name [ssh anarchaserver, for example] as opposed to ssh superuser@[ip] you'll need to modify the config file in .ssh directory. Open it: | |||
::<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%"> sudo nano ~/.ssh/config</pre> | |||
:: | |||
Host anarchaserver | |||
User superuser | |||
Hostname [IP] | |||
Port 22 | |||
Identityfile /home/superuser/.ssh/[key] | |||
Serveraliveinterval 30 | |||
:: Now you can ssh as Host | |||
<pre style="background-color:#F2E4F5; border-color:#7F1594; color:blue; width:90%"> ssh anarchaserver</pre> | |||
<br> | |||
====. . . . . . › After reboot==== | |||
::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==== | |||
== | ::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 <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> | ||
sudo | ::<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 you change this value, you will need to keep in mind that your server is running on the new port. | ::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] | |||
http://ubuntuforums.org/showthread.php?t=1773227 | ::_ [https://pzwiki.wdka.nl/mediadesign/User:E.zn/rpi#Login_with_a_Host_name / How to make config file] | ||
Latest revision as of 10:13, 13 June 2023
›››››››››››››› 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
- Debug with a
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.
- 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 [
- 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.
- By default it will generate two keys in
- 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
- To securely copy the public key, we shall enable password authentication on the remote server, by editing the
sudo nano /etc/ssh/sshd_config
- uncomment and set
PasswordAuthentication
toyes
and then tono
after copying is done.
- uncomment and set
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:
- 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
›››››››››››››› 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 docd ~
just in case.
- When we change user, we will be by default in the user's home directory [
- 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
- It is also possible to create a user [
touch /etc/sudoers.d/waters
- and edit it with
visudo
[this is very important as it alerts you regarding any syntax errors]
- and edit it with
sudo visudo /etc/sudoers.d/waters
- and add the following line to allow the user
waters
to use achmod
command for example
- and add the following line to allow the user
waters ALL=(ALL) /usr/bin/chmod
- To allow user
waters
to run achmod
command without entering a password edit the file this way
- To allow user
waters ALL= NOPASSWD: /usr/bin/chmod
›››››››››››››› .ssh directory and authorized_keys
- Creating
.ssh
directory andauthorized_keys
document in the superuser home directory.
- Creating
- 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:
- As the superuser to whom we want to give ssh access to the server, create a new directory called
mkdir .ssh
chmod 700 .ssh
chown superuser:group
- Now open a file in
.ssh
calledauthorized_keys
with a text editor. We will use nano to edit the file:
- Now open a file in
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:
- Now restrict the permissions of the
chmod 600 .ssh/authorized_keys
chown superuser:superuser
- 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
]
- As we are creating a new user + authentication as root, we have to change the owner of the
chown <user>:<user> .ssh -R
- Now you [the new sysadmin] may SSH login as your new user, using the private key as authentication.
. . . . . . › How to login with a Host name
- You can more smoothly login by ssh with a Host name [ssh anarchaserver, for example] as opposed to ssh superuser@[ip] you'll need to modify the config file in .ssh directory. Open it:
sudo nano ~/.ssh/config
Host anarchaserver User superuser Hostname [IP] Port 22 Identityfile /home/superuser/.ssh/[key] Serveraliveinterval 30
- Now you can ssh as Host
ssh anarchaserver
. . . . . . › 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
- Access physical mainframe to reboot the encrypted virtual machine. Ask permissions to the syteradmins, by posting on the mailingslist
. . . . . . › 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
- The first option that you may want to change is the port that SSH runs on. Find the line that looks like this:
- 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>