2.7 KiB
2.7 KiB
name, description
| name | description |
|---|---|
| linux-ssh-operator | 通过 SSH 连接并操作 Linux 服务器:执行远程命令、查看日志、管理 systemd 服务、传输文件(scp/rsync/tar/sftp)、排障。用户提到 ssh/scp/rsync、远程服务器 IP:端口、systemctl/journalctl、部署到服务器、在服务器上运行命令、远程拷贝文件 等场景时使用。 |
Linux SSH Operator
Goal
Use SSH for safe, repeatable Linux server operations.
Fast Decision
- Remote command or service check ->
scripts/ssh_run.sh - Single file copy ->
scripts/ssh_copy.sh --method scp - Directory sync or exclusions ->
scripts/ssh_copy.sh --method rsync -r - Many small files ->
scripts/ssh_copy.sh --method tar - If sudo may prompt -> add
--tty --sudo
Prefer explicit method selection when the shape is already known. It is faster and avoids bad auto guesses.
Before Acting
- Confirm
host,port,user, and auth method. - Prefer SSH keys and
~/.ssh/configaliases. - Start with read-only checks, then change, then verify.
- For first-connect automation, prefer
--accept-newonly when appropriate. - On flaky links, set
--connect-timeoutso failed attempts return fast.
Command Runs
Use ssh_run.sh for non-interactive commands:
ssh_run.sh my-server -- uname -a
ssh_run.sh --tty --sudo my-server -- systemctl restart nginx
ssh_run.sh --sudo-non-interactive my-server -- systemctl status nginx --no-pager
Notes:
--sudois for commands that may prompt.--sudo-non-interactiveis only for passwordless sudo paths.
File Transfer
Use ssh_copy.sh for transfers:
ssh_copy.sh --method scp push my-server ./local.txt /tmp/local.txt
ssh_copy.sh --method rsync -r push my-server ./dir /tmp/dir
ssh_copy.sh --method tar push my-server ./many-small-files/ /tmp/
Rules:
--taris a packaging mode, not something to mix with--method rsync/scp/sftp.- Use
--excludeonly withrsync. - Use
--deleteonly when you really want destination cleanup.
Common Ops
- Disk:
df -h,du -sh /path/* | sort -h - Memory/CPU:
free -h,top,ps aux --sort=-%mem | head - Logs:
journalctl -u SERVICE -n 200 --no-pager - Services:
systemctl status|restart|stop SERVICE - Networking:
ss -lntp,ip a,ip r
Safety
- Never store passwords in repo files or chat logs.
- Avoid
StrictHostKeyChecking=no. - For destructive commands, ask for explicit confirmation and show the exact command first.
References
- SSH security + troubleshooting:
references/ssh-playbook.md
Scripts
scripts/ssh_run.sh: remote command execution with consistent options.scripts/ssh_copy.sh: file transfer via scp/rsync/tar/sftp with consistent options.