cmdref.net - Command Reference

This site is command references for system engineers.

User Tools

Site Tools


Top     SiteMap

Sidebar

Top     SiteMap








Etc


Reference











.

os:linux:command:ssh.html



Linux Commands#network
ssh configuration
SSH Portfoward
X11 Forwarding

ssh - How to use ssh command in Linux (With Examples)

SSH Options

-A  Enables forwarding of the authentication agent connection.

-i identity_file
    Selects a file from which the identity (private key) for RSA or DSA authentication is read.
    The default is ~/.ssh/identity for protocol version 1, and ~/.ssh/id_rsa and ~/.ssh/id_dsa for protocol version 2.

-n  Redirects stdin from /dev/null (actually, prevents reading from stdin).
    This must be used when ssh is run in the background.

-t  Force pseudo-tty allocation.  This can be used to execute arbitrary screen-based programs on a remote
    machine, which can be very useful, e.g., when implementing menu services.  Multiple -t options force tty
    allocation, even if ssh has no local tty


Remote login

ssh IP
ssh USER@IP
ssh -l USER IP (l = login name)
ssh -i KEY  USER@IP (i = identity = Key)


Command at remote host

If you don't use sudo

ssh USER@IP COMMAND
ssh -l USER IP COMMAND
ssh -n USER@IP COMMAND    <- "-n" is needed, if you do loop handling in your shell script.
su - USER -c "ssh USER@IP COMMAND"

Example

ssh 127.0.0.1 hostname
ssh -t 192.168.0.10 /usr/sbin/ntpq -p
ssh -t 192.168.0.10 cat /etc/hosts |grep localhost


If you use sudo

ssh -t USER@IP sudo COMMAND

Example

ssh -t 192.168.0.10 sudo /sbin/reboot &
ssh -t 192.168.0.10 uptime

ssh -t 192.168.0.10 sudo cat /etc/hosts |grep localhost
ssh -t 192.168.0.10 "sudo su - -c '/tmp/tmp.sh >> /tmp/2013052.txt && diff /tmp/20130501.txt /tmp/20130502.txt'"

Process

ssh -t 127.0.0.1 sudo /etc/init.d/httpd restart
ssh -t 192.168.0.10 "hostname;sudo su - -c '/etc/init.d/httpd stop;/etc/init.d/httpd status'"

Netwrok

ssh -t 192.168.0.10  sudo "bash -c 'sed s/192.168.100.10/192.168.50.10/g /etc/hosts > /etc/hosts.20121111' "
ssh -t IP_ADDRESS sudo /sbin/route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.0.1

ssh IP_ADDRESS sudo "cp -p /etc/sysconfig/static-routes /etc/sysconfig/static-routes.`date '+%Y%m%d'`"
ssh IP_ADDRESS sudo "bash -c 'echo \"any host 192.168.100.1 gw 192.168.0.5\" >> /etc/sysconfig/static-routes'"
ssh IP_ADDRESS sudo "bash -c 'echo \"any net 192.168.10.0 netmask 255.255.0.0 gw 192.168.0.5\" >> /etc/sysconfig/static-routes'"

ssh -t 192.168.0.10 "hostname; netstat -rn  |grep 10.110.0"

Installing rpm

ssh xx.xx.xx.xx sudo "bash -c 'ls /tmp/*.rpm | xargs ls -lh ; echo OK'"
ssh xx.xx.xx.xx sudo "bash -c 'ls /tmp/*.rpm | xargs rpm -ivh ; echo OK'"
ssh xx.xx.xx.xx  rpm -qa |grep XXX |wc -l

hostname

H=test-server1
ssh xx.xx.xx.xx sudo hostname $H
ssh xx.xx.xx.xx sudo "cp -p /etc/sysconfig/network /etc/sysconfig/network.`date '+%Y%m%d'`"
ssh xx.xx.xx.xx sudo "bash -c 'grep -v HOSTNAME /etc/sysconfig/network > /tmp/network ; cat /tmp/network > /etc/sysconfig/network ; echo HOSTNAME=$H >> /etc/sysconfig/network'"
ssh xx.xx.xx.xx cat /etc/sysconfig/network

User

ssh xx.xx.xx.xx sudo /usr/sbin/userdel -r test1
ssh xx.xx.xx.xx sudo "bash -c 'userdel -r test1; userdel -r test2; userdel -r test3'"

Package

ssh xx.xx.xx.xx  sudo yum -y install nc

Compileing Software

