Tuesday, August 14, 2012

SSH useful commands

I post below some useful SSH commands

Secure Copy File Transfer
SCP copies files between hosts on a network. It uses ssh for data transfer, and uses the same authentication and provides the same security as ssh. Some examples here

From a remote host to  local host
scp -P10022 /home/stelios/Desktop/                                            100% 137KB 136.6KB/s  00:00   

More that one file from localhost to remote host
scp -P10022 test01.txt test02.txt  
 test01.txt                                                 100%  0   0.0KB/s  00:00    
 test02.txt                                                 100%  0   0.0KB/s  00:00    

From the local host to a remote host
scp -P10022 /home/stelios/Desktop/                                               100%  75MB  1.5MB/s  00:49    

Secure Copy  directory from local to remote host
scp -P10022 -r /home/stelios/Desktop/scp_test  
 WHowe_gns3_vbox.pdf                                       100% 1902KB  1.9MB/s  00:00    

Run commands remotely
ssh -p 10022 uptime  
  21:29:46 up 2:25, 2 users, load average: 0.21, 0.13, 0.30  
ssh -p 10022 'ps aux | echo $HOSTNAME'  
ssh -p 10022 uname -a  
 Linux desktop 3.2.0-29-generic-pae #46-Ubuntu SMP Fri Jul 27 17:25:43 UTC 2012 i686 athlon i386 GNU/Linux  

if command needs terminal then use -t option to force ssh to allocate pseudo-tty, like :
ssh -p 10022 top  
 TERM environment variable not set.
ssh -p 10022 -t top  
 top - 08:47:40 up 8 min, 2 users, load average: 0.01, 0.47, 0.42  
 Tasks: 161 total,  1 running, 160 sleeping,  0 stopped,  0 zombie  
 Cpu(s): 0.0%us, 0.1%sy, 0.0%ni, 99.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st  
 Mem:  1931440k total,  729236k used, 1202204k free,  60528k buffers  
 Swap: 1963004k total,    0k used, 1963004k free,  394952k cached  
  PID USER   PR NI VIRT RES SHR S %CPU %MEM  TIME+ COMMAND                                 
  2565 stelios  20  0 2836 1156 860 R  0 0.1  0:00.05 top                                   
   1 root   20  0 3652 1968 1248 S  0 0.1  0:00.81 init                                   
   2 root   20  0   0  0  0 S  0 0.0  0:00.00 kthreadd        

SSHFS - Mouting partitions over SSH
Mount a partition located on a remote machine to your local machine.

For this you need to install sshfs. 
sudo apt-get install sshfs  

Then run :
sshfs -p 30022 /home/stelios/temp/  


Use the .ssh/config to enter convenient aliases for the remote hosts

 cat .ssh/config  
 Host remote-desktop  
  HostName myremotemachine  
  Port 10022  
  User stelios  
 Host remote-fileserver  
  HostName myremotemachine  
  Port 30022  
  user stelios  
ssh remote-desktop 'cat /etc/lsb-release'  
ssh remote-fileserver 'cat /etc/lsb-release'  

No comments: