25 SSH commands and skills

Source: Internet
Author: User
Tags pdf manual rsync ssh server

Address: http://wowubuntu.com/25-ssh-cmd.html

SSH is a great tool. If you want to remotely connect to the server on the internet, SSH is undoubtedly the best candidate. The following are the 25 best SSH commands selected by voting on the Internet.

1. Copy the SSH key to the target host and enable password-less SSH login.

Ssh-copy-ID user @ host

If no key exists, use the ssh-keygen command to generate it.

2. Enable the SSH tunnel from port 80 of a host to port 2001 of the local host

Ssh-n-l2001: localhost: 80 somemachine

Now you can enter http: // localhost: 2001 in your browser to access this website.

3. Output your microphone to a remote computer speaker

Dd If =/dev/DSP | ssh-C arcfour-C username @ host dd OF =/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

SSH user @ host CAT/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

Sshfs name @ 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-T reachable_host SSH unreachable_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 the files on the remote host 1 to the files on the remote host 2 through your computer

SSH root @ host1 "CD/somedir/tocopy/& tar-CF-." | SSH root @ host2 "CD/samedir/tocopyto/& tar-xf -"

If only you can access host1 and host 2 at the same time, but they cannot access your host (So ncat cannot work), and they cannot access them directly.

8. Run the GUI on any remote hostProgram

Ssh-Fx <user >@< host> <program>

The SSH server must meet the following requirements:

X11forwarding yes # ensure that X11 forwarding is enabled

You can also enable

Compression delayed

Of course, you must be able to access host.

9. Create a persistent connection to the target host

Ssh-MNF <user >@< host>

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:

Host host
Controlpath ~ /. Ssh/master-% R @ % H: % P
Controlmaster No

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. directly enable and restore the screen command through SSH

Ssh-T remote_host screen-R

Directly connect 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 = 5000
Seq_timeout = 5
Command =/sbin/iptables-A input-I eth0-S % IP %-P TCP-dport 22-J accept
Tcpflags = SYN
[Closessh]
Sequence = 3000
Seq_timeout = 5
Command =/sbin/iptables-D input-I eth0-S % IP %-P TCP-dport 22-J accept
Tcpflags = 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 login .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 777 k | 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. In addition, to learn more shell commands, please download the best Linux Shell Command pdf manual.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.