#Ruby
ssh xx.xx.xx.xx sudo "bash -c 'cd /tmp && xzf ruby-2.0.0-p647.tar.gz'"
ssh xx.xx.xx.xx sudo "bash -c 'cd /tmp/ruby-2.0.0-p647 && ./configure --prefix=/usr/local/ruby2.0.0-p647'"
ssh xx.xx.xx.xx sudo "bash -c 'cd /tmp/ruby-2.0.0-p647 && make;make install'"
ssh xx.xx.xx.xx sudo ln -s /usr/local/ruby2.0.0-p647/bin/* /usr/local/bin/

Kernel Parameter

ssh xxxxxx cat /etc/sysctl.conf
ssh xxxxxx sudo "cp -p /etc/sysctl.conf /etc/sysctl.conf.`date '+%Y%m%d'`"
ssh xxxxxx sudo "bash -c 'echo \"\" >> /etc/sysctl.conf ; echo \"net.core.rmem_max = 33554432\" >> /etc/sysctl.conf ; echo \"net.core.wmem_max = 33554432\" >> /etc/sysctl.conf'"
ssh xxxxxx cat /etc/sysctl.conf
ssh xxxxxx sudo "bash -c 'sysctl -p |grep net.core |grep mem_max'"

Password

ssh xx.xx.xx.xx sudo "bash -c 'echo \"root:pass123\" > pass.tmp ; chpasswd < pass.tmp ; rm -f pass.tmp'"


TIPS

To Many hosts

for i in 192.168.0.10 192.168.0.11 ; do ssh -n $i "hostname" ; done
for i in 192.168.0.10 192.168.0.11 ; do ssh -n $i "netstat -rn |grep 0.0.0.0" ; done
for i in 192.168.0.10 192.168.0.11 ; do ssh -n $i "hostname ; sudo su - -c '/etc/init.d/httpd stop ; /etc/init.d/httpd status'" ; done

H="192.168.0.1 192.168.0.2"
for i in $H ; do ssh -n $i "hostname" ; done
for i in $H ; do ssh -n $i "netstat -rn |grep 0.0.0.0" ; done
for i in $H ; do ssh -n $i "hostname ; sudo su - -c '/etc/init.d/ntpd restart ; /etc/init.d/ntpd status'" ; done

for s in $(cat servers.txt); do ssh $s service httpd graceful; done

Example : How to change routing.

H="web1 web2 web3"

for i in $H ; do ssh $i "hostname"  ; done
for i in $H ; do ssh $i "uname -n ; sudo cp -p /etc/sysconfig/static-routes /etc/sysconfig/static-routes.`date '+%Y%m%d'`"  ; done
for i in $H ; do ssh $i "uname -n ; ls -lh /etc/sysconfig/static-routes*"  ; done
for i in $H ; do scp $i.new $i:/tmp/  ; done
for i in $H ; do ssh $i "uname -n ; sudo cp -f /tmp/$i.new /etc/sysconfig/static-routes"  ; done
for i in $H ; do ssh $i "uname -n ; diff /etc/sysconfig/static-routes /etc/sysconfig/static-routes.`date '+%Y%m%d'`"  ; done
for i in $H ; do ssh $i "uname -n ; ls -lh /etc/sysconfig/static-routes*"  ; done

for i in $H ; do ssh $i "uname -n ; netstat -rn > /tmp/route.20170421"  ; done
for i in $H ; do ssh $i "uname -n ; netstat -rn |grep 10.50."  ; done
for i in $H ; do ssh $i "uname -n ; sudo /sbin/route add -net 10.50.0.0 netmask 255.255.0.0 gw 10.50.1.1"  ; done
for i in $H ; do ssh $i "uname -n ; netstat -rn |grep 10.50."  ; done
for i in $H ; do ssh $i "uname -n ; sudo /sbin/route del -net 10.50.5.0 netmask 255.255.255.0 gw 10.50.1.1"  ; done
for i in $H ; do ssh $i "uname -n ; netstat -rn |grep 10.50."  ; done
for i in $H ; do ssh $i "uname -n ; netstat -rn > /tmp/route.20170422"  ; done
for i in $H ; do ssh $i "uname -n ; diff  /tmp/route.20170421  /tmp/route.20170422"  ; done

for i in $H ; do ssh $i "uname -n ; ping -c 1 10.50.1.22 ; ping -c 1 10.50.2.1  ; ping -c 1 10.50.3.1" ; done

# cat /tmp/list.lst
192.168.10.5
192.168.10.6
192.168.10.7
#
for host in `cat /tmp/list.lst`
do
scp tmp.sh ${host}:/tmp
ssh -n ${host} "sudo su - -c '/tmp/tmp.sh >> /tmp/2013052.txt && diff /tmp/20130501.txt /tmp/20130502.txt'"
done

note

# man bash
(abbr)
command1 && command2
       command2 is executed if, and only if, command1 returns an exit status of zero.
(abbr)

$ echo aaa ; echo bbb ; echo ccc
aaa
bbb
ccc
$
$ echo aaa && echo bbb && echo ccc
aaa
bbb
ccc
$


$ ls aaa ; echo bbb
ls: aaa: No such file or directory
bbb
$
$ ls aaa && echo bbb
ls: aaa: No such file or directory
$                                          <--------not found bbb


diff between remote file and local file

ssh remotename cat /etc/hosts | diff /etc/hosts  - 


Agent forwarding

user01@mypc:~ $ ssh -A ladder-server
user01@ladder-server:~ $ ssh  target-server






Linux Commands#network




os/linux/command/ssh.html.txt ยท Last modified: 2017/12/09 by admin