giovedì, Aprile 25, 2024

Rsync over SSH con autenticazione RSA

Giovanni "Jinko" Uccio
Giovanni "Jinko" Ucciohttps://ie.linkedin.com/in/giovanniuccio
- Software Engineer - Debian Evangelist - Escalation Engineer - Debianizzati.org

Vedremo (completo di tutti i passi) la sincronizzazione di file/cartelle su un client linux da un server linux, over ssh con autenticazione tramite chiave RSA e senza bisogno dell’ inserimento della password in modo che si potrebbe pensare ad uno script bash da inserire in cron per poter effettuare una sincronizzazione/backup temporizzata (argomento che non verrà trattato in questo articolo):

Configurazione Client Side

1) generare la chiave rsa (ipotizziamo un utente dal nome “user” presente sulla macchina client):

cd /home/user

mkdir .ssh

cd .ssh

ssh-keygen -t rsa      # ( -t specifica il tipo di chiave da creare)

dopo aver battuto invio ci verranno effettuate alcune domande, ovvero:

Generating public/private rsa key pair:

Enter file in which to save the key (/home/user/.ssh/id_rsa):

premiamo invio per lasciare il path di default

Enter passphrase (empty for no passphrase):

battiamo semplicemente invio:

Enter same passphrase again:

ancora una volta battiamo invio, e avremo in output una cosa del genere:

Your identification has been saved in /home/user/.ssh/id_rsa

Your public key has been saved in /home/user/.ssh/id_rsa.pub

The public key fingerprint is:

00:3b:15:09:d4:f6:d2:f1:5d:45:de:82:43:67:16:01 user@hostname

adesso all’interno della cartella /home/user/.ssh , troveremo questi file:

$ ls

id_rsa id_rsa.pub

adesso tramite scp copiamo la nostra chiave pubblica sul server:

$ scp id_rsa.pub utente_server@INDIRIZZO_IP_SERVER:/home/utente_server/.ssh

Configurazione Server Side

cd /home/utente_server/.ssh

cat id_rsa.pub > authorized_keys

#consiglio: controllate sempre i permessi, altrimenti:

chown utente_server:gruppo authorized_keys

chmod 600 authorized_keys

Configurazione demone SSH per autorizzazione senza upassword ma con chiave RSA

di seguito troverete un file sshd_config (/etc/ssh/sshd_config) configurato per permettere la connessione che noi vogliamo (tutti i paramentri come listenaddress, porte e via scorrendo sono stati lasciati di default appositamente, l’utente configurerà questi parametri a seconda delle proprie esigenze:

(da utente root)

$ nano /etc/ssh/sshd_config
#########################################################################
# $OpenBSD: sshd_config,v 1.73 2005/12/06 22:38:28 reyk Exp $
# This is the sshd server system-wide configuration file. See
# sshd_config(5) for more information.
# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin
# The strategy used for options in the default sshd_config shipped with
# OpenSSH is to specify options with their default value where
# possible, but leave them commented. Uncommented options change a
# default value.
Port 22
#Protocol 2,1
Protocol 2
#AddressFamily any
#ListenAddress 0.0.0.0
#ListenAddress ::
# HostKey for protocol version 1
#HostKey /etc/ssh/ssh_host_key
# HostKeys for protocol version 2
#HostKey /etc/ssh/ssh_host_rsa_key
#HostKey /etc/ssh/ssh_host_dsa_key
# Lifetime and size of ephemeral version 1 server key
#KeyRegenerationInterval 1h
ServerKeyBits 1024
# Logging
# obsoletes QuietMode and FascistLogging
#SyslogFacility AUTH
SyslogFacility AUTHPRIV
#LogLevel INFO
# Authentication:
#LoginGraceTime 2m
PermitRootLogin no <-------- NON VIENE PERMESSO IL LOGIN COME UTENTE ROOT
#StrictModes yes
#MaxAuthTries 6
RSAAuthentication yes <------- ABILITA L'AUTENTICAZIONE RSA
PubkeyAuthentication yes <------- ABILITA L'AUTENTICAZIONE CON CHIAVE PUBBLICA
AuthorizedKeysFile .ssh/authorized_keys <---- CARTELLA FILE authorized_keys
# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
#HostbasedAuthentication no
# Change to yes if you don't trust ~/.ssh/known_hosts for
# RhostsRSAAuthentication and HostbasedAuthentication
#IgnoreUserKnownHosts no
# Don't read the user's ~/.rhosts and ~/.shosts files
#IgnoreRhosts yes
# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication no <----- DISABILITA L'AUTENTICAZIONE TRAMITE PASSWORD
# Change to no to disable s/key passwords
#ChallengeResponseAuthentication yes
ChallengeResponseAuthentication no
# Kerberos options
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosGetAFSToken no
# GSSAPI options
#GSSAPIAuthentication no
GSSAPIAuthentication no
#GSSAPICleanupCredentials yes
GSSAPICleanupCredentials yes
# Set this to 'yes' to enable PAM authentication, account processing,
# and session processing. If this is enabled, PAM authentication will
# be allowed through the ChallengeResponseAuthentication mechanism.
# Depending on your PAM configuration, this may bypass the setting of
# PasswordAuthentication, PermitEmptyPasswords, and
# "PermitRootLogin without-password". If you just want the PAM account and
# session checks to run without PAM authentication, then enable this but set
# ChallengeResponseAuthentication=no
#UsePAM no
UsePAM yes
# Accept locale-related environment variables
AcceptEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
AcceptEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
AcceptEnv LC_IDENTIFICATION LC_ALL
#AllowTcpForwarding yes
#GatewayPorts no
#X11Forwarding no
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
#PrintMotd yes
#PrintLastLog yes
#TCPKeepAlive yes
#UseLogin no
#UsePrivilegeSeparation yes
#PermitUserEnvironment no
#Compression delayed
#ClientAliveInterval 0
#ClientAliveCountMax 3
#ShowPatchLevel no
#UseDNS yes
#PidFile /var/run/sshd.pid
#MaxStartups 10
#PermitTunnel no
#ChrootDirectory none
# no default banner path
#Banner /some/path
# override default of no subsystems
Subsystem sftp /usr/libexec/openssh/sftp-server
#########################################################################

Sincronizzazione

questa è la parte “meno faticosa” poichè consiste in un unico comando:

rsync -avz -e ssh utente_server@INDIRIZZO_IP_SERVER:/directory_server /directory_locale

andiamo ad illustrare le opzioni passate a questo comando, nel particolare:

-avz : trasferisce ricorsivamente tutti i file dall directory_server alla
       directory_locale. Cosa interessante  e che i file vengono trasferiti
       in una modalità che si chiama "archive", essa assicura che link simbolici,
       device, attributi,  permessi, proprietari siano preservati durante il
       trasferimento, in più essi vengono compressi in modo da ridurre la
       dimensione dei file da trasferire.

-e :   viene specificato la shell remota da utilizzare

Un saluto,
Giovanni Uccio

Articoli correlati

Non perdere il lancio online della Community GDPR Day: 26 marzo 2024

La sicurezza dei dati e delle informazioni non è più un'opzione, ma una necessità imprescindibile. Lo dimostrano i tanti attacchi informatici che, con frequenza...

Digital Transformation


 

Noleggia una Tesla per il tuo evento ICT!

Categorie