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

济南手工网站建设上海网络推广培训机构

济南手工网站建设,上海网络推广培训机构,做网络调查的网站赚钱,做的比较好的公司网站文章目录一、SSH是什么二、openssh软件三、如何判断ssh服务是否启动1. 查看进程2. 查看端口号3. 查看ssh服务状态4. 直接ssh登录四、对ssh服务进行安全上的加固1. 禁用root用户登录2. 修改ssh服务的端口号3. 限制登录用户4. 限制能ssh登录的客户端的ip地址5. 堡垒机 / 跳板机6.…

文章目录

  • 一、SSH是什么
  • 二、openssh软件
  • 三、如何判断ssh服务是否启动
    • 1. 查看进程
    • 2. 查看端口号
    • 3. 查看ssh服务状态
    • 4. 直接ssh登录
  • 四、对ssh服务进行安全上的加固
    • 1. 禁用root用户登录
    • 2. 修改ssh服务的端口号
    • 3. 限制登录用户
    • 4. 限制能ssh登录的客户端的ip地址
    • 5. 堡垒机 / 跳板机
    • 6. ssh故障排查
  • 五、ssh服务的认证方式
    • 1. 用户名和密码
    • 2. 密钥认证(免密通道)
      • 1. 创建密钥对
      • 2. 上传密钥到服务器
      • 3. 尝试使用密钥去连接
      • 4. 双向信任关系
      • 5. 如何同时批量完成在多台机器上同时安装软件或者备份文件?
  • 六、传输文件
    • 1. 复制本机上的文件到远程
    • 2. 复制远程服务器上的文件到本机
  • 总结


一、SSH是什么

Secure Shell 安全外壳 --》对在网络上传输的数据进行加密

场景: 远程控制和数据传输的安全性的要求 --》不泄露数据 --》保障数据安全 --》数据机密性

解决的问题

  • 对传输的数据进行加密,保护传输数据的安全
  • 远程登录,远程控制

二、openssh软件

查看本机的linux系统里安装了哪些软件
rpm -qa

rpm 也是linux系统里的软件管理的命令 --》redhat系的linux
redhat packet manager --》红帽的软件包管理工具

daemon --》守护进程 : 一直在linux系统里运行的程序,日夜等着你过来访问,除非我们手动的停止

查看已经安装了哪些与ssh相关的软件

[root@localhost ~]# rpm -qa|grep openssh
openssh-8.7p1-38.el9.x86_64 --》提供公共的工具包
openssh-clients-8.7p1-38.el9.x86_64 --》提供ssh客户端工具
openssh-server-8.7p1-38.el9.x86_64 --》提供sshd服务程序的软件

在ubuntu里查看安装的openssh软件

fengdeyong@sanchuang:~$ apt list |grep openssh

  • rocky linux
    centos
    默认已经安装了openssh软件,可以用来远程控制和远程登录

  • ubuntu系统默认不安装
    root 在ubuntu里默认是不允许远程登录ssh服务的

自动下载并安装 OpenSSH 的最新可用版本

[root@localhost ~]# yum update openssh -y


三、如何判断ssh服务是否启动

1. 查看进程

sshd 是ssh服务提供者 --》程序

[root@localhost ~]# ps -axu|grep sshd
root         816  0.0  0.2  15860  9088 ?        Ss   10:26   0:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
root        1381  0.0  0.2  19216 11008 ?        Ss   10:28   0:00 sshd: root [priv]
root        1385  0.0  0.1  19216  6972 ?        R    10:28   0:00 sshd: root@pts/0
root        1416  0.0  0.0   6636  2176 pts/0    S+   10:36   0:00 grep --color=auto sshd

2. 查看端口号

ssh服务 --》ssh的程序 --》监听端口(默认22) --》门或者窗户 --》入口

不同的程序会占用不同的端口号

查看linux系统里的哪些程序占用哪些端口? --》本机开放了哪些端口
ss
netstat

