Home >Operation and Maintenance >Linux Operation and Maintenance >Best way to manage ssh hosts and private keys
As a system administrator, handle multiple remote systems on a regular basis. Need to use ssh system many times during work. Many remote Linux servers are accessed via passwords, many via private keys. So that's more typical for managing all of this stuff. This article will introduce you to the details about using key files to correctly organize the ssh server.
Configuration file syntax:
We can add multiple ssh host details to the ~/.ssh/config file . Edit the configuration file in your favorite editor such as vi, vim or nano.
$ vi~/.ssh/config
The syntax is as follows:
Host<NICK_NAME> HostName<IP地址远程> IdentityFile <PATH TO私有文件> User<LOGIN AS USERNAME> Port<SSH要使用的端口> LocalForward <本地端口> <REMOTE_LOCATION:PORT>
1. Add the first SSH host
For example, our first ssh host is running A php development web server with details name php-web1, user root, port 22, and accessible via password. Add the following content in the configuration file.
Host php-web1 HostName 192.168.1.100 User root
Now try using SSH as the following command.
$ ssh php-web1
2. Add a second SSH host
Our second host server (php-web2) can use ssh with user root on the default port 22 Key pair access. Add the following content in the configuration file.
Host php-web2 HostName 192.168.1.101 IdentityFile ~/.ssh/php-web2.pem User root
Now try using SSH as the following command.
$ ssh php-web2
3. Add a third SSH host
Our third ssh host server (php-db1) is running on port 2222 and can be accessed through user ubuntu key pair access. Add the following content in the configuration file.
Host php-db1 HostName 192.168.1.110 Port 2222 IdentityFile ~/.ssh/php-db1.pem User ubuntu
Now try using SSH as the following command.
$ ssh php-db1
4. Set up forwarding using SSH
In this setup we need to forward the local system port 3306 to the remote server (php-db1) on port 3306 ) host. Add the following content in the configuration file.
Host php-db1-mysql-tunnel HostName 192.168.1.110 Port 2222 IdentityFile ~/.ssh/php-db1.pem LocalForward 3306 127.0.0.1:3306
Now try using SSH as the following command.
$ ssh php-db1-mysql-tunnel
Final configuration file
The final configuration file ~/.ssh/config is as follows.
Host php-web1 HostName 192.168.1.100 User root Host php-web2 HostName 192.168.1.101 IdentityFile ~/.ssh/php-web2.pem User root Host php-db1 HostName 192.168.1.110 Port 2222 IdentityFile ~/.ssh/php-db1.pem User ubuntu Host php-db1-mysql-tunnel HostName 192.168.1.110 Port 2222 IdentityFile ~/.ssh/php-db1.pem LocalForward 3306 127.0.0.1:3306
The above is the detailed content of Best way to manage ssh hosts and private keys. For more information, please follow other related articles on the PHP Chinese website!