当前位置: 首页 > news >正文

ssh工具

文章目录

  • ssh简介
  • ssh远程连接
  • Linux下使用SSH
    • 安装
      • 安装ssh服务端
      • 安装ssh客户端
    • 命令
      • 启动
      • 重启
      • 查看ssh的状态
    • ssh 配置文件
    • ssh连接地址 配置文件
      • 基本配置
      • 注意
      • 通配符
      • 心跳和密钥
      • ssh的Include
      • 跳板 ProxyJump
      • 内网穿透
  • Windows下使用SSH
    • 安装
    • ssh 配置文件
    • ssh连接地址 配置文件

ssh简介

ssh(Secure Shell,简称SSH)是安全外壳协议,是一种在不安全网络上用于安全远程登录和其他安全网络服务的协议。

ssh远程连接

格式:

ssh 远程用户名@远程主机ip
ssh -p 端口号 远程用户名@远程主机ip
ssh 远程用户名@远程主机ip -p 端口号

示例:

ssh root@47.xx.xxx.90
然后要输入root用户的密码

当然想要使用ssh,那么计算机上必须安装了ssh程序。

Linux下使用SSH

以Ubuntu为例。

安装

安装ssh服务端

安装了openssh-server才能被其他主机连接。

sudo apt-get install openssh-server

安装ssh客户端

安装了openssh-client才能连接远程主机。

sudo apt-get install sshsudo apt-get install openssh-client

命令

启动

systemctl start ssh

或

systemctl start sshd

或

service ssh start

或

启动sshserver:/etc/init.d/ssh start

重启

systemctl restart ssh

或

systemctl restart sshd

或

service ssh restart

或

重启sshserver:/etc/init.d/ssh restart

查看ssh的状态

systemctl status ssh
或
systemctl status sshd
或
service ssh status

ssh 配置文件

ssh客户端配置文件:/etc/ssh/ssh_config

ssh服务端配置文件:/etc/ssh/sshd_config

修改配置文件后重启系统(电脑)生效。

ssh连接地址 配置文件

~/.ssh/config文件,没有的话,自己创建即可。

基本配置

config文件的内容

Host 自己给远程机器起的别名
  HostName 远程主机ip地址
  User 远程用户名
  Port 端口
  ForwardAgent yes

Host不能重复,重复的别名中只有第一个有效
Port默认值为22;使用22端口时,可省略Port行。
示例

Host ubuntu
  HostName 172.24.0.1
  User uu
  Port 22
  ForwardAgent yes

设置好配置文件:可以通过ssh 别名 —> 输入“密码” —> 回车,来登录远程计算机(机器),如:

ssh ubuntu
输入密码

注意

~/.ssh/config文件,ssh远程机器配置信息,有覆盖效果;前面配置的信息,会覆盖后面配置的Host同名的信息。

通配符

Host server*
  User uu
  Port 22
  ForwardAgent yes

Host server1
  HostName 172.24.0.1

Host server2
  HostName 172.24.0.2

Host server3
  HostName 172.24.0.3

存在用户名、端口等不相同的信息,防止覆盖:

Host server1
  HostName 172.24.0.1

Host server2
  HostName 172.24.0.2
  User s2

Host server3
  HostName 172.24.0.3
  Port 3333

Host server*
  User uu
  Port 22
  ForwardAgent yes

心跳和密钥

Host 自己给远程机器起的别名
  HostName 远程主机ip地址
  User 远程用户名
  Port 端口
  ForwardAgent yes
  ServerAliveInterval 180
  IdentityFile ~/.ssh/secret_key.pem

ServerAliveInterval 心跳机制。180 表示在建立连接后,每隔180秒向远程机器发送一个心跳,避免用户长时间无操作而导致连接中断。
IdentityFile ~/.ssh/secret_key.pem 表示使用指定密钥(专用密钥)。

ssh的Include

ssh支持多文件引用,可通过在~/.ssh/config文件使用Include包含多个配置文件。

# 默认配置文件
~/.ssh/config

# 其他文件示例
~/.ssh/config1
~/.ssh/config2
~/.ssh/config3
/path/to/config4

~/.ssh/config,使用Include

# 分别include每个配置文件
Include config1
Include config2
Include config3
Include /path/to/config4

# 使用通配符
Include config*
Include /path/to/config4

跳板 ProxyJump

Host Jumper
  HostName 74.24.x.x
  User haha
  Port 22

Host server*
  User haha
  Port 22
  ProxyJump Jumper
  ForwardAgent yes

Host server1
  HostName 172.24.0.1

Host server2
  HostName 172.24.0.2

Host server3
  HostName 172.24.0.3

Jumper 为跳板,Host Jumper段配置了跳板机的登陆方式;跳板机也是远程机器,可以通过ssh连接到它,前提是跳板机也有User haha,并且Port是跳板机的ssh端口。
Host server*中ProxyJump指定了所有server开头的远程机器(服务器),在登录时从哪里跳转到对应server;这里的User、Port要设置到Jumper的User和Port中。

内网穿透

使用frp等进行内网穿透。

local* 是公网服务器,HostName是公网ip。

local_1、local_2 是内网机器,Port是本地的ssh端口,公网服务器需要开放对应端口。

Host local*
  HostName 74.24.x.x

Host local_1
  #HostName 172.24.46.100 假设内网ip是这样的
  User uu
  Port 16000

Host local_docker_1
  #HostName 172.24.46.159  假设内网ip是这样的
  User u1
  Port 16010

Host local_docker_2
  #HostName 172.24.46.159  假设内网ip是这样的
  User u2
  Port 16020

Windows下使用SSH

安装

适用于 Windows 的 OpenSSH 入门 | Microsoft Learn

ssh 配置文件

C:\ProgramData\ssh\sshd_config

ssh连接地址 配置文件

C:/User/用户名/.ssh/config,如果文件不存在,可以自己新建,编写方式同Linux。

相关文章:

  • ROS的action通信——实现阶乘运算(一)
  • 数据安全_笔记系列05:数据合规与隐私保护(GDPR、CCPA、中国《数据安全法》)深度解析
  • vite react 项目打包报错处理
  • 矩阵碰一碰发视频的后端源码技术,支持OEM
  • 使用 Containerd 通过 HTTP 协议拉取 Harbor 私有镜像仓库的镜像
  • 使用内置命令查看笔记本电池健康状态
  • Uppy - 免费开源、功能强大的新一代 web 文件上传组件,支持集成到 Vue 项目
  • Modelfile配置说明
  • AI绘画软件Stable Diffusion详解教程(2):Windows系统本地化部署操作方法(专业版)
  • ui设计公司兰亭妙微分享:科研单位UI界面设计
  • c#笔记-基础知识
  • muduo源码阅读:linux timefd定时器
  • 学习Flask:Day 1:基础搭建
  • AI大模型(四)基于Deepseek本地部署实现模型定制与调教
  • Python图像处理入门:如何打开图像文件及常见格式
  • MySQL知识
  • SpringBoot整合sharding-jdbc 实现分库分表操作
  • 实操系列:我用deepseek写sql
  • C++ | 面向对象 | 类
  • 六十天前端强化训练之第二天CSS选择器与盒模型深度解析
  • 2000多年前的“新衣”长这样!马王堆文物研究新成果上新
  • 坚持吃素,是不是就不会得高血脂了?
  • 30平米的无障碍酒吧里,我们将偏见折叠又摊开
  • 第1现场 | 美国称将取消制裁,对叙利亚意味着什么
  • 外交部亚洲司司长刘劲松会见印度驻华大使罗国栋
  • 商务部召开外贸企业圆桌会:全力为外贸企业纾困解难,提供更多支持