Run Remote Commands with SSH and Certificate Authentication on Linux / macOS Terminal - 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 TerminalLast 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 replyYour email address will not be published. Required fields are marked *
Comment *
Email *
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 SearchTags:
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 InfiniteProudly powered by WordPress
智能索引记录
-
2026-02-27 19:17:49
综合导航
成功
标题:7-Eleven Releases Chips Ahoy Hot Chocolate
简介:7-Eleven Inc. has once again tapped the name recognition of
-
2026-02-27 19:42:30
综合导航
成功
标题:Aktuelle Stellenangebote der Software-Manufaktur TUP
简介:Du möchtest Dich in einem spannenden Tätigkeitsfeld nach dem
-
2026-02-27 15:04:35
综合导航
成功
标题:Specialized Health Services Products - PR.com
简介:View products from Specialized Health Services businesses.
-
2026-02-27 18:21:19
综合导航
成功
标题:Contact Chris Hakim - PR.com
简介:Contact Chris Hakim via this online contact form.
-
2026-02-27 17:58:53
游戏
成功
标题:2wi游戏-好玩的网页传奇-传奇类网页游戏-新开网页版传奇排名-超变网页传奇 - 2维网
简介:2wi游戏网专业运营众多精品网页游戏和好玩的网页传奇,最新超变网页传奇游戏齐聚一堂,免费领取传奇游戏好礼,更多2020好
-
2026-02-27 13:33:58
综合导航
成功
标题:Chang Wa Shan v Esther Chan Pui Kwan - 5RB Barristers
简介:Chang Wa Shan v Esther Chan Pui Kwan -
-
2026-02-27 17:29:10
教育培训
成功
标题:考研网上确认可以用临时身份证吗?在有效期内可以-高顿教育
简介:考研网上确认可以使用临时身份证。只要临时身份证在有效期内,就可以用于考研报名和现场确认。网上确认一般要准备三个月以内的证
-
2026-02-27 16:05:19
综合导航
成功
标题:Cartuchos filtrantes para filtros de óleo hidráulico - VTE-FILTER GmbH
简介:Os cartuchos filtrantes para filtros de óleo hidráulico da V
-
2026-02-27 20:07:11
综合导航
成功
标题:Abbas Ali Amir
简介:1x.com is the world
-
2026-02-27 13:20:27
综合导航
成功
标题:Mou Fine Knit Scarf Mou Official Website
简介:The Mou Fine Knit Scarf is handcrafted using a specialist op
-
2026-02-27 20:03:31
综合导航
成功
标题:Fans Calgary Flames Calgary Flames
简介:The official National Hockey League website including news,
-
2026-02-27 17:38:47
游戏娱乐
成功
标题:游戏图片_纵览出彩的游戏世界_3DM图片
简介:游戏世界给我们带来了一幅又一幅精彩的画面,3DM游戏图片频道将这些出彩的画面进行收集整理,让每一位来3DM的玩家都能够找
-
2026-02-27 18:02:38
综合导航
成功
标题:au
简介:auのオフィシャルサイト。個人のお客さま向けサービスをご紹介します。
-
2026-02-27 18:22:39
综合导航
成功
标题:Results Contest : Wind
简介:1x.com is the world
-
2026-02-27 19:40:51
综合导航
成功
标题:Ariana's Graduation Day - Play The Free Game Online
简介:Ariana
-
2026-02-27 18:01:46
综合导航
成功
标题:Men’s & Women’s Jeans, Clothes & Accessories American Eagle
简介:Shop American Eagle men
-
2026-02-27 14:18:39
综合导航
成功
标题:Wellington Management CEO Jean Hynes: Hard Lessons Morgan Stanley
简介:Watch Wellington Management CEO Jean Hynes and Morgan Stanle
-
2026-02-27 17:33:24
游戏娱乐
成功
标题:602游戏平台 - 做玩家喜爱、信任的游戏平台!
简介:602游戏平台(www.602.com)专注精品网页游戏,以精细化运营和优质服务为核心,秉持
-
2026-02-27 15:55:52
教育培训
成功
标题:湖北大学考研有面试吗?复试一般有-高顿教育
简介:湖北大学考研复试一般有面试,其2024考研复试方案还未公布,所以小编整理了湖北大学2023年硕士研究生复试录取工作办法的
-
2026-02-27 19:50:47
综合导航
成功
标题:Shipborne Aircraft Combat Simulator - Play The Free Mobile Game Online
简介:Shipborne Aircraft Combat Simulator - click to play online.
-
2026-02-27 19:30:21
综合导航
成功
标题:SEC Requests Industry Comments on Fund Names Publications Insights Faegre Drinker Biddle & Reath LLP
简介:The SEC identified several fund developments and challenges
-
2026-02-27 12:28:45
综合导航
成功
标题:3522浦京集团_官方网站入口与注册登录指南
简介:点击次数 →25813访问3522浦京集团官方网站,获取正规登录入口与注册通道,了解平台动态与使用安全提示。
-
2026-02-27 17:51:55
综合导航
成功
标题:VRV IV-S RXTQ - HTS Commercial & Industrial HVAC Systems, Parts, & Services Company
简介:Daikin’s VRV IV-S Series is available in smaller 3, 4, and 5
-
2026-02-27 19:58:00
综合导航
成功
标题:JS Bank Sponsors 34th CAS Open Golf Championship – 4th March, 2015 JSCLJSCL
简介:JS Bank limited (JSBL) has sponsored 34th CAS Open Golf Cham
-
2026-02-27 17:56:02
新闻资讯
成功
标题:602《女神联盟》117服3月2日11点火爆开启 - 新闻公告 - 602游戏平台 - 做玩家喜爱、信任的游戏平台!cccS
简介:602《女神联盟》117服3月2日11点火爆开启
-
2026-02-27 14:30:46
综合导航
成功
标题:Matchers.toBeNumber method bun:test module Bun
简介:API documentation for method bun:test.Matchers.toBeNumber
-
2026-02-27 16:07:41
视频影音
成功
标题:教官的失忆迷糊妻第20集红豆剧场_在线播放[高清流畅]_爽文短剧
简介:爽文短剧_教官的失忆迷糊妻剧情介绍:教官的失忆迷糊妻是由内详执导,内详等人主演的,于2025年上映,该剧情讲述的是@好男
-
2026-02-27 13:06:33
综合导航
成功
标题:جامعة كوانتلين بوليتكنيك: اكتشف شركاء مسار جامعة ILSC
简介:ILSC’s University Pathway Program prepares international stu
-
2026-02-27 15:36:58
综合导航
成功
标题:Specialty Building Products Homepage United States
简介:Located in Georgia, SBP is the leading distributor of specia
-
2026-02-27 18:16:25
综合导航
成功
标题:Industrie pétrolière et gazière - VTE-FILTER GmbH
简介:Découvrez comment les solutions de filtration sûres et avanc