[root@localhost ~]# ss  -anplut
Netid          State           Recv-Q          Send-Q                   Local Address:Port                   Peer Address:Port          Process                                    
udp            UNCONN          0               0                            127.0.0.1:323                         0.0.0.0:*              users:(("chronyd",pid=742,fd=5))          
udp            UNCONN          0               0                                [::1]:323                            [::]:*              users:(("chronyd",pid=742,fd=6))          
tcp            LISTEN          0               128                            0.0.0.0:22                          0.0.0.0:*              users:(("sshd",pid=816,fd=3))             
tcp            LISTEN          0               128                               [::]:22                             [::]:*              users:(("sshd",pid=816,fd=4))             [root@localhost ~]# netstat  -anplut
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name    
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      816/sshd: /usr/sbin 
tcp        0    176 192.168.100.147:22      192.168.100.1:57595     ESTABLISHED 1381/sshd: root [pr 
tcp        0      0 192.168.100.147:35866   120.194.101.75:443      TIME_WAIT   -                   
tcp6       0      0 :::22                   :::*                    LISTEN      816/sshd: /usr/sbin 
udp        0      0 192.168.100.147:68      192.168.100.254:67      ESTABLISHED 796/NetworkManager  
udp        0      0 127.0.0.1:323           0.0.0.0:*                           742/chronyd         
udp6       0      0 ::1:323                 :::*                                742/chronyd         

lsof -i:端口号

[root@localhost ~]# lsof -i:22 查看22号端口被那个程序占用了
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
sshd 829 root 3u IPv4 17166 0t0 TCP *:ssh (LISTEN)
sshd 829 root 4u IPv6 17168 0t0 TCP *:ssh (LISTEN)


3. 查看ssh服务状态

systemctl status sshd

[root@localhost ~]#systemctl status sshd
● sshd.service - OpenSSH server daemon
Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; preset: enabled) --》看ssh服务是否开机启动 disabled 表示不启动
Active: active (running) since Mon 2025-06-23 17:55:33 CST; 6h ago
Docs: man:sshd(8)
man:sshd_config(5)
Main PID: 761 (sshd)
Tasks: 1 (limit: 22950)
Memory: 5.8M
CPU: 64ms
CGroup: /system.slice/sshd.service
└─761 “sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups”

设置ssh服务开机启动
systemctl enable ssh

4. 直接ssh登录

ssh 是linux里的ssh客户端工具,可以用来远程连接到别的服务器里的ssh服务上

ssh -p [端口号] [用户名]@[服务器IP或域名]


四、对ssh服务进行安全上的加固

1. 禁用root用户登录

[root@localhost ~]# cd /etc/ssh
[root@localhost ssh]# ls
moduli ssh_host_ecdsa_key ssh_host_ed25519_key.pub sshd_config
ssh_config ssh_host_ecdsa_key.pub ssh_host_rsa_key sshd_config.d
ssh_config.d ssh_host_ed25519_key ssh_host_rsa_key.pub
[root@localhost ssh]# vim sshd_config
PermitRootLogin prohibit-password 阻止root用户使用密码登录
#PermitRootLogin no
[root@localhost ssh]# ls sshd_config.d/
01-permitrootlogin.conf 50-redhat.conf
[root@localhost ssh]# cd sshd_config.d/
[root@localhost sshd_config.d]# vim 01-permitrootlogin.conf
#PermitRootLogin yes 注释掉这一行


2. 修改ssh服务的端口号

端口号的范围 0~65535
1-1024 预留的经典端口,不建议使用

  1. 编辑 /etc/ssh/sshd_config 文件

