admin 管理员组

文章数量: 1184232

ssh远程连接

ssh默认使用22端口

1windows系统远程连接

ssh root@192.168.74.129

ssh -p22 root@192.168.74.129指定端口号

2linux系统远程连接

ssh -p 端口号 root@192.168.74.129指定端口号

[root@server 桌面]# ssh 192.168.74.129
root@192.168.74.129's password: 
Last login: Tue Aug  5 22:25:42 2025 from 192.168.74.8
[root@backup ~]# exit
登出
Connection to 192.168.74.129 closed.
[root@server 桌面]# ssh -p 2222 192.168.74.129
ssh: connect to host 192.168.74.129 port 2222: Connection refused
[root@server 桌面]# ssh -p 22 192.168.74.129
root@192.168.74.129's password: 
Last login: Wed Aug  6 03:04:00 2025 from 192.168.74.130
[root@backup ~]# 

scp远程执行命令

scp命令主要用于在本地主机和远程主机之间安全地复制文件,它基于SSH协议进行数据传输,因此也继承了SSH的安全特性。以下是SCP命令的一些基本用法:

语法:scp [选项] [源路径] [目标路径]

案例一:把本机的scp.txt文件传到192.168.74.129的家目录下
[root@server ~]# scp scp.txt 192.168.74.129:/root
root@192.168.74.129's password: 
scp.txt                                       100%    0     0.0KB/s   00:00    
案例二:把192.168.74.129下的1.txt下载到当前所在目录
[root@server ~]# scp 192.168.74.129:/root/1.txt .
root@192.168.74.129's password: 
1.txt                                         100%    0     0.0KB/s   00:00    
[root@server ~]# scp -p 22 scp.txt 192.168.74.129:/root
案例三:指定端口推送
[root@server ~]# scp -p22 scp.txt 192.168.74.129:/root
root@192.168.74.129's password: 
scp.txt                                       100%    0     0.0KB/s   00:00    
案例四:拷贝目录需要-r参数递归
[root@server ~]# scp -r xiazai 192.168.74.129:/root
root@192.168.74.129's password: 
sersync2.5.4_64bit_binary_stable_final.tar.gz 100%  710KB 710.2KB/s   00:00    
sersync2.5.4_64bit_binary_stable_final.tar.gz 100%  710KB 710.2KB/s   00:00  

ssh免密钥连接

流程:本地创建一对密钥和公钥,密钥只有本地保留,公钥上传到服务器

第一步:本地创建密钥对

[root@server ~]# ssh-keygen
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:
04:c7:02:b9:6c:f4:77:c3:38:08:a7:06:6d:c6:7e:14 root@server
The key's randomart image is:
+--[ RSA 2048]----+
|   o.oEo.        |
|  . O +o.        |
|   B B o.o       |
|    B +.+ +      |
|   o . .So .     |
|                 |
|                 |
|                 |
|                 |
+-----------------+

二:钥匙文件保存在.ssh目录里,查看

id_rsa是密钥,id_rsa.pub是公钥

[root@server ~]# ll .ssh/
总用量 12
-rw------- 1 root root 1675 8月   6 04:54 id_rsa
-rw-r--r-- 1 root root  393 8月   6 04:54 id_rsa.pub
-rw-r--r-- 1 root root  176 8月   2 01:50 known_hosts

三:把公钥推送到服务器

[root@server .ssh]# ssh-copy-id 192.168.74.129

四:此时远程连接不需要输入密码

[root@server .ssh]# ssh 192.168.74.129
Last login: Wed Aug  6 04:58:44 2025 from 192.168.74.130

企业做免秘钥出现问题最多的:

一切都没问题后还是无法免秘钥连接: 需要检查每层目录的权限 一直到/目录

ssh必须是700
root权限必须是550
/ 权限必须是555

切忌一定不要使用-R递归修改root的权限

远程执行命令

语法:ssh  地址  "命令"

[root@server .ssh]# ssh 192.168.74.129 "touch /root/123456"
[root@server .ssh]# ssh 192.168.74.129 "hostname"
backup

这是我的个人学习笔记,主要用于记录自己对知识点的理解和梳理。由于目前仍在学习探索阶段,内容中难免存在理解偏差或表述疏漏,恳请各位大佬不吝赐教,多提宝贵意见~ 若有不同看法,欢迎理性交流探讨,感谢包容与指正!

本文标签: 指定端口 命令 桌面