linuxSshMcpServer

linuxSshMcpServer

Okay, here's a breakdown of how to establish an SSH connection and send shell commands or files to a target Linux server, along with examples and explanations: **1. Prerequisites:** * **SSH Client:** You need an SSH client installed on your local machine. Most Linux and macOS systems have one built-in (`ssh` in the terminal). For Windows, you can use PuTTY, OpenSSH (available in recent Windows versions), or other SSH clients. * **SSH Server:** The target Linux server must have an SSH server running (usually `sshd`). It's typically enabled by default on most Linux distributions. * **Credentials:** You need a valid username and password (or, preferably, an SSH key pair) for an account on the target server. Using SSH keys is much more secure than passwords. * **Network Connectivity:** Your local machine must be able to reach the target server over the network (e.g., the server's IP address or hostname must be resolvable). **2. Basic SSH Connection (Password Authentication):** Open your terminal or command prompt and use the following command: ```bash ssh username@server_address ``` * `username`: The username of the account on the target server. * `server_address`: The IP address or hostname of the target server. Example: ```bash ssh john.doe@192.168.1.100 ``` You'll be prompted for the password for the `john.doe` account. Type it in and press Enter. (Note: You won't see the password as you type it.) **3. SSH Connection with SSH Key Authentication (Recommended):** This is the more secure method. If you don't have an SSH key pair, you'll need to generate one first. * **Generate SSH Key Pair (if you don't have one):** ```bash ssh-keygen -t rsa -b 4096 ``` This command will: * Generate a new RSA key pair with a key size of 4096 bits (a good standard). * Prompt you for a file to save the key. The default (`~/.ssh/id_rsa`) is usually fine. * Prompt you for a passphrase. A passphrase adds an extra layer of security. You can leave it blank for no passphrase, but it's generally recommended to use one. * **Copy the Public Key to the Target Server:** There are several ways to do this. The easiest (if you have password access initially) is to use `ssh-copy-id`: ```bash ssh-copy-id username@server_address ``` You'll be prompted for the password for the `username` account. This command will append your public key (`~/.ssh/id_rsa.pub`) to the `~/.ssh/authorized_keys` file on the target server. Alternatively, if `ssh-copy-id` isn't available, you can manually copy the public key: 1. **Copy the public key:** ```bash cat ~/.ssh/id_rsa.pub ``` Copy the entire output of this command. 2. **Connect to the server using password authentication:** ```bash ssh username@server_address ``` 3. **Edit the `authorized_keys` file:** ```bash mkdir -p ~/.ssh chmod 700 ~/.ssh nano ~/.ssh/authorized_keys # Or use your preferred text editor (vi, vim, etc.) ``` 4. **Paste the public key** into the `authorized_keys` file. Make sure it's all on one line. Save the file. 5. **Set correct permissions:** ```bash chmod 600 ~/.ssh/authorized_keys ``` * **Connect using SSH Key Authentication:** Now, when you try to connect: ```bash ssh username@server_address ``` You should *not* be prompted for a password (unless you used a passphrase when generating the key, in which case you'll be prompted for the passphrase). **4. Sending Shell Commands:** You can execute a single shell command on the remote server directly from your local machine: ```bash ssh username@server_address "command to execute" ``` Example: ```bash ssh john.doe@192.168.1.100 "ls -l /home/john.doe" ``` This will execute the `ls -l /home/john.doe` command on the remote server and display the output in your local terminal. You can chain commands using `&&` or `;`: ```bash ssh john.doe@192.168.1.100 "mkdir -p /tmp/test && touch /tmp/test/file.txt" ``` **5. Sending Files (using `scp`):** `scp` (Secure Copy) is used to securely transfer files between your local machine and the remote server. * **Copy a file from your local machine to the server:** ```bash scp local_file username@server_address:remote_directory ``` Example: ```bash scp my_script.sh john.doe@192.168.1.100:/home/john.doe/scripts/ ``` This will copy the `my_script.sh` file from your current directory on your local machine to the `/home/john.doe/scripts/` directory on the remote server. * **Copy a file from the server to your local machine:** ```bash scp username@server_address:remote_file local_directory ``` Example: ```bash scp john.doe@192.168.1.100:/home/john.doe/data.txt ./ ``` This will copy the `data.txt` file from the `/home/john.doe/` directory on the remote server to your current directory on your local machine. **6. Sending Files (using `sftp`):** `sftp` (Secure File Transfer Protocol) provides an interactive file transfer session. ```bash sftp username@server_address ``` Once connected, you can use commands similar to `ftp`: * `put local_file remote_file`: Upload a file. * `get remote_file local_file`: Download a file. * `ls`: List files on the remote server. * `cd`: Change directory on the remote server. * `lcd`: Change directory on the local machine. * `exit`: Close the connection. **7. Sending a File as Input to a Command (using redirection):** You can use input redirection to send the contents of a local file as input to a command executed on the remote server: ```bash ssh username@server_address "cat > remote_file.txt" < local_file.txt ``` This will: 1. `cat > remote_file.txt`: On the remote server, this command will create (or overwrite) a file named `remote_file.txt` and redirect standard input to it. 2. `< local_file.txt`: On your local machine, this redirects the contents of `local_file.txt` to standard input. 3. The SSH connection pipes the standard input from your local machine to the standard input of the `cat` command on the remote server. **8. Using `bash` script to execute multiple commands:** ```bash #!/bin/bash # Server details USERNAME="your_username" SERVER="your_server_address" # Commands to execute COMMANDS=( "mkdir -p /tmp/test" "touch /tmp/test/file1.txt" "echo 'Hello from remote server' > /tmp/test/file1.txt" "ls -l /tmp/test" ) # Loop through the commands and execute them for CMD in "${COMMANDS[@]}"; do echo "Executing: $CMD" ssh "$USERNAME@$SERVER" "$CMD" if [ $? -ne 0 ]; then echo "Command failed: $CMD" exit 1 # Exit the script if a command fails fi done echo "All commands executed successfully." ``` **Important Security Considerations:** * **Use SSH Keys:** Always prefer SSH key authentication over password authentication. It's much more secure. * **Disable Password Authentication (if possible):** Once you have SSH key authentication set up, consider disabling password authentication in the `sshd_config` file on the server (`/etc/ssh/sshd_config`). Set `PasswordAuthentication no` and restart the SSH service. This prevents brute-force password attacks. * **Firewall:** Make sure your server's firewall is configured to only allow SSH connections from trusted IP addresses or networks. * **Keep SSH Software Updated:** Regularly update your SSH client and server software to patch security vulnerabilities. * **Be Careful with Sudo:** If you need to run commands with `sudo` on the remote server, be very careful about what commands you're executing. Avoid running arbitrary commands with `sudo` unless absolutely necessary. * **Permissions:** Pay attention to file permissions on the remote server. Make sure files and directories have appropriate permissions to prevent unauthorized access. **Chinese Translation of Key Terms:** * SSH: 安全外壳协议 (Ānquán Wàiké Xiéyì) * Server: 服务器 (Fúwùqì) * Client: 客户端 (Kèhùduān) * Username: 用户名 (Yònghùmíng) * Password: 密码 (Mìmǎ) * SSH Key: SSH 密钥 (SSH Mìyào) * Public Key: 公钥 (Gōngyào) * Private Key: 私钥 (Sīyào) * File: 文件 (Wénjiàn) * Directory: 目录 (Mùlù) or 文件夹 (Wénjiànjiā) * Command: 命令 (Mìnglìng) * `scp`: 安全拷贝 (Ānquán Kǎobèi) * `sftp`: 安全文件传输协议 (Ānquán Wénjiàn Chuánshū Xiéyì) * Firewall: 防火墙 (Fánghuǒqiáng) This comprehensive guide should help you establish SSH connections and send commands or files to your target Linux server securely. Remember to prioritize security best practices.

shiyi25928988

开发者工具
访问服务器

README

linuxSshMcpServer

建立 SSH 连接并向目标 Linux 服务器发送 shell 命令或文件。

推荐服务器

Playwright MCP Server

Playwright MCP Server

一个模型上下文协议服务器,它使大型语言模型能够通过结构化的可访问性快照与网页进行交互,而无需视觉模型或屏幕截图。

官方
精选
TypeScript
Magic Component Platform (MCP)

Magic Component Platform (MCP)

一个由人工智能驱动的工具,可以从自然语言描述生成现代化的用户界面组件,并与流行的集成开发环境(IDE)集成,从而简化用户界面开发流程。

官方
精选
本地
TypeScript
MCP Package Docs Server

MCP Package Docs Server

促进大型语言模型高效访问和获取 Go、Python 和 NPM 包的结构化文档,通过多语言支持和性能优化来增强软件开发。

精选
本地
TypeScript
Claude Code MCP

Claude Code MCP

一个实现了 Claude Code 作为模型上下文协议(Model Context Protocol, MCP)服务器的方案,它可以通过标准化的 MCP 接口来使用 Claude 的软件工程能力(代码生成、编辑、审查和文件操作)。

精选
本地
JavaScript
@kazuph/mcp-taskmanager

@kazuph/mcp-taskmanager

用于任务管理的模型上下文协议服务器。它允许 Claude Desktop(或任何 MCP 客户端)在基于队列的系统中管理和执行任务。

精选
本地
JavaScript
mermaid-mcp-server

mermaid-mcp-server

一个模型上下文协议 (MCP) 服务器,用于将 Mermaid 图表转换为 PNG 图像。

精选
JavaScript
Jira-Context-MCP

Jira-Context-MCP

MCP 服务器向 AI 编码助手(如 Cursor)提供 Jira 工单信息。

精选
TypeScript
Linear MCP Server

Linear MCP Server

一个模型上下文协议(Model Context Protocol)服务器,它与 Linear 的问题跟踪系统集成,允许大型语言模型(LLM)通过自然语言交互来创建、更新、搜索和评论 Linear 问题。

精选
JavaScript
Sequential Thinking MCP Server

Sequential Thinking MCP Server

这个服务器通过将复杂问题分解为顺序步骤来促进结构化的问题解决,支持修订,并通过完整的 MCP 集成来实现多条解决方案路径。

精选
Python
Curri MCP Server

Curri MCP Server

通过管理文本笔记、提供笔记创建工具以及使用结构化提示生成摘要,从而实现与 Curri API 的交互。

官方
本地
JavaScript