SSH has many cool features. How can it be your daily work partner? I think you need to know the following 16 tips for using SSH efficiently, they help you save much more time than you need to configure them.
1. Multiple connections are shared.
If you need to open a connection to the same server in multiple windows, instead of entering the user name and password each time, or waiting for the connection to be established, you can configure the SSH connection sharing option, open your SSH configuration file locally. Usually they are located in ~ /. Ssh/config, and then add the following two lines:
ControlMaster autoControlPath /tmp/ssh_mux_%h_%p_%r
Now try to disconnect your connection from the server, create a new connection, open a new window, and create a new connection. You will find that, the second connection was established almost instantly.
Windows users
If you are a Windows user, unfortunately, the most popular open-source SSH client Putty does not support this feature, but OpenSSH implementation is also available in Windows, such as Copssh, if you think the following tips are helpful to you, maybe you should try Copssh.
File Transfer
Connection sharing not only helps you share multiple SSH connections. If you need to transfer files with the server through SFTP, you will find that they are still using the same connection, if you use Bash, you will find that you even support Automatic completion of server files by Using SSH or even tabs. shared connection options are applicable to tools that require SSH, such as rsync, git is also valid.
2. persistent connection
If you find that you need to connect to the same server for countless times, the persistent connection option is prepared for you:
ControlPersist 4h
Now, every time you establish a connection with the server through SSH, this connection will be retained for four hours. Even after you exit the server, this connection can still be reused. Therefore, when you log on to the server next time (within 4 hours), you will find that the connection is established at lightning speed. This option is especially helpful for copying multiple files through scp, because you do not need to perform separate authentication for each file.
3. Do not enter the password again.
If you are still logging on to SSH using a password, try SSH Keys. First, use OpenSSH to claim a key pair for yourself:
$ ssh-keygen
Follow the instructions and you should be able. there are two files in the ssh directory. id_rsa is your private key, while id_ras.pub is your public key. Now you need to copy your public key to the server, if your system has the ssh-copy-id command, the copy operation is simple:
$ ssh-copy-id smylers@compo.example.org
Otherwise, you need to manually copy your private key to ~ /. Ssh/authorized_keys file:
$ < ~/.ssh/id_rsa.pub ssh clegg.example.org ‘mkdir -p .ssh; cat >> .ssh/authorized_keys; chmod go-w .ssh .ssh/authorized_keys’
Now try to re-connect to the SSH server or copy the file. Do you no longer need to enter the password?
Configure an SSH Key for Putty
Putty can also use the SSH Key to download PuttyGen and Pageant from the Putty website, then use PuttyGen to generate your Key and copy the public Key to the server '. ssh/authorized_keys 'directory, run Pageant, import your private key, and run it in the background. You can use Putty to directly log on to the server through the public key, you can learn more about this feature in Chapter 8 and 9 of the Putty manual.
4. Connection Transfer
Sometimes you may need to connect to another server from one server, for example, directly transferring data between two servers, instead of using a local computer:
www1 $ scp -pr templates www2:$PWD
(By The Way, $ PWD is useful when you need to copy files between two servers ), because even if you have added the public key of your local computer to two servers, scp will still prompt you to enter the password by default: this is because the server you use as the stepping stone does not have your private key, so the second-born server will reject your public key, but do not copy your private key to the Transit server to solve this problem. You can use agent forwarding to solve this problem, as long. add the following line of code to the ssh/config file:
ForwardAgent yes
Or you can check the "Allow agent forwarding" option in Putty. Now your local SSH becomes the SSH proxy of the first server, connecting the first server to other servers is as simple as connecting to your local server. Note that if you want to enable this option, the intermediate server is worthy of your trust.
5. Omit the Host Name
It is boring to enter the complete host name of the server to create a new SSH connection, especially when you have a group of servers with the same domain name but different sub-domain names that need to be managed, for example:
* www1.example.com* www2.example.com* mail.example.com* intranet.internal.example.com* backup.internal.example.com* dev.internal.example.com
You may have configured your network to directly use short domain names, such as intranet. However, if your network does not support it, you can solve the problem by yourself instead of asking the network administrator for help.
The solution varies slightly depending on the operating system you use. The following is the configuration of my Ubuntu system:
prepend domain-search “internal.example.com”, “example.com”;
Then you need to restart the network: $ sudo restart network-manager
These two commands may be slightly different for different systems.
5. Host alias
You can also define the host alias in your SSH configuration, as shown below:
Host devHostName dev.internal.example.com
You can also use wildcards for grouping:
Host dev intranet backupHostName %h.internal.example.comHost www* mailHostName %h.example.com
In Putty, you can save a separate session for each host name, and double-click to establish a connection (but it may not support wildcards ).
7. Save the user name
If your user name on the remote server is different from your local user name, you can also set it in SSH Configuration:
Host www* mailHostName %h.example.comUser simon
Now, even if my local user name is smylers, I can still connect to my server like this:
$ ssh www2
SSH will use the simon account to connect to your server. Similarly, Putty can save this information in your session.
8. jump between servers
In some cases, you may not be able to directly connect to a server, but you need to use an intermediate server for transit. This process can also be automated. First, make sure that you have configured public key access for the server and enabled agent forwarding. Now you can use two commands to connect to the target server without any prompt:
$ ssh gatewaygateway $ ssh db
Then add the following configuration in your local SSH Configuration:
Host dbHostName db.internal.example.comProxyCommand ssh gateway netcat -q 600 %h %p
Now you can use a command to directly connect to the target server:
$ ssh db
Here you may need to wait for a long time, because SSH requires two authentication. Note that netcat may also be written as nc or ncat, or g must be added before, you need to check your intermediate server to determine the actual parameters.