当前位置:网站首页>SSH 远程执行命令简介
SSH 远程执行命令简介
2022-07-03 18:33:00 【星哥玩云】
在之前,看到大都是说修改/etc/sudoers,然后NOPASSWD:指定的cmd,但是真心不管用,没有远程虚拟终端这个方法就是浮云,Ubuntu10.04 Server 亲测!!
ssh执行远程操作
命令格式
ssh -p $port [email protected]$p 'cmd'
$port : ssh连接端口号 $user: ssh连接用户名 $ip:ssh连接的ip地址 cmd:远程服务器需要执行的操作
准备工作
基于公私钥认证或者用户名密码认证能确保登录到远程local2服务器(有点基本运维知识的人做这个事情都不是问题)
cmd如果是脚本,注意绝对路径问题(相对路径在远程执行时就是坑)
不足
- 这个命令可以满足我们大多数的需求,但是通常运维部署很多东西的时候需要root权限,但是有几处限制:
- 远程服务器local2禁止root用户登录
- 在远程服务器脚本里转换身份用expect需要send密码,这样不够安全
执行远程服务器需要sudo权限的脚本
ssh的-t参数
-t Force pseudo-tty allocation. This can be used to execute arbitrary screen-based programs on a remote machine, which can be very useful, e.g. when implementing menu services. Multiple -t options force tty allocation, even if ssh has no local tty.
中文翻译一下:就是可以提供一个远程服务器的虚拟tty终端,加上这个参数我们就可以在远程服务器的虚拟终端上输入自己的提权密码了,非常安全
命令格式
ssh -t -p $port [email protected]$ip 'cmd'
示例脚本
#!/bin/bash
#变量定义 ip_array=("192.168.1.1" "192.168.1.2" "192.168.1.3") user="test1" remote_cmd="/home/test/1.sh"
#本地通过ssh执行远程服务器的脚本 for ip in ${ip_array[*]} do if [ $ip = "192.168.1.1" ]; then port="7777" else port="22" fi ssh -t -p $port [email protected]$ip "remote_cmd" done
这个方法还是很方便的,-t虚拟出一个远程服务器的终端,在多台服务器同时部署时确实节约了不少时间啊!
边栏推荐
- [combinatorics] generating function (property summary | important generating function)*
- Recent learning experience
- On Data Mining
- Okaleido, a multimedia NFT aggregation platform, is about to go online, and a new NFT era may come
- What is SQL get connection
- Image 24 bits de profondeur à 8 bits de profondeur
- 2022-2028 global aircraft head up display (HUD) industry research and trend analysis report
- English語法_名詞 - 分類
- How to analyze the rising and falling rules of London gold trend chart
- AcWing 271. Teacher Yang's photographic arrangement [multidimensional DP]
猜你喜欢

English語法_名詞 - 分類
![Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]](/img/da/0a282b4773fe3909d1e5e9d19f8549.jpg)
Lesson 13 of the Blue Bridge Cup -- tree array and line segment tree [exercise]

Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download

On Data Mining

12、 Service management

Redis core technology and practice - learning notes (IX): slicing cluster

Kratos微服务框架下实现CQRS架构模式
![[combinatorics] dislocation problem (recursive formula | general term formula | derivation process)*](/img/e8/67961bf8a589869bde2a0aa3e09605.jpg)
[combinatorics] dislocation problem (recursive formula | general term formula | derivation process)*
![[Godot] add menu button](/img/44/ef1e6ac25bcbc8cc3ecc00c52f8ee2.jpg)
[Godot] add menu button
![The number of incremental paths in the grid graph [dfs reverse path + memory dfs]](/img/57/ff494db248171253996dd6c9110715.png)
The number of incremental paths in the grid graph [dfs reverse path + memory dfs]
随机推荐
[combinatorics] generating function (commutative property | derivative property | integral property)
Boost.Asio Library
PHP MySQL Update
Analysis of the reasons why enterprises build their own software development teams to use software manpower outsourcing services at the same time
How to draw non overlapping bubble chart in MATLAB
Computer graduation project PHP library book borrowing management system
Torch learning notes (7) -- take lenet as an example for dataload operation (detailed explanation + reserve knowledge supplement)
Summary and Reflection on the third week of winter vacation
Ping problem between virtual machine and development board
Nodejs (01) - introductory tutorial
[untitled]
Why can deeplab v3+ be a God? (the explanation of the paper includes super detailed notes + Chinese English comparison + pictures)
SQL injection -day16
Real time split network (continuous update)
Typescript official website tutorial
[combinatorics] generating function (positive integer splitting | basic model of positive integer splitting | disordered splitting with restrictions)
Self executing function
A. Odd Selection【BruteForce】
Data analysis is popular on the Internet, and the full version of "Introduction to data science" is free to download
This diversion