温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.ms8.com/run-remote-commands-with-ssh-and-certificate-authentication-on-linux-server
点击访问原文链接

Run Remote Commands with SSH and Certificate Authentication on Linux / macOS Terminal - Making Sense of the Infinite

Run Remote Commands with SSH and Certificate Authentication on Linux / macOS Terminal - Making Sense of the Infinite

Making Sense of the Infinite

Unlocking Infinite Possibilities Through Curiosity

January 28, 2025 Run Remote Commands with SSH and Certificate Authentication on Linux / macOS Terminal

1. Ensure SSH Key-Based Authentication Is Set Up Before proceeding, make sure you have SSH key-based authentication set up between your local machine and the remote server:

Generate SSH Key Pair (if not already done): On your local machine, generate a new SSH key pair (private and public key): ssh-keygen -t rsa -b 4096 -C "[email protected]"ShellScript By default, this creates the keys in ~/.ssh/id_rsa (private key) and ~/.ssh/id_rsa.pub (public key).

2. Copy the Public Key to the Remote Server: Copy the public key (id_rsa.pub) to the remote server’s ~/.ssh/authorized_keys file. You can use ssh-copy-id:

ssh-copy-id -i ~/.ssh/id_rsa.pub user@remote_hostShellScript Or, manually append the public key to the ~/.ssh/authorized_keys file on the remote server.

2. SSH Command to Execute Script Remotely Once SSH key-based authentication is set up, you can log into the server and execute a script remotely by using the following script:

Make sure to replace username, IP Address, and other placeholders with your actual values.

To save the script as a local_execute.sh file locally:

"$LOG_FILE" 2>&1 # Check the exit status if [ $? -eq 0 ]; then echo "Script executed successfully. Exiting..." else echo "Script execution failed. Check the log file for details: $LOG_FILE" fi # Terminate the terminal session echo "Closing terminal session..." exit " style="color:#F8F8F2;display:none" aria-label="Copy" class="code-block-pro-copy-button">#!/bin/bash # Configuration PRIVATE_KEY="/path/to/private_key" # Path to the private key REMOTE_USER="username" # Remote host username REMOTE_HOST="remote_host_or_ip" # Remote host address or IP REMOTE_PORT="22" # SSH port (default is 22, change if necessary) REMOTE_SCRIPT="/path/to/remote_script.sh" # Path to the remote script LOG_FILE="/tmp/remote_execution.log" # Path to the local log file # Connect to the remote host and execute the script echo "Connecting to the remote host on port $REMOTE_PORT and executing the script..." ssh -i "$PRIVATE_KEY" -p "$REMOTE_PORT" "$REMOTE_USER@$REMOTE_HOST" "bash $REMOTE_SCRIPT" > "$LOG_FILE" 2>&1 # Check the exit status if [ $? -eq 0 ]; then echo "Script executed successfully. Exiting..." else echo "Script execution failed. Check the log file for details: $LOG_FILE" fi # Terminate the terminal session echo "Closing terminal session..." exit Bash Make the script executable: After saving the file, you need to give it executable permissions. In the terminal, run:

chmod +x ./local_execute.shShellScript Run the script: To execute the script locally, you can run:

./local_execute.shShellScript This will execute the script on your local machine, which will connect to the remote server via SSH using the provided private key and run the specified script.

Related Posts Using LFTP and Dynamic Variables to Backup Files to a Remote FTP Server Using cURL to Upload and Download Files via the SFTP Protocol Different Use Cases of Bash and EXP (Expect) How to Set Up a Proxy Server Using the Terminal Avoid Accidentally Executing rm -rf: A Comprehensive Guide Linux Code Private Key Certificate File Key macOS SSH Remote Commands Shell Script Bash Terminal

Last revised on

January 28, 2025 ←Optimized MySQL Configuration for High Performance Using the RouterOS API to Bulk Reboot MikroTik Router Devices Remotely→ Comments Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comment *

Name *

Website

Δ

More posts Model Context Protocol February 26, 2026 Faraday Future: A Persistent Scam December 9, 2025 Afeela: What Brought Honda and Sony Together? December 8, 2025 Loop Quantum Gravity, LQG November 15, 2025 Search

Tags:

Ad-Blocking Administrator Privileges Algorithm Application APT-Get Install Artificial Intelligence Artificial Intelligence Generated Content Bash Certificate File Cloudflare Code Command Line Concept Cryptocurrency Decentralization Developer Digital Certificate DNS over HTTPS DNS Resolver Domain Name Resolution Domain Name System Economic Encrypt Finance Firmware Formula Google Hardware Homebrew Home Lab Home Network Hypertext Transfer Protocol Secure Internet Investment iOS IPv6 Linux Machine Learning macOS Mathematics Microsoft Windows MikroTik Network Network Attached Storage Network File System Networking Network Management Network Security Network Service Network Switch Nginx NVIDIA Open Source Operating System Opinion Optimization Paradox Philosophy Physics Popular Science PowerShell Prediction Privacy Programming Language Proxy Server Python Quantum Computing Redundant Array of Independent Disks ROS Route Router RouterOS Routing Science Explained Secure Sockets Layer Security Shell Script Small Office Home Office Software SSH System Administration System Management Technology Terminal Theory Ubuntu Universe Unlocking Virtual eXtensible Local Area Network Virtualization Virtual Local Area Network Virtual Private Network VXLAN Web Web Server Wi-Fi WinBox Windows 11 Windows Server WireGuard

Making Sense of the Infinite

Proudly powered by WordPress

智能索引记录