Home  >  Article  >  Operation and Maintenance  >  Tips for using SSH streamlined operation commands in Linux SysOps

Tips for using SSH streamlined operation commands in Linux SysOps

王林
王林Original
2023-09-27 11:51:34718browse

Linux SysOps中使用SSH精简操作命令的技巧

Tips of using SSH to streamline operation commands in Linux SysOps

Introduction:
In Linux system operation and maintenance work, SSH (Secure Shell) is a commonly used The remote login protocol allows you to remotely connect to the server via SSH and execute commands. However, using SSH can become tedious and inefficient when performing a large number of repetitive operations. This article will introduce some techniques for using SSH to streamline operation commands, and help SysOps improve work efficiency through specific code examples.

1. Use SSH configuration file
The SSH configuration file is located in the ~/.ssh directory under the user's home directory. Using the configuration file can easily save multiple SSH connection configurations and improve connection speed and security. . The following is an example of a simple SSH configuration file:

Host server1
    Hostname 192.168.1.100
    User myuser
    Port 22
    IdentityFile ~/.ssh/id_rsa

Host server2
    Hostname 192.168.1.101
    User myuser
    Port 22
    IdentityFile ~/.ssh/id_rsa

With such a configuration file, we can use the following command to connect to the server specified in the configuration file:

ssh server1

2. Use SSH key pair
In order to avoid having to enter a password for each SSH connection, we can use an SSH key pair to complete passwordless login. Here are the steps to use a key pair:

  1. Generate a key pair:
    ssh-keygen -t rsa
  2. Copy the public key to the server Up:
    ssh-copy-id server
  3. Test passwordless login:
    ssh server

3. Using the SSH command spring machine
In some cases, we need to connect to the target server through the spring machine. The SSH command can achieve this function through the ProxyJump parameter. Here is an example:

ssh -J jumpuser@jumpserver destinationuser@destinationserver

This allows you to connect directly to the destination server locally, skipping the jump server.

4. Use SSH to execute commands in batches
When we need to execute the same command on multiple servers, we can use SSH to execute commands in batches without logging in to each server one by one. Here is an example:

for server in $(cat servers.txt); do ssh $server "command"; done

This command will be retrieved from servers.txt Read the server IP line by line in the file, then connect to each server and execute the specified command.

5. Using SSH pipes and remote port forwarding
In some cases, we may need to establish an encrypted pipe between the local and remote servers through SSH, or perform remote port forwarding. The following is an example:

ssh -L localport:localhost:remoteport server

This command will bind the local port localport to the remote server port remoteport. You can directly access the remote server's services locally.

Conclusion:
By using SSH configuration files, key pairs, springboards, batch execution of commands, pipelines and remote port forwarding, we can streamline operating commands and improve efficiency in Linux SysOps work . I hope the above tips will be helpful to SysOps workers.

Author: Intelligent Assistant
Date: October 20, 2021

The above is the detailed content of Tips for using SSH streamlined operation commands in Linux SysOps. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn