Accedere ad un pc remoto con SSH

SSH è un protocollo di rete che permette lo scambio dei dati (in modo sicuro) tra due computer, la porta utilizzata è la 22. Il server utilizza un demone sshd per le accettare le connessioni.



Installazione

Per installare ssh su Linux è necessario aggiungere al nostro parco software il pacchetto ssh.
Con apt:
$ sudo apt-get install ssh

Con pacman (Archlinux):
$ sudo pacman -S openssh

Configurazione

Dopo aver installato il pacchetto troverete nella vostra cartella /etc/ i seguenti files:
- /etc/ssh/sshd_config -> file di configurazione per il demone
- /etc/ssh/ssh_config -> file di configurazione per il client

Una configurazione base del demone (/etc/ssh/sshd_config) può essere la seguente:
# What ports, IPs and protocols we listen for
Port 22
# Use these options to restrict which interfaces/protocols sshd will bind to
#ListenAddress ::
#ListenAddress 0.0.0.0
Protocol 2
# HostKeys for protocol version 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
#Privilege Separation is turned on for security
UsePrivilegeSeparation yes
# AllowTcpForwarding yes


# Lifetime and size of ephemeral version 1 server key
KeyRegenerationInterval 3600
ServerKeyBits 768

# Logging
SyslogFacility AUTH
LogLevel INFO

# Authentication:
LoginGraceTime 120
PermitRootLogin yes
StrictModes yes

RSAAuthentication yes
PubkeyAuthentication yes
#AuthorizedKeysFile %h/.ssh/authorized_keys

# Don't read the user's ~/.rhosts and ~/.shosts files
IgnoreRhosts yes
# For this to work you will also need host keys in /etc/ssh_known_hosts
RhostsRSAAuthentication no
# similar for protocol version 2
HostbasedAuthentication no
# Uncomment if you don't trust ~/.ssh/known_hosts for RhostsRSAAuthentication
#IgnoreUserKnownHosts yes

# To enable empty passwords, change to yes (NOT RECOMMENDED)
PermitEmptyPasswords no

# Change to yes to enable challenge-response passwords (beware issues with
# some PAM modules and threads)
ChallengeResponseAuthentication no

# Change to no to disable tunnelled clear text passwords
PasswordAuthentication yes

# Kerberos options
#KerberosAuthentication no
#KerberosGetAFSToken no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes

# GSSAPI options
#GSSAPIAuthentication no
#GSSAPICleanupCredentials yes

# X11 tunneling options
X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost yes

PrintMotd no
PrintLastLog yes
TCPKeepAlive yes
#UseLogin no

#MaxStartups 10:30:60
#Banner /etc/issue.net

# Allow client to pass locale environment variables
AcceptEnv LANG LC_*

Subsystem sftp /usr/lib/openssh/sftp-server

# 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 and
# PasswordAuthentication.  Depending on your PAM configuration,
# PAM authentication via ChallengeResponseAuthentication may bypass
# the setting of "PermitRootLogin without-password".
# If you just want the PAM account and session checks to run without
# PAM authentication, then enable this but set PasswordAuthentication
# and ChallengeResponseAuthentication to 'no'.
UsePAM yes
- Come potete notare è permesso il root-login. Chiaramente se non siete sicuri di chi potrebbe connettersi al vostro server sarebbe preferibile dare l'opzione "no".
Anche la porta in ascolto di default è settata a 22 mentre, per maggiore sicurezza, bisognerebbe assegnare una porta differente.
- per eseguire applicazioni X è necessario specificare le impostazioni come sopra (# X11 tunneling options).

Avviare il server

Per avviare il server una volta fatte le vostre configurazioni basterà digitare in console:
$ sudo /etc/init.d/ssh start

Se il demone è già avviato invece usate:
$ sudo /etc/init.d/ssh restart

Collegarsi al server

Naturalmente il client per eseguire il collegamento deve avere a disposizione il comando ssh, di solito installato di default.

Per connetervi ad una macchina remota avete bisogno dell'ip della stessa, della porta utilizzata e del nome utente per il login.
$ ssh -p  @

Ad esempio:
$ ssh -p 22 antonio@192.168.0.102

Se tutto è andato a buon fine vi verrà chiesta la password per accedere.

Per chiarimenti vari vi ricordo che esiste la possibilità di commentare il post! :)

Commenti