25 SSH commands that must be remembered in Linux
1. Copy the SSH key to the target host and enable password-less SSH login.
ssh-copy-iduser@host
If no key exists, use the ssh-keygen command to generate it.
2. Open a tunnel from port 80 of a host to port 2001 of the local host
ssh-N-L2001:localhost:80somemachine
Now you can enter http: // localhost: 2001 in your browser to access this website.
3. Output your microphone to a remote computer speaker
ddif=/dev/dsp|ssh-carcfour-Cusername@hostddof=/dev/dsp
In this way, the sound from your microphone port will be output on the speaker port of the SSH target computer, but unfortunately, the sound quality is poor and you will hear a lot of hissing.
4. Compare remote and local files
sshuser@hostcat/path/to/remotefile|diff/path/to/localfile–
This command is useful when comparing the differences between local and remote files.
5. Mount the directory/file system through SSH
sshfsname@server:/path/to/folder/path/to/mount/point
Download sshfs from http://fuse.sourceforge.net/sshfs.htmlto allow you to upload a directory for cross-network security.
6. Establish an SSH connection through the intermediate host
ssh-treachable_hostsshunreachable_host
Unreachable_host indicates that the host cannot be directly accessed from the local network, but can be accessed from the network where reachable_host is located. This command creates a connection to unreachable_host by "hiding" the connection to reachable_host.
7. Copy your SSH public key to a remote host and enable password-less logon.
ssh-copy-idusername@hostname
8. directly connect to host A that can only be connected through host B
ssh-thostAsshhostB
Of course, you must be able to access host.
9. Create a persistent connection to the target host
ssh-MNf<user>@
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:
HosthostControlPath~/.ssh/master-%r@%h:%pControlMasterno
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.
10. Connect to the screen through SSH
ssh-tremote_hostscreen–r
Connect directly to a remote screen SESSION (saves useless parent bash processes ).
11. Port detection (knocking)
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-AINPUT-ieth0-s%IP%-ptcp–dport22-jACCEPTtcpflags=syn[closeSSH]sequence=5000,4000,3000seq_timeout=5command=/sbin/iptables-DINPUT-ieth0-s%IP%-ptcp–dport22-jACCEPTtcpflags=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
sshhost-luser$(<cmd.txt)
More portable versions:
sshhost-luser“`catcmd.txt`”
14. Copy the MySQL database to the new server through SSH
mysqldump–add-drop-table–extended-insert–force–log-error=error.log-uUSER-pPASSOLD_DB_NAME|ssh-Cuser@newhost“mysql-uUSER-pPASSNEW_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-i8d~/.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|sshuser@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-getinstallpv
If it is Fedora:
yuminstallpv
(Additional Software repositories may need to be enabled ).
18. If you create a remote GNU screen that can be reconnected
ssh-tuser@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