SSH
From ArchWiki
i18n |
---|
English |
Italiano |
Русский |
简体中文 |
Contents |
Introduction
Secure Shell or SSH is a network protocol that allows data to be exchanged over a secure channel between two computers. Encryption provides confidentiality and integrity of data. SSH uses public-key cryptography to authenticate the remote computer and allow the remote computer to authenticate the user, if necessary.
SSH is typically used to log into a remote machine and execute commands, but it also supports tunneling, forwarding arbitrary TCP ports and X11 connections; it can transfer files using the associated SFTP or SCP protocols.
An SSH server, by default, listens on the standard TCP port 22. An ssh client program is typically used for establishing connections to an sshd daemon accepting remote connections. Both are commonly present on most modern operating systems, including Mac OS X, Linux, Solaris and OpenVMS. Proprietary, freeware and open source versions of various levels of complexity and completeness exist.
OpenSSH
OpenSSH (OpenBSD Secure Shell) is a set of computer programs providing encrypted communication sessions over a computer network using the ssh protocol. It was created as an open source alternative to the proprietary Secure Shell software suite offered by SSH Communications Security. OpenSSH is developed as part of the OpenBSD project, which is led by Theo de Raadt.
OpenSSH is occasionally confused with the similarly-named OpenSSL; however, the projects have different purposes and are developed by different teams, the similar name is drawn only from similar goals.
Installing OpenSSH
# pacman -Sy openssh
Configuring SSH
Client
The SSH client configuration file can be found and edited in /etc/ssh/ssh_config.
An example configuration:
# $OpenBSD: ssh_config,v 1.25 2009/02/17 01:28:32 djm Exp $ # This is the ssh client system-wide configuration file. See # ssh_config(5) for more information. This file provides defaults for # users, and the values can be changed in per-user configuration files # or on the command line. # Configuration data is parsed as follows: # 1. command line options # 2. user-specific file # 3. system-wide file # Any configuration value is only changed the first time it is set. # Thus, host-specific definitions should be at the beginning of the # configuration file, and defaults at the end. # Site-wide defaults for some commonly used options. For a comprehensive # list of available options, their meanings and defaults, please see the # ssh_config(5) man page. Host * # ForwardAgent no # ForwardX11 no # RhostsRSAAuthentication no # RSAAuthentication yes # PasswordAuthentication yes # HostbasedAuthentication no # GSSAPIAuthentication no # GSSAPIDelegateCredentials no # BatchMode no # CheckHostIP yes # AddressFamily any # ConnectTimeout 0 # StrictHostKeyChecking ask # IdentityFile ~/.ssh/identity # IdentityFile ~/.ssh/id_rsa # IdentityFile ~/.ssh/id_dsa # Port 22 # Protocol 2,1 # Cipher 3des # Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc # MACs hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160 # EscapeChar ~ # Tunnel no # TunnelDevice any:any # PermitLocalCommand no # VisualHostKey no HashKnownHosts yes StrictHostKeyChecking ask
It is recommended to change the Protocol line into this:
Protocol 2
That means that only Protocol 2 will be used, since Protocol 1 is considered somewhat insecure.
Daemon
The SSH daemon configuration file can be found and edited in /etc/ssh/sshd_config.
An example configuration:
# $OpenBSD: sshd_config,v 1.75 2007/03/19 01:01:29 djm Exp $ # This is the sshd server system-wide configuration file. See # sshd_config(5) for more information. # This sshd was compiled with PATH=/usr/bin:/bin:/usr/sbin:/sbin # 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 ListenAddress 0.0.0.0 #ListenAddress :: # HostKey for protocol version 1 #HostKey /etc/ssh/sshhostkey # HostKeys for protocol version 2 #HostKey /etc/ssh/sshhostrsa_key #HostKey /etc/ssh/sshhostdsa_key # Lifetime and size of ephemeral version 1 server key #KeyRegenerationInterval 1h #ServerKeyBits 768 # Logging #obsoletes ~QuietMode and ~FascistLogging #SyslogFacility AUTH #LogLevel INFO # Authentication: #LoginGraceTime 2m #PermitRootLogin yes #StrictModes yes #MaxAuthTries 6 #RSAAuthentication yes #PubkeyAuthentication yes #AuthorizedKeysFile .ssh/authorized_keys # For this to work you will also need host keys in /etc/ssh/sshknownhosts #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 # Change to no to disable s/key passwords #ChallengeResponseAuthentication yes # Kerberos options #KerberosAuthentication no #KerberosOrLocalPasswd yes #KerberosTicketCleanup yes #KerberosGetAFSToken no # GSSAPI options #GSSAPIAuthentication no #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 #AllowTcpForwarding yes #GatewayPorts no #X11Forwarding no #X11DisplayOffset 10 #X11UseLocalhost yes #PrintMotd yes #PrintLastLog yes #TCPKeepAlive yes #UseLogin no #UsePrivilegeSeparation yes #PermitUserEnvironment no #Compression yes #ClientAliveInterval 0 #ClientAliveCountMax 3 #UseDNS yes #PidFile /var/run/sshd.pid #MaxStartups 10 # no default banner path #Banner /some/path # override default of no subsystems Subsystem sftp /usr/lib/ssh/sftp-server
To allow access only for some users add this line:
AllowUsers user1 user2
You might want to change some lines so that they look as following:
Protocol 2 . . . LoginGraceTime 120 . . . PermitRootLogin no # (put yes here if you want root login)
You could also uncomment the BANNER option and edit /etc/issue for a nice welcome message.
Even though the port ssh is running on could be detected by using a port-scanner like nmap, changing it will reduce the number of log entries caused by automated authentication attempts.
Allowing others in
To let other people ssh to your machine you need to adjust /etc/hosts.allow, add the following:
# let everyone connect to you sshd: ALL # OR you can restrict it to a certain ip sshd: 192.168.0.1 # OR restrict for an IP range sshd: 10.0.0.0/255.255.255.0 # OR restrict for an IP match sshd: 192.168.1.
Now you should check your /etc/hosts.deny for the following line and make sure it looks like this:
ALL: ALL: DENY
That's it. You can SSH out and others should be able to SSH in :).
To start using the new configuration, restart the daemon (as root):
# /etc/rc.d/sshd restart
Managing SSHD Daemon
Just add sshd to the "DAEMONS" section of your /etc/rc.conf:
DAEMONS=(... ... sshd ... ...)
To start/restart/stop the daemon, use the following:
# /etc/rc.d/sshd {start|stop|restart}
Connecting to the server
To connect to a server, run:
$ ssh -p port user@server-address
Tips and Tricks
Encrypted Socks Tunnel
This is highly useful for laptop users connected to various unsafe wireless connections. The only thing you need is an SSH server running at a somewhat secure location, like your home or at work. It might be useful to use a dynamic DNS service like DynDNS so you don't have to remember your IP-address.
Step 1: Start the Connection
You only have to execute this single command in your favorite terminal to start the connection:
$ ssh -ND 4711 user@host
where "user" is your username at the SSH server running at the "host". It will ask for your password, and then you're connected! The "N" flag disables the interactive prompt, and the "D" flag specifies the local port on wich to listen on (you can choose any port number if you want).
One way to make this easier is to put an alias line in your ~/.bashrc file as following:
alias sshtunnel="ssh -ND 4711 -v user@host"
It's nice to add the verbose "-v" flag, because then you can verify that it's actually connected from that output. Now you just have to execute the "sshtunnel" command :)
Step 2: Configure your Browser (or other programs)
The above step is completely useless if you don't configure your web browser (or other programs) to use this newly created socks tunnel.
- For Firefox: Edit → Preferences → Advanced → Network → Connection → Setting:
- Check the "Manual proxy configuration" radio button, and enter "localhost" in the "SOCKS host" text field, and then enter your port number in the next text field (I used 4711 above).
- Make sure you select SOCKS4 as the protocol to use. This procedure will not work for SOCKS5.
Enjoy your secure tunnel!
X11 Forwarding
To run graphical programs through a SSH connection you can enable X11 forwarding. An option needs to be set in the configuration files on the server and client.
Install xorg-xauth on the server:
# pacman -Sy xorg-xauth
- Enable the AllowTcpForwarding option in sshd_config on the server.
- Enable the X11Forwarding option in sshd_config on the server.
- Set the X11DisplayOffset option in sshd_config on the server to 10.
- Enable the X11UseLocalhost option in sshd_config on the server.
- Enable the ForwardX11 option in ssh_config on the client.
To use the forwarding, log on to your server through ssh:
# ssh -X -p port user@server-address
If you receive errors trying to run graphical applications try trusted forwarding instead:
# ssh -Y -p port user@server-address
You can now start any X program on the remove server, the output will be forwarded to your local session:
# xclock
Mounting a Remote Filesystem with SSHFS
Install sshfs
# pacman -Sy sshfs
Load the Fuse module
# modprobe fuse
Add fuse to the modules array in /etc/rc.conf to load it on each system boot.
Mount the remote folder using sshfs
# mkdir ~/remote_folder # sshfs USER@remote_server:/tmp ~/remote_folder
The command above will cause the folder /tmp on the remote server to be mounted as ~/remote_folder on the local machine. Copying any file to this folder will result in transparent copying over the network using SFTP. Same concerns direct file editing, creating or removing.
When we’re done working with the remote filesystem, we can unmount the remote folder by issuing:
# fusermount -u ~/remote_folder
If we work on this folder on a daily basis, it is wise to add it to the /etc/fstab table. This way is can be automatically mounted upon system boot or mounted manually (if noauto option is chosen) without the need to specify the remote location each time. Here is a sample entry in the table:
sshfs#USER@remote_server:/tmp /full/path/to/directory fuse defaults,auto 0 0
Keep Alive
Your ssh session will automatically log out if it is idle. To keep the connection active (alive) add this to ~/.ssh/config or to /etc/ssh/ssh_config on the client.
ServerAliveInterval 5
This will send a "keep alive" signal to the server every 5 seconds. You can usually increase this interval, and I use 120.