cmdref.net - Cheat Sheet and Example

cmdref.net is command references/cheat sheets/examples for system engineers.

User Tools

Site Tools


Sidebar








Cloud



Etc


Reference














.

os:linux:command:ssh.html



Linux Commands#network

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

'-n' is very important for many tasks.

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"
ssh -o "StrictHostKeyChecking no" xx.xx.xx.xx

Example

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


If you use sudo

ssh -n USER@IP sudo COMMAND

Example

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

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

ssh -n $i  sudo "bash -c 'sed -i \"s/^rotate 14/rotate 30/\" /etc/logrotate.conf'"

Process

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

Network

ssh -n 192.168.0.10  sudo "bash -c 'echo "\\# Comment" >> /etc/hosts' "
ssh -n 192.168.0.10  sudo "bash -c 'echo "x.x.x.x   test-server1" >> /etc/hosts' "
ssh -n 192.168.0.10  sudo "bash -c 'sed s/192.168.100.10/192.168.50.10/g /etc/hosts > /etc/hosts.20121111' "
ssh -n 192.168.0.10  sudo "bash -c 'sed -i \"s/192.168.100.10/192.168.50.10/\" /etc/hosts'"

ssh -n IP_ADDRESS sudo /sbin/route add -net 192.168.0.0 netmask 255.255.255.0 gw 192.168.0.1

ssh -n IP_ADDRESS sudo "cp -p /etc/sysconfig/static-routes /etc/sysconfig/static-routes.`date '+%Y%m%d'`"
ssh -n IP_ADDRESS sudo "bash -c 'echo \"any host 192.168.100.1 gw 192.168.0.5\" >> /etc/sysconfig/static-routes'"
ssh -n 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 -n 192.168.0.10  sudo "bash -c 'sed -i \"/192.168.20.0/i any net 192.168.10.0 netmask 255.255.0.0 gw 192.168.0.5\" /etc/sysconfig/static-routes'"

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

Installing rpm

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

hostname

H=test-server1
ssh -n xx.xx.xx.xx sudo hostname $H
ssh -n xx.xx.xx.xx sudo "cp -p /etc/sysconfig/network /etc/sysconfig/network.`date '+%Y%m%d'`"
ssh -n 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 -n xx.xx.xx.xx cat /etc/sysconfig/network

User

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

Package

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

Compileing Software

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

Kernel Parameter

ssh -n xxxxxx cat /etc/sysctl.conf
ssh -n xxxxxx sudo "cp -p /etc/sysctl.conf /etc/sysctl.conf.`date '+%Y%m%d'`"
ssh -n 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 -n xxxxxx cat /etc/sysctl.conf
ssh -n xxxxxx sudo "bash -c 'sysctl -p |grep net.core |grep mem_max'"

Password

ssh -n xx.xx.xx.xx sudo "bash -c 'echo user:xxxxxx | chpasswd'"
ssh -n 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
for i in $H ; do ssh -n $i  sudo "bash -c 'sed -i \"s/^rotate 14/rotate 30/\" /etc/logrotate.conf'"  ; 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 -n $s service httpd graceful; done

Example : How to change routing.

H="web1 web2 web3"

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

for i in $H ; do ssh -n $i "uname -n ; netstat -rn > /tmp/route.20170421"  ; done
for i in $H ; do ssh -n $i "uname -n ; netstat -rn |grep 10.50."  ; done
for i in $H ; do ssh -n $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 -n $i "uname -n ; netstat -rn |grep 10.50."  ; done
for i in $H ; do ssh -n $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 -n $i "uname -n ; netstat -rn |grep 10.50."  ; done
for i in $H ; do ssh -n $i "uname -n ; netstat -rn > /tmp/route.20170422"  ; done
for i in $H ; do ssh -n $i "uname -n ; diff  /tmp/route.20170421  /tmp/route.20170422"  ; done

for i in $H ; do ssh -n $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


Using SSH to remotely start a process

command - Using SSH to remotely start a process - Server Fault

ssh user@host "/script/to/run < /dev/null > /tmp/mylogfile 2>&1 &"
SSH connects stdin, stdout and stderr of the remote shell to your local terminal, so you can interact with the command that's running on the remote side.
As a side effect, it will keep running until these connections have been closed, which happens only when the remote command and all its children (!) have >terminated (because the children, which is what “&” starts, inherit std* from their parent process and keep it open).
So you need to use something like
ssh user@host "/script/to/run > /tmp/ssh.stdout 2>&1 && cat /tmp/ssh.stdout && rm -f /tmp/ssh.stdout"






Linux Commands#network




os/linux/command/ssh.html.txt · Last modified: 2022/01/24 by admin

Page Tools