Tuesday, July 26, 2011

Common use of SSH for UNIX and Linux systems


You typically use SSH to allow users to log in to a remote host and execute commands. However, SSH also supports tunneling and X11 connections. It can even transfer files using SFTP or SCP. SSH is applicable for numerous applications within most common platforms, including Linux, UNIX, Windows, and Apple® OS X, although some applications may require features that are only available or compatible with specific SSH clients or servers. Here are a few common SSH syntax examples:

  • Remote host shell access (supersedes telnet and rlogin clear text, insecure protocols):
# ssh fsmythe@example.com
[fsmythe@example.com] ~

  • Executing a single command on a remote host (replacing rsh):
# ssh root@example.com reboot
root@example.com's password: ******

  • Copying files from a local server to a remote host by way of the SCP command:
root@edb-01.example.com's password: ******
file1.txt      100%    0     0.0KB/s   00:00
file2.txt      100%    0     0.0KB/s   00:00

  • In combination with SFTP, as a secure substitute to FTP file transfer:
sftp fsmythe@example.com
Connecting to example.com...
fsmythe@example.com's password: *******
sftp>

  • In combination with rsync to back up, copy, and mirror files efficiently and securely to a local or remote host:
# rsync -avul --rsh=ssh /opt/edbdata/ root@example.com:/root/backup/
root@example.com's password: ******
building file list ... done
./
file1.txt
file2.txt
file3.txt
file4.txt
dir1/file5.txt
dir2/file6.txt

sent 982813 bytes  received 2116 bytes  1374860.38 bytes/sec
total size is 982138  speedup is 1.00

  • Port forwarding or tunneling a port (not to be confused with a VPN):
ssh -L 8000:mailserver:110 example.com    fsmythe@example.com's password: ********

  • Forwarding X sessions from a remote host (possible through multiple intermediate hosts):
Edit /etc/ssh/sshd_config and change 2 keywords :
AllowTcpForwarding yes
X11Forwarding yes
# service sshd restart
$ export DISPLAY
$ ssh -X fsmythe@example.com

  • With the X11 forwarding configuration in conjunction with an X Windows client with SSH X11 tunneling to allow for the implementation of a UNIX or Linux GUI subsystem run over SSH securely on the same Windows machine host that is the source for the SSH session to the Linux or UNIX remote host:
ssh -ND 8000 fsmythe@example.com
Browser Settings, goto 'Manual Proxy Configuration' set "SOCKS Host" to example.com,
the 'Port to 8000' , Enable SOCKS v5, and lastly set 'No Proxy for' field
to 'localhost, 127.0.0.1'

  • Securely mounting a directory on a remote server as a file system on a local computer using sshfs:
# yum install sshfs fuse-utils (Install sshfs and fuse-utils)
$sshfs example.com:/remote_dir /mnt/local_dir

  • Automated remote host monitoring and management of servers through one or more mechanism:
(Report number of apache processes running on the remote server example.com):
$ ssh example.com ps -ef | grep httpd | wc -l
root@example.com's password: *****

No comments:

Post a Comment