Create a persistent connection to the target host in the background and send this command to you ~ The configuration in/. Ssh/config is used in combination:
All SSH connections to the target host will use persistent SSH sockets. If you use SSH to regularly synchronize files (using rsync/SFTP/CVS/SVN), this command will be very useful, because no new socket is created each time an SSH connection is opened.
Connect directly to a remote screen SESSION (saves useless parent bash processes ).
knock
To open a service port (such as SSH) on a port, and then close the port, you need to install knockd first. The following is a configuration file example.
[options]logfile = /var/log/knockd.log[openSSH]sequence = 3000,4000,5000seq_timeout = 5command = /sbin/iptables -A INPUT -i eth0 -s %IP% -p tcp –dport 22 -j ACCEPTtcpflags = syn[closeSSH]sequence = 5000,4000,3000seq_timeout = 5command = /sbin/iptables -D INPUT -i eth0 -s %IP% -p tcp –dport 22 -j ACCEPTtcpflags = syn
12. delete a line of content in a text file and fix it effectively.
ssh-keygen -R <the_offending_host>
In this case, it is best to use professional tools.
13. Run complex remote shell commands through SSH
ssh host -l user $(<cmd.txt)
More portable versions:
ssh host -l user “`cat cmd.txt`”
14. Copy the MySQL database to the new server through SSH
mysqldump –add-drop-table –extended-insert –force –log-error=error.log -uUSER -pPASS OLD_DB_NAME | ssh -C user@newhost “mysql -uUSER -pPASS NEW_DB_NAME”
Dump a MySQL database through the compressed SSH tunnel and pass it as an input to the MySQL command. I think this is the fastest and best way to migrate the database to the new server.
15. delete a row in the text file and fix the "SSH host key change" Warning.
sed -i 8d ~/.ssh/known_hosts
16. Copy your SSH public key from a host without SSH-COPY-ID commands to the server
cat ~/.ssh/id_rsa.pub | ssh user@machine “mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys”
If you use Mac OS X or other * nix variants without the ssh-copy-id command, this command can copy your public key to a remote host, therefore, you can achieve SSH login without a password.
17. Real-time SSH network throughput Test
yes | pv | ssh $host “cat > /dev/null”
Connect to the host through SSH to display the real-time transmission speed. Point all transmitted data to/dev/null and install PV first.
For Debian:
apt-get install pv
If it is fedora:
yum install pv
(Additional Software repositories may need to be enabled ).
18. If you create a remote GNU screen that can be reconnected
ssh -t user@some.domain.com /usr/bin/screen –xRR
People always like to open many shells in a text terminal. If the session is suddenly interrupted or you press Ctrl-a D, the shell on the remote host will not be affected at all, you can reconnect. Other Useful screen Commands include "Ctrl-a c" (open a new shell) and "Ctrl-A" (switch back and forth between shells ), please visit http://aperiodic.net/screen/quick_referenceto read more about the screencommand quick reference.
19. Continue SCP large files
rsync –partial –progress –rsh=ssh $file_source $user@$host:$destination_file
It can restore failed rsync commands. This command is very useful when you transmit large files through VPN, such as a backup database. You need to install rsync on both hosts.
rsync –partial –progress –rsh=ssh $file_source $user@$host:$destination_file local -> remote
Or
rsync –partial –progress –rsh=ssh $user@$host:$remote_file $destination_file remote -> local
20. analyze traffic through SSH w/Wireshark
ssh root@server.com ‘tshark -f “port !22″ -w -' | wireshark -k -i –
Use tshark to capture network communication on the remote host, send the original pcap data through an SSH connection, and display it in Wireshark. Press Ctrl + C to stop capturing, but also close the wireshark window, you can pass a "-C #" parameter to tshark so that it can only capture the data packet type specified by "#", or redirect data through the named pipe, instead of directly transmitting data to Wireshark through SSH, I suggest you filter data packets to save bandwidth. tshark can be replaced by tcpdump:
ssh root@example.com tcpdump -w – ‘port !22′ | wireshark -k -i –
21. Keep the SSH session open permanently
autossh -M50000 -t server.example.com ‘screen -raAd mysession’
After opening an SSH session, keep it permanently open. If you need to switch between Wi-Fi hotspots for laptop users, you can ensure that the connection will not be lost after the switch.
22. More stable, faster, and stronger SSH client
ssh -4 -C -c blowfish-cbc
Use IPv4 to compress data streams and use blowfish for encryption.
23. Use cstream to control bandwidth
tar -cj /backup | cstream -t 777k | ssh host ‘tar -xj -C /backup’
Use bzip to compress the folder and transmit it to the remote host at 777 kb/s. Cstream has more features. Visit http://www.cons.org/cracauer/cstream.html#usageto learn more, for example:
echo w00t, i’m 733+ | cstream -b1 -t2
24. Transmit the SSH public key to another machine in one step
ssh-keygen; ssh-copy-id user@host; ssh user@host
This command combination allows you to log on without a password for ssh. Note that ~ /. There is already an SSH key pair in the SSH directory. The new keys generated by the ssh-keygen command may overwrite them. The Ssh-copy-ID copies the keys to the remote host, and append it to the Remote Account ~ In the/. Ssh/authorized_keys file, if you do not use the key password during SSH connection, a remote shell will be displayed shortly after you call SSH user @ host.
25. Copy the standard input (stdin) to your X11 Buffer
ssh user@host cat /path/to/some/file | xclip
Do you want to use SCP to copy files to a work computer so that they can be copied to an email? Xclip can help you. It can copy the standard input to the X11 buffer. You need to click and paste the content in the buffer.
If you have other SSH command skills, post them in this article.
Original article: http://blog.urfix.com/25-ssh-commands-tricks/