[root@localhost ssh]# vim sshd_config # sshd服务的主配置文件
Include /etc/ssh/sshd_config.d/*.conf # 加载存放
# sshd_config.d 存放其他次要配置文件的目录
# 在/etc/ssh/sshd_config.d目录下的以.conf结尾的文件
#Port 22
Port 2233

  1. 重启sshd服务

[root@localhost ssh]# service sshd restart
error: Bind to port 2233 on 0.0.0.0 failed: Permission denied.

selinux是rocky linux里的一套安全机制,用于保护linux系统,默认只允许 sshd 监听标准端口(22)

查看selinux的执行状态

[root@localhost sshd_config.d]# getenforce
Enforcing

临时关闭selinux

[root@localhost sshd_config.d]# setenforce 0

再重启sshd服务

[root@localhost sshd_config.d]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service

firewalld 是linux里防火墙软件,默认不允许访问2233端口

关闭firewalld服务

[root@localhost sshd_config.d]# service firewalld stop
Redirecting to /bin/systemctl stop firewalld.service
[root@localhost sshd_config.d]#

永久关闭firewalld和selinux

设置firewalld服务开机不启动

[root@localhost sshd_config.d]# systemctl disable firewalld

禁用selinux

[root@localhost sshd_config.d]# vim /etc/selinux/config
SELINUX=disabled

修改端口号,导致ssh服务启动不起来,启动起来后又ssh不上的原因?
1.selinux 阻止了我们去修改sshd监听2233端口,导致sshd服务启动不了
2.访问不了2233端口,因为防火墙阻止了访问2233

新建普通用户feng并且设置密码为123456

[root@localhost sshd_config.d]# useradd feng 新建用户
[root@localhost sshd_config.d]# passwd feng 设置密码
Changing password for user feng.
New password:
BAD PASSWORD: The password is shorter than 8 characters
Retype new password:
passwd: all authentication tokens updated successfully.

先使用普通用户ssh进入linux系统,然后使用su - root 切换到超级管理员

默认情况下ubuntu里的root用户是禁用的,需要去激活root用户
激活root本质上就是给root用户设置密码,默认的root用户没有设置密码

为什么直接ssh不能登录,进来后可以su切换呢?

  1. SSH 认证:由sshd服务控制
    PermitRootLogin no 仅禁止 root 用户通过 SSH 远程登录,但不影响 root 账户在本地的使用。
  2. su/sudo 认证:由系统本地 PAM 模块(Pluggable Authentication Modules)控制
    普通用户登录后,可通过su -(需输入 root 密码)或sudo(需自身密码且被授权)临时获取 root 权限

3. 限制登录用户

禁用sc用户ssh远程登录

[root@db ssh]# vim sshd_config
DenyUsers sc

修改配置后重启sshd服务

[root@db ssh]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service


4. 限制能ssh登录的客户端的ip地址

tcp wrappers --》管控很多服务,允许别人访问或者进行访问 --》中间人 --》保安 --》防火墙

SSH使用TCP Wrappers实现访问控制
主要配置文件
/etc/hosts.allow —》白名单,在里面的都放行,允许访问
/etc/hosts.deny --》黑名单 ,拒绝访问

TCP Wrappers的访问控制原则

  1. 首先检查 hosts.allow 文件,若找到相匹配的策略,则允许访问
  2. 否则继续检查 hosts.deny 文件,若找到相匹配的策略,则拒绝访问
  3. 如果两个文件中都没有相匹配的策略,则允许访问
    user -->hosts.allow —>hosts.deny

修改文件后,立马生效,不需要刷新服务

[root@db ssh]# vim /etc/hosts.allow
sshd:192.168.100.150
[root@db ssh]# vim /etc/hosts.deny
sshd:ALL
sshd:192.168.100.150,192.168.100.1

centos和 ubuntu 是支持tcp wrappers功能
rocky linux 都不支持tcp_wrappers 功能 --》默认

rocky linux 如何实现限制其他的机器不能ssh过来呢? 控制ssh远程连接的ip地址

iptables 防火墙工具可以做到

[root@rocky-1 .ssh]# iptables -A INPUT -s 192.168.100.1 -p tcp --dport 2233 -j ACCEPT

-A 追加策略 append
INPUT 进入防火墙数据过滤的进入主机系统的位置
-s 192.168.100.1 指定ip数据包的源ip source
-p tcp 指定协议是tcp协议 protocol
--dport 2233 指定目的端口是2233 destination port
-j ACCEPT 采取的策略是接受 --》允许访问

设置INPUT链(位置)默认规则为拒绝

[root@rocky-1 .ssh]# iptables -P INPUT DROP

查看iptables规则
-L list 列出
-n number 以数字的形式显示

[root@rocky-1 .ssh]# iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT tcp – 192.168.100.1 0.0.0.0/0 tcp dpt:2233
 
Chain FORWARD (policy ACCEPT)
target prot opt source destination
 
Chain OUTPUT (policy ACCEPT)
target prot opt source destination

清除防火墙规则

[root@rocky-1 backup]# iptables -P INPUT ACCEPT 将INPUT链默认策略修改为ACCEPT
[root@rocky-1 backup]# iptables -F 清除其他规则


5. 堡垒机 / 跳板机

JumpServer 堡垒机帮助企业以更安全的方式管控和登录各种类型的资产。

等保合规 --》等级保护 --》计算机安全管理条例 --》全国所有的企业 --》信息安全建设 --》投入资金
5个等级

大企业/国企/政府

JumpSever v4版本https://docs.jumpserver.org/zh/v4/installation/setup_linux_standalone/offline_install/

堡垒机


6. ssh故障排查

如何有一台linux系统ssh远程连接不到服务器,请排查原因?

[root@rocky-1 .ssh]# iptables -A INPUT -p icmp -j ACCEPT 允许ping的规则

nc 是一个网络端口扫描/嗅探工具–》远程网络端口扫描 --》查看别人的机器开放了哪些端口

netstat 和 ss 是在本机使用的,查看本机开放了哪些端口

fengdeyong@sanchuang:~$ nc -z 192.168.100.154 -w 1 3308
Connection to 192.168.100.154 3308 port [tcp/] succeeded!
fengdeyong@sanchuang:~$ nc -z 192.168.100.154 33060
Connection to 192.168.100.154 33060 port [tcp/
] succeeded!
fengdeyong@sanchuang:~$ nc -z 192.168.100.154 -w 1 33060
Connection to 192.168.100.154 33060 port [tcp/*] succeeded!

ssh故障排查


五、ssh服务的认证方式

1. 用户名和密码

PasswordAuthentication yes
#PermitEmptyPasswords no --》ssh服务不允许空密码登录

rocky新建用户

[root@localhost ssh]# useradd feng
[root@localhost ssh]# echo “feng:123456”|chpasswd

ubuntu连接rocky

root@sanchuang:/etc/ssh# ssh -p 2233 feng@192.168.100.150
The authenticity of host ‘[192.168.100.150]:2233 ([192.168.100.150]:2233)’ can’t be established.
ED25519 key fingerprint is SHA256:NmhJilR0o4EB/9J/Wf3WVEVR31RGiT5RO2TA0nuJkIw.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added ‘[192.168.100.150]:2233’ (ED25519) to the list of known hosts.
feng@192.168.100.150’s password:
Last login: Mon Jun 23 16:23:34 2025 from 192.168.100.1

ubuntu新建用户

root@sanchuang:/etc/ssh# useradd xiarui
root@sanchuang:/etc/ssh# echo “xiarui:123456”|chpasswd
root@sanchuang:/etc/ssh#

rocky连接ubuntu

[root@localhost ssh]# ssh -p2233 xiarui@192.168.100.151
The authenticity of host ‘[192.168.100.151]:2233 ([192.168.100.151]:2233)’ can’t be established.
ED25519 key fingerprint is SHA256:9kaGZcEP7F4zD26TR6NkPzwxqH0MJIwFDJrtrDbLIwU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added ‘[192.168.100.151]:2233’ (ED25519) to the list of known hosts.
xiarui@192.168.100.151’s password:
Welcome to Ubuntu 24.04 LTS (GNU/Linux 6.8.0-62-generic x86_64)
$ su - root
Password:
root@sanchuang:~#


2. 密钥认证(免密通道)

#PubkeyAuthentication yes
公钥和私钥

对称加密
非对称加密

rocky 192.168.100.150 --》client
Ubuntu 192.168.100.151 --》server

免密通道的创建步骤

1. 创建密钥对

在2台服务器上都创建huang用户

Ubuntu切换用户到huang

root@sanchuang:/home# mkdir /home/huang
root@sanchuang:/home# cd /home/huang
root@sanchuang:/home/huang# ls -a
. …
root@sanchuang:/home/huang# cp /etc/skel/.* . # 拷贝用户登录初始化的脚本文件到huang用户的家目录下
root@sanchuang:/home/huang# ls -a
. … .bash_logout .bashrc .profile
root@sanchuang:/home/huang# chown huang:huang /home/huang -R 修改/home/huang文件夹的属主和属组
root@sanchuang:/home/huang# ll /home
total 16
drwxr-xr-x 4 root root 4096 Jun 24 03:31 ./
drwxr-xr-x 23 root root 4096 Jun 23 03:46 …/
drwxr-x— 4 fengdeyong fengdeyong 4096 Jun 24 01:45 fengdeyong/
drwxr-xr-x 2 huang huang 4096 Jun 24 03:31 huang/
切换到huang用户登录系统
[root@localhost ~]# su - huang

生成密钥对
ssh-keygen -t rsa

-t 指定加密算法

[huang@localhost ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/huang/.ssh/id_rsa):
Created directory ‘/home/huang/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/huang/.ssh/id_rsa
Your public key has been saved in /home/huang/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:44IO/y85zqlwnZpf8mjzLI8woMWrSx52GxdNMgDdlxY huang@localhost.localdomain
The key’s randomart image is:
±–[RSA 3072]----+
| .o… Eo |
| … + |
| oo. |
| . = |
| + . .S |
| o o o.o . |
|.++o=.=.o |
|+.o*+*BX |
|oo .*OO |
±—[SHA256]-----+
[huang@localhost ~]$ cd /home/huang/.ssh # 进入存放密钥对的目录
[huang@localhost .ssh]$ ls
id_rsa id_rsa.pub known_hosts

id_rsa.pub 公钥
id_rsa 私钥
known_hosts文件 --》存放连接过的服务器的公钥,主要用来后续传输数据进行加密使用


2. 上传密钥到服务器

ssh-copy-id user@server_ip

[huang@localhost .ssh]$ ssh-copy-id -p 2233 -i id_rsa.pub huang@192.168.100.151
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “id_rsa.pub”
The authenticity of host ‘[192.168.100.151]:2233 ([192.168.100.151]:2233)’ can’t be established.
ED25519 key fingerprint is SHA256:9kaGZcEP7F4zD26TR6NkPzwxqH0MJIwFDJrtrDbLIwU.
This key is not known by any other names
Are you sure you want to continue connecting (yes/no/[fingerprint])? 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
huang@192.168.100.151’s password:
 
Number of key(s) added: 1
 
Now try logging into the machine, with: “ssh -p ‘2233’ ‘huang@192.168.100.151’”
and check to make sure that only the key(s) you wanted were added.


3. 尝试使用密钥去连接

ssh -p [端口号] [用户名]@[服务器IP或域名]

[huang@localhost .ssh]$ ssh -p ‘2233’ ‘huang@192.168.100.151’
Welcome to Ubuntu 24.04 LTS (GNU/Linux 6.8.0-62-generic x86_64)
$
$ ls -a
. … .bash_logout .bashrc .cache feng .profile .ssh

.ssh 是存放客户端的公钥的文件–》/home/huang目录下

$ cd .ssh
$ ls
authorized_keys 存放公钥的文件

免密通道建立好了后,可以直接在另外一台服务器上执行命令和传输文件

[huang@rocky ~]$ ssh -p 2233 huang@192.168.100.151 mkdir ~/tang
[huang@rocky ~]$ scp -P 2233 sc.txt huang@192.168.100.151:~
sc.txt 100% 0 0.0KB/s 00:00

远程执行脚本

root@sanchuang:/nginx# ssh -p 22 root@192.168.100.148 bash /root/install_nginx.sh
root@192.168.100.148’s password:
未安装软件包 nginx
没有安装nginx


4. 双向信任关系

双向信任关系

在ubuntu的机器上也创建密钥对

root@sanchuang:/home/huang# su - huang
$ ls -a
. … .bash_logout .bashrc .profile

  1. 生成密钥对

$ ssh-keygen
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/huang/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/huang/.ssh/id_ed25519
Your public key has been saved in /home/huang/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:beh1w6/XXT56niyqOPbY73EEpvzY09Rno9PaWIA1McA huang@sanchuang
The key’s randomart image is:
±-[ED25519 256]–+
| …o |
| E o |
| o o |
| .oo.+ o |
| So+.++ oo|
| . o+.+o+.+|
| … = =.=o|
| o+ +oOo=|
| .oo+++o===.|
±—[SHA256]-----+
$ ls -a
. … .bash_logout .bashrc .cache feng .profile .ssh
$ cd .ssh
$ ls
authorized_keys id_ed25519 id_ed25519.pub

  1. 上传密钥到服务器

$ ssh-copy-id -p 2233 -i id_ed25519.pub huang@192.168.100.150
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “id_ed25519.pub”
The authenticity of host ‘[192.168.100.150]:2233 ([192.168.100.150]:2233)’ can’t be established.
ED25519 key fingerprint is SHA256:NmhJilR0o4EB/9J/Wf3WVEVR31RGiT5RO2TA0nuJkIw.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? 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
huang@192.168.100.150’s password:
 
Number of key(s) added: 1
 
Now try logging into the machine, with: “ssh -p 2233 ‘huang@192.168.100.150’”
and check to make sure that only the key(s) you wanted were added.

  1. 验证免密通道是否建立成功

$ ssh -p 2233 ‘huang@192.168.100.150’
Last login: Tue Jun 24 11:37:02 2025

openssh主配置文件里已经定义好了,存放对方的公钥的文件

[root@localhost ssh]# cat /etc/ssh/sshd_config
AuthorizedKeysFile .ssh/authorized_keys

[huang@localhost ~]$ cd .ssh
[huang@localhost .ssh]$ ls
authorized_keys id_rsa id_rsa.pub known_hosts known_hosts.old
[huang@localhost .ssh]$ cat authorized_keys 存放公钥的文件
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIE2x3kDX2+oL5ADxE22tHPf8lHXiVz4wxcDZrJn+ikhi huang@sanchuang
[huang@localhost .ssh]$ su - root
Password:
Last login: Tue Jun 24 09:46:51 CST 2025 on pts/0


5. 如何同时批量完成在多台机器上同时安装软件或者备份文件?

定义一个文件server.txt 存放服务器的ip地址

[root@rocky-1 backup]# vim server.txt
192.168.100.152
192.168.100.151

编写下发脚本文件的脚本

[root@rocky-1 backup]# vim deploy.sh
#!/bin/bash
for i in $(cat /backup/server.txt)
do
   scp -P 2233 /backup/backup.sh root@$i:/root && echo “下发脚本 $i backup.sh 成功”
done

执行下发文件的脚本

[root@rocky-1 backup]# bash deploy.sh
backup.sh 100% 341 460.2KB/s 00:00
下发脚本 192.168.100.152 backup.sh 成功
backup.sh 100% 341 198.7KB/s 00:00
下发脚本 192.168.100.151 backup.sh 成功

编写在远程服务器上执行的脚本

[root@rocky-1 backup]# vim run_backup.sh
#!/bin/bash
for i in $(cat /backup/server.txt)
do
   ssh -p 2233 root@$i bash /root/backup.sh && echo “$i backup ok”
done

执行脚本

[root@rocky-1 backup]# bash run_backup.sh
tar: Removing leading `/’ from member names
tar: Removing leading `/’ from hard link targets
192.168.100.152 backup ok
tar: Removing leading `/’ from member names
tar: Removing leading `/’ from hard link targets
192.168.100.151 backup ok


六、传输文件

cp 复制,是在本机里复制,从一个文件夹到另外一个文件夹

scp 跨主机的复制,可以从一台服务器里复制文件到另外一台服务器
底层使用ssh通道去传输数据

编写一个安装脚本用来安装nginx软件
脚本: install_nginx.sh
功能: 检测nginx是否已经安装,安装好了,是否启动

nginx 是一个web服务器软件

root@sanchuang:/nginx# vim  install_nginx.sh
#!/bin/bash
# 安装 nginx 的脚本,在rocky  linux  9.4
if  rpm  -q nginx ;thenecho  "已经安装了nginx"service nginx restartif  pidof  nginx ;thenecho "nginx进程已经运行"elseecho  "nginx进程没有启动"fi
elseecho  "没有安装nginx"# 安装  nginx yum  install  nginx -y# start  nginxservice  nginx  start# 检查 nginx 进程if  pidof  nginx ;thenecho "nginx进程已经运行"elseecho  "nginx进程没有启动"fi
fi

1. 复制本机上的文件到远程

scp 源文件 目的地

root@sanchuang:/nginx# scp /nginx/install_nginx.sh root@192.168.100.148:/root
The authenticity of host ‘192.168.100.148 (192.168.100.148)’ can’t be established.
ED25519 key fingerprint is SHA256:ZQJtCnkYy+/RyRbQkwr/0Kub52T3+TqxNWizhQ4D9zY.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added ‘192.168.100.148’ (ED25519) to the list of known hosts.
root@192.168.100.148’s password:
install_nginx.sh 100% 520 194.0KB/s 00:00


2. 复制远程服务器上的文件到本机

root@sanchuang:/nginx# scp root@192.168.100.148:/etc/passwd .
root@192.168.100.148’s password:
passwd 100% 954 700.6KB/s 00:00
root@sanchuang:/nginx# ls
install_nginx.sh passwd

复制文件夹需要接 -r 选项

root@sanchuang:/nginx# scp -r root@192.168.100.148:/boot .
root@192.168.100.148’s password:
device.map 100% 68 68.6KB/s 00:00
adler32.mod 100% 1492 95.4KB/s 00:00
affs.mod 100% 7120 1.2MB/s 00:00
afs.mod 100% 9012 624.3KB/s 00:00


总结

SSH(Secure Shell)服务是一种网络协议,主要用于在不安全的网络环境中为计算机之间的通信提供安全加密的连接

SSH 服务的核心价值在于 “安全的远程连接与管理”,通过加密技术解决了明文传输的安全隐患,同时提供灵活的认证和控制机制,成为网络时代远程操作、系统运维、数据传输的基础协议

http://www.dtcms.com/wzjs/74759.html

相关文章:

  • 丰县网站建设搜索词分析工具
  • 专业做网站建设 昆山竞价托管公司排名
  • 免费建站有哪些网站汕头seo代理商
  • 网站域名必须备案吗aso推广方案
  • 做非洲出口的网站seo网络推广什么意思
  • wordpress 注册不了seo站长网
  • 做网站 信科网络搜索热度查询
  • 相同网站名沈阳市网站
  • 杭州知名的企业网站建设策划怎么做推广和宣传
  • 廊坊住建委网站首页宁波关键词优化平台
  • 网站建设价格对比分析百度seo教程
  • php网站开发需要多久数字营销服务商seo
  • 海外网站服务器网址网站制作详细流程
  • 站群系列服务器做视频网站销售管理系统
  • 网站的二级页面在哪里做丽水网站seo
  • 个人博客网站制作流程专业的营销团队哪里找
  • 电子商务网站建设 上海网络营销平台
  • 重庆集团网站建设买淘宝店铺多少钱一个
  • 源码之家进不去搜索引擎优化搜索优化
  • 网站滚动效果怎么做的搜索引擎优化的方法有哪些?
  • 基于阿里云的网站开发搜索引擎分析论文
  • html 网站首页百度推广业务电话
  • 怎样做网站收录网站营销网站营销推广
  • 重庆北碚网站制作seo技术培训宁波
  • 做食品那些网站好sku电商是什么意思
  • 网站做研究生毕业论文站点查询
  • wordpress架构青岛自动seo
  • 帮别人做ppt赚钱的网站今天的新闻 最新消息摘抄
  • 网站开发保密合同中国站免费推广入口
  • 企业网站建设套餐上海网站流量查询站长之家