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

Linux系统——ssh远程连接

Linux系统——ssh远程连接

  • 一、ssh协议介绍
    • 1、远程连接协议
    • 2、ssh服务基本操作
    • 3、ssh常用操作
  • 二、ssh加密
    • 1、加密算法类型
    • 2、对称加密算法
    • 3、非对称加密算法
  • 三、免密ssh的配置
    • 1、ssh认证方式
    • 2、配置免密ssh
    • 3、ssh-copy-id做了什么?
  • 四、ssh服务配置

一、ssh协议介绍

1、远程连接协议

ssh协议,应用层

远程连接的协议:
1、ssh协议,典型连接linux服务器、网络设备【密文】
2、telnet协议,典型在局域网连接网络设备【明文】
3、RDP协议,典型连接windows服务器

服务器操作方式:
1、本地操作
2、远程连接操作

2、ssh服务基本操作

  • 查看sshd服务的状态
[root@martin-host ~]# systemctl status sshd 
● sshd.service - OpenSSH server daemonLoaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)Active: active (running) since 二 2024-10-22 09:04:39 CST; 38min agoDocs: man:sshd(8)man:sshd_config(5)Main PID: 1160 (sshd)Tasks: 1CGroup: /system.slice/sshd.service└─1160 /usr/sbin/sshd -D1022 09:04:39 martin-host.linux.com systemd[1]: Starting OpenSSH server daemon...
1022 09:04:39 martin-host.linux.com sshd[1160]: Server listening on 0.0.0.0 port 22.
1022 09:04:39 martin-host.linux.com sshd[1160]: Server listening on :: port 22.
1022 09:04:39 martin-host.linux.com systemd[1]: Started OpenSSH server daemon.
[root@martin-host ~]# 
[root@martin-host ~]# ps -elf | grep ssh
4 S root       1160      1  0  80   0 - 28225 poll_s 09:04 ?        00:00:00 /usr/sbin/sshd -D
// 查看ssh服务的端口
[root@martin-host ~]# netstat -tunlp | grep ssh 
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1160/sshd           
tcp6       0      0 :::22                   :::*                    LISTEN      1160/sshd         
  • 查看sshd服务对应的软件
[root@martin-host ~]# which sshd
/usr/sbin/sshd[root@martin-host ~]# rpm -qf /usr/sbin/sshd
openssh-server-7.4p1-21.el7.x86_64
  • windows客户端软件
    xshell、secureCRT、xterminal、XModerm、Putty

3、ssh常用操作

  • 远程连接
# ssh 用户名@主机
  • 执行远程命令
[root@martin-host ~]# ssh root@192.168.140.10 ifconfig ens33 
  • 远程拷贝文件
// 远程拷贝文件 
[root@martin-host ~]# scp file01 root@192.168.140.10:/tmp 
root@192.168.140.10's password: 
file01                                                                100%    4     2.2KB/s   00:00    [root@martin-host ~]# scp root@192.168.140.10:/etc/fstab ./ 
root@192.168.140.10's password: 
fstab                                                                100%  465   339.8KB/s   00:00    // 拷贝目录 
[root@martin-host ~]# scp -r test/ root@192.168.140.10:/tmp

二、ssh加密

1、加密算法类型

对称加密算法
非对称加密算法

2、对称加密算法

加密、解密时使用的密钥是一样的
在这里插入图片描述典型算法: DES、3DES、AES

  • 加密数据
[root@martin-host ~]# openssl enc -e -des -in /opt/file01 -out /opt/file01_new 
enter des-cbc encryption password:
Verifying - enter des-cbc encryption password:
  • 解密数据
[root@node01 ~]# openssl enc -d -des -in /opt/file01_new -out /opt/file01 
enter des-cbc decryption password:

3、非对称加密算法

加密、解密时使用的密钥是不一样的

加密方式:公钥加密、私钥解密

典型算法:RSA、DSA

在这里插入图片描述

三、免密ssh的配置

1、ssh认证方式

基于密码的认证
基于密钥的认证(免密ssh)

2、配置免密ssh

  • 在客户端生成一个密钥对
[root@martin-host ~]# ssh-keygen -t rsa 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:zYgk4ThXS0U8e9kY/Gu3iFhesz3rxqRJtFpnKRiNfCQ root@martin-host.linux.com
The key's randomart image is:
+---[RSA 2048]----+
|    . o+o.       |
|   o + .o E .    |
|  o + o  + @     |
|   o o ..+B *    |
|      . S.o= o . |
|          o X *  |
|         + B # . |
|        . + = *  |
|             ooo |
+----[SHA256]-----+[root@martin-host ~]# ls /root/.ssh/
id_rsa  id_rsa.pub
[root@martin-host ~]# 
  • 将公钥拷贝到远端的服务器
[root@martin-host ~]# ssh-copy-id root@192.168.140.10
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.140.10 (192.168.140.10)' can't be established.
ECDSA key fingerprint is SHA256:6If14U96x1VtkGO5zXGKxnmGRUw2Cv04pVWSeq6Rd0I.
ECDSA key fingerprint is MD5:9b:1c:33:d9:31:ff:4d:11:a7:bd:77:bb:b0:82:11:3e.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.140.10's password: Number of key(s) added: 1Now try logging into the machine, with:   "ssh 'root@192.168.140.10'"
and check to make sure that only the key(s) you wanted were added.
  • 测试
[root@martin-host ~]# ssh root@192.168.140.10
Last login: Tue Oct 22 14:08:08 2024 from 192.168.140.1
[root@node01 ~]# exit
登出
Connection to 192.168.140.10 closed.[root@martin-host ~]# ssh root@192.168.140.10 hostname
node01
[root@martin-host ~]# 
[root@martin-host ~]# scp /etc/passwd root@192.168.140.10:/tmp/
passwd                                                                100% 2368     1.1MB/s   00:00    
[root@martin-host ~]# 注意:
1、ssh免密是基于用户的
2、ssh免密是单向的

3、ssh-copy-id做了什么?

  • 将公钥文件拷贝到远端服务器对应用户的家目录,改名为authorized_keys
[root@node01 ~]# ls -a /home/martin/.ssh/
.  ..  authorized_keys
  • 将密钥文件的权限修改为600
[root@node01 ~]# ls -l /home/martin/.ssh/
total 4
-rw------- 1 martin martin 408 Oct 22 14:18 authorized_keys
[root@node01 ~]# 

四、ssh服务配置

  • 配置文件
    /etc/ssh/sshd_config

  • 修改默认端口

[root@martin-host ~]# vim /etc/ssh/sshd_config 
Port 23333[root@martin-host ~]# systemctl restart sshd[root@martin-host ~]# netstat -tunlp | grep ssh
tcp        0      0 127.0.0.1:6010          0.0.0.0:*               LISTEN      6022/sshd: root@pts 
tcp        0      0 0.0.0.0:23333           0.0.0.0:*               LISTEN      8472/sshd 
[root@node01 ~]# ssh root@192.168.140.135 -p 23333[root@node01 ~]# scp -P 23333 /etc/passwd root@192.168.140.135:/tmp/
  • 禁止使用root用户远程登录
[root@martin-host ~]# vim /etc/ssh/sshd_config 
PermitRootLogin no[root@martin-host ~]# systemctl restart sshd[root@node01 ~]# ssh -p 23333 martin@192.168.140.135
martin@192.168.140.135's password: 
[martin@martin-host ~]$ 
  • 关闭DNS解析
[root@martin-host ~]# vim /etc/ssh/sshd_config 
UseDNS no[root@martin-host ~]# systemctl restart sshd
http://www.lryc.cn/news/466846.html

相关文章:

  • python学习-第一个小游戏(vscode环境)
  • 程序设计基础I-单元测试2(机测)
  • Claude 3.5深夜觉醒,学会模仿人类用电脑,力压GPT-4o
  • PuTTY
  • 2024软件测试面试秘籍(含答案+文档)
  • 券商api怎么获取,如何获取券商API接口?
  • 跟着六西格玛设计DFSS走,让你的项目、服务、产品都“牛”起来——张驰咨询
  • 【2024.10.22练习】机器人塔
  • 酒店预订订房小程序源码系统 多酒店入驻+打造类似美团的酒店模式 带完整的安装代码包以及搭建部署教程
  • springboot037基于SpringBoot的墙绘产品展示交易平台的设计与实现(论文+源码)_kaic
  • YOLOv8实战人脸-口罩检测与识别【数据集+YOLOv8模型+源码+PyQt5界面】
  • 《黑神话悟空》各章节boss顺序汇总
  • rust中cargo.toml详细介绍
  • jupyter notebook 笔记
  • Atlas800昇腾服务器(型号:3000)—CANN安装(二)
  • 考研鼓励小程序
  • Wooden UI(木头UI纹理按钮边框 背景图标 带PNG素材)
  • WebRTC音频 03 - 实时通信框架
  • Maven陷阱揭秘:避开Java项目构建的10大常见误区
  • 基础数据结构思路写法记录,便于回顾
  • 基于AI的量化投资框架Qlib的Python依赖包pyqlib安装问题记录
  • 《语音识别方案选择》
  • 目标检测数据集图片及标签同步裁剪
  • 【设计模式-简单工厂】
  • 多个版本的GCC(GNU编译器集合)可以同时安装并存
  • 量子纠错--shor‘s 码
  • 机器学习2
  • 二分查找_ x 的平方根搜索插入位置山脉数组的峰顶索引
  • 汽车建模用什么软件最好?汽车建模渲染建议!
  • 蘑菇分类识别数据集(猫脸码客 第222期)