当前位置:   article > 正文

Linux——ssh服务_linux ssh

linux ssh

ssh是什么?

ssh是一个远程登录相关的协议,主要的用途是登陆到远程电脑中执行命令。SSH 叫安全外壳协议(Secure Shell),是一种加密的网络传输协议,可在不安全的网络中为网络服务提供安全的传输环境。
ssh服务: 实现远程登录和远程控制,在网络上传输的数据是加密的

SSH 是一种协议, 存在多种实现,既有商业实现,也有开源实现(OSSH,OpenSSH)。本文使用的自有软件 OpenSSH, 毕竟它是目前最流行的 SSH 实现,而且是所有操作系统的默认组件。 

Openssh 是一个安全的shell,是一个远程登录的程序,帮助我们在远程的机器上执行命令,控制远程机器

SSH服务--》守护进程(一直在内存里运行的进程)--》sshd 

安装

默认linux系统是安装,centos系统默认安装

Ubuntu需要指定安装

  1. [root@localhost lianxi]# rpm -qa|grep openssh
  2. openssh-clients-7.4p1-21.el7.x86_64 --》提供工具的
  3. openssh-7.4p1-21.el7.x86_64 --》提供工具的
  4. openssh-server-7.4p1-21.el7.x86_64 --》提供ssh 服务的

查看sshd的进程树

  1. [root@localhost lianxi]# pstree -p|grep sshd
  2. |-sshd(1179)-+-sshd(12759)---bash(12766)
  3. | `-sshd(15353)---bash(15360)-+-grep(18956)

查看sshd是否运行?(对这些命令不熟悉的可以看另一篇文档:Linux系统性能监控

1.看进程
    ps  aux
    pidof  
    pstree
 

  1. [root@sanchuang ~]# ps aux|grep sshd
  2. root 1082 0.0 0.2 112900 4328 ? Ss 09:42 0:00 /usr/sbin/sshd -D 主进程
  3. root 1602 0.0 0.3 161512 6096 ? Ss 09:42 0:00 sshd: root@pts/0 连接到linux系统里的进程
  4. root 1656 0.0 0.0 112824 984 pts/0 S+ 10:01 0:00 grep --color=auto sshd
  5. [root@sanchuang ~]#

2.看端口

  1. [root@sanchuang ~]# netstat -anplut 查看本机开放了哪些端口,允许其他人连接过来
  2. Active Internet connections (servers and established)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1664/sshd
  5. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1248/master
  6. tcp 0 0 192.168.2.197:22 192.168.2.123:60946 ESTABLISHED 1665/sshd: root@pts
  7. tcp 0 36 192.168.2.197:22 192.168.2.123:60447 ESTABLISHED 1602/sshd: root@pts
  8. tcp6 0 0 :::22 :::* LISTEN 1664/sshd
  9. tcp6 0 0 ::1:25 :::* LISTEN 1248/master
  10. udp 0 0 127.0.0.1:323 0.0.0.0:* 713/chronyd
  11. udp 0 0 192.168.2.197:45460 116.203.151.74:123 ESTABLISHED 713/chronyd
  12. udp6 0 0 ::1:323 :::* 713/chronyd

127.0.0.1   代表本机的地址 ---》自己,任何一台机器都有这个地址
192.168.2.197 就是ens33接口配置的ip地址
0.0.0.0 代表本机的任意ip,所有的ip地址
LISTEN 表示监听,理解为已经打开了的端口
ESTABLISHED 表示已经建立连接,通过网络有人已经连接到你的机器了

  1. [root@sanchuang ~]# w 查看有哪些人登录进入我们的linux系统了
  2. 10:13:44 up 32 min, 3 users, load average: 0.01, 0.03, 0.05
  3. USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT
  4. root tty1 09:42 31:12 0.02s 0.02s -bash
  5. root pts/0 192.168.2.123 09:42 7:44 0.13s 0.13s -bash
  6. root pts/1 192.168.2.123 10:04 0.00s 0.05s 0.00s w
  1. [root@sanchuang ~]# ss -anplut
  2. Netid State Recv-Q Send-Q Local Address:Port Peer Address:Port
  3. udp UNCONN 0 0 127.0.0.1:323 *:* users:(("chronyd",pid=713,fd=5))
  4. udp UNCONN 0 0 [::1]:323 [::]:* users:(("chronyd",pid=713,fd=6))
  5. tcp LISTEN 0 128 *:22 *:* users:(("sshd",pid=1664,fd=3))
  6. tcp LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1248,fd=13))
  7. tcp LISTEN 0 128 [::]:22 [::]:* users:(("sshd",pid=1664,fd=4))
  8. tcp LISTEN 0 100 [::1]:25 [::]:* users:(("master",pid=1248,fd=14))
  1. [root@sanchuang ~]# yum install lsof -y 安装lsof软件
  2. [root@sanchuang ~]# lsof -i:22
  3. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  4. sshd 1602 root 3u IPv4 21189 0t0 TCP sanchuang:ssh->192.168.2.123:60447 (ESTABLISHED)
  5. sshd 1664 root 3u IPv4 24115 0t0 TCP *:ssh (LISTEN)
  6. sshd 1664 root 4u IPv6 24117 0t0 TCP *:ssh (LISTEN)
  7. sshd 1665 root 3u IPv4 24982 0t0 TCP sanchuang:ssh->192.168.2.123:60946 (ESTABLISHED)
  1. [root@localhost lianxi]# rpm -qi openssh-server
  2. Name : openssh-server
  3. Version : 7.4p1
  4. Release : 21.el7
  5. Architecture: x86_64
  6. Install Date: 20221028日 星期五 212156
  7. Group : System Environment/Daemons
  8. Size : 993586
  9. License : BSD
  10. Signature : RSA/SHA256, 20190823日 星期五 053729秒, Key ID 24c6a8a7f4a80eb5
  11. Source RPM : openssh-7.4p1-21.el7.src.rpm
  12. Build Date : 20190809日 星期五 094049
  13. Build Host : x86-01.bsys.centos.org
  14. Relocations : (not relocatable)
  15. Packager : CentOS BuildSystem <http://bugs.centos.org>
  16. Vendor : CentOS
  17. URL : http://www.openssh.com/portable.html
  18. Summary : An open source SSH server daemon
  19. Description :
  20. OpenSSH is a free version of SSH (Secure SHell), a program for logging
  21. into and executing commands on a remote machine. This package contains
  22. the secure shell daemon (sshd). The sshd daemon allows SSH clients to
  23. securely connect to your SSH server.
  24. [root@localhost lianxi]#

配置文件目录:/etc/ssh
配置文件的作用就是给应用程序传递参数
sshd_config --》服务器端的配置文件
ssh_config --》客户端命令的配置文件
进程名字: sshd  -->sshd daemon
占用的端口号是:22


sshd_config  --》sshd进程的配置文件
#Port 22  告诉sshd进程占用22号端口


端口号:
    1~1024 都是一些(经典)常见的服务使用的,很多操作系统预留的端口
    端口的范围:0~65535  1~65536

  1. [root@localhost ssh]# service sshd restart 重启sshd服务
  2. Redirecting to /bin/systemctl restart sshd.service

关闭selinux服务,这个服务会加强linux的安全性,当我们去修改sshd服务的端口号的时候,会被拒绝

配置

目录 /etc/ssh

配置文件:给进程传递参数的

  1. [root@nameserver ssh]# cd /etc/ssh/
  2. sshd_config --》服务器端的配置文件 server --》进程:sshd
  3. ssh_config --》ssh命令使用的配置文件 client --》进程:ssh
  4. ssh_host_ecdsa_key.pub --》ecdsa加密算法的公钥
  5. ssh_host_ecdsa_key --》ecdsa加密算法的私钥

日志文件 

/var/log/secure

rsyslogd 是专门给linux系统和其他的程序记日志的服务

日志进行了分类:
    authpriv :与权限认证相关的
    crond : 计划任务
    mail: 邮件
    kern: 内核相关的
    local7 : boot相关的
    等

sshd 它自己没有日志记录功能,它外包给了rsyslogd

关闭selinux和防火墙 

开机不要启动

关闭防火墙(开机不启动):  systemctl disable firewalld

关闭selinux:

selinux 是linux系统里的一套安全机制,用来保护linux系统的安全,会限制进程去做某些对安全有威胁的事情

永久修改selinux的策略为disabled

  1. root@localhost ssh]# vim /etc/selinux/config
  2. SELINUX=disabled
  3. [root@localhost ssh]# getenforce 查看selinux的状态
  4. Enforcing
  5. [root@localhost ssh]# setenforce 0 临时调整selinux的策略为宽容模式
  6. [root@localhost ssh]# getenforce
  7. Permissive

修改sshd进程的端口号和禁止root用户登录

修改端口号为2244端口 (在配置文件里修改 /etc/ssh/sshd_config)
Port 2244
禁用root用户登录
PermitRootLogin no


添加欢迎横幅文件
Banner /etc/ssh/welcome.txt

  1. [root@fengdeyong ssh]# cat /etc/ssh/welcome.txt
  2. welcome to sanchuang ssh server
  1. [root@localhost ssh]# getenforce 查看selinux的状态
  2. Enforcing 强制执行的
  3. [root@localhost ssh]# setenforce 0 临时关闭selinux 1 开启selinux
  4. [root@localhost ssh]# getenforce
  5. Permissive 宽容模式
  1. [root@localhost ssh]# netstat -anplut 查看本机开放了哪些端口
  2. Active Internet connections (servers and established)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp 0 0 0.0.0.0:3001 0.0.0.0:* LISTEN 20257/sshd
  5. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1359/master
  6. tcp 0 0 192.168.227.144:22 192.168.227.1:58818 ESTABLISHED 12759/sshd: root@pt
  7. tcp 0 0 192.168.227.144:22 192.168.227.1:59598 ESTABLISHED 15353/sshd: root@pt
  8. tcp6 0 0 :::3001 :::* LISTEN 20257/sshd
  9. tcp6 0 0 ::1:25 :::* LISTEN 1359/master
  10. udp 0 0 0.0.0.0:68 0.0.0.0:* 12585/dhclient
  11. udp 0 0 127.0.0.1:323 0.0.0.0:* 842/chronyd
  12. udp 0 0 192.168.227.144:45793 31.3.135.232:123 ESTABLISHED 842/chronyd
  13. udp6 0 0 ::1:323 :::* 842/chronyd
  14. [root@localhost ssh]#

0.0.0.0  代表任意ip地址
 

  1. [root@localhost ssh]# netstat -anplut|grep LISTEN
  2. tcp 0 0 0.0.0.0:3001 0.0.0.0:* LISTEN 20257/sshd
  3. tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 1359/master
  4. tcp6 0 0 :::3001 :::* LISTEN 20257/sshd
  5. tcp6 0 0 ::1:25 :::* LISTEN 1359/master

关闭防火墙,因为防火墙开启不允许3001端口随便进入的

  1. [root@localhost lianxi]# service firewalld stop
  2. Redirecting to /bin/systemctl stop firewalld.service

需要关闭selinux和Firewalld防火墙服务,不然ssh不能正常的运行和访问。

ssh远程连接不上,如何排查??

1.sshd进程是否开启

  1. [root@sanchuang ssh]# service sshd start
  2. Redirecting to /bin/systemctl start sshd.service
  3. [root@sanchuang ssh]# service sshd status
  4. Redirecting to /bin/systemctl status sshd.service
  5. ● sshd.service - OpenSSH server daemon
  6. Loaded: loaded (/usr/lib/systemd/system/sshd.service; enabled; vendor preset: enabled)
  7. Active: active (running) since 一 2022-11-07 11:20:52 CST; 7s ago
  8. Docs: man:sshd(8)
  9. man:sshd_config(5)
  10. Main PID: 2001 (sshd)
  11. CGroup: /system.slice/sshd.service
  12. └─2001 /usr/sbin/sshd -D
  13. 1107 11:20:52 sanchuang systemd[1]: Starting OpenSSH server daemon...
  14. 1107 11:20:52 sanchuang sshd[2001]: Server listening on 0.0.0.0 port 22.
  15. 1107 11:20:52 sanchuang systemd[1]: Started OpenSSH server daemon.
  16. 1107 11:20:52 sanchuang sshd[2001]: Server listening on :: port 22.

2.查看ip地址是否发生改变

 公司内部一般服务器都会使用固定ip,不会变化的

3.防火墙或者selinux

    service firewalld stop
    getenforce  查看selinux的状态
    setenforce 0  临时关闭
    永久关闭 vim  /etc/selinux/config
    SELINUX=disabled

4.查看端口号

查看主配置文件里的端口号和是否禁用root用户

远程登陆的命令 

ssh的命令的使用
1.只是登陆

  1. [root@manager161 .ssh]# ssh root@192.168.0.180 --》推荐
  2. [daijun@manager161 ~]$ ssh 192.168.0.180 不指定登陆用户,默认会使用当前的用户名远程连接
  3. [daijun@manager161 ~]$ ssh -l root 192.168.0.180
  4. root@192.168.0.180's password:
  5. Last login: Sat Jun 26 11:27:12 2021 from 192.168.0.163
  6. [root@www ~]# exit 退出
  7. 登出
  8. Connection to 192.168.0.180 closed.
  9. [daijun@manager161 ~]$

2.运行远程机器上的脚本或者命令

  1. [root@manager161 .ssh]# ssh 192.168.0.180  mkdir  /luogg{1..10}
  2. [root@manager161 .ssh]# ssh 192.168.0.180  sudo mkdir  /danyh{1..10}
  3. [root@manager161 .ssh]# 
  4. [root@manager161 .ssh]# ssh 192.168.0.180  sudo bash /lianxi/scan_lan_ip.sh

-p  指定端口号

sftp命令

SFTP(Secure File Transfer Protocol,安全文件传输协议)是一种基于可靠数据流(data stream),提供文件存取和管理的网络传输协议

因为 SFTP 是基于 SSH 协议的,所以默认的身份认证方法与 SSH 协议保持一致。通常我们使用 SSH Key 来进行连接,如果你已经可以使用 SSH 连接到远程服务器上,那么可以使用以下命令来连接 SFTP:

sftp user_name@remote_server_address[:path]

如果远程服务器自定义了连接的端口,可以使用 -P 参数:

sftp -P remote_port user_name@remote_server_address[:path]

从远程服务器拉取文件

使用 get 命令可以从远程服务器拉取文件到本地:

sftp> get remoteFile [newName]

如果不指定 newName,将使用和远程服务器相同的文件名。

使用 -r 参数可以拉取整个目录:

sftp> get -r remoteDirectory

从本地上传文件到服务器

使用 put 命令可以从本地上传文件到服务器:

sftp> put localFile

同样的,可以使用 -r 参数来上传整个目录,但是有一点要注意,如果服务器上不存在这个目录需要首先新建

  1. sftp> mkdir folderName
  2. sftp> put -r folderName

scp命令

scp命令推送文件在linux服务器之间

  1. [root@manager161 lianxi9]# scp mkdir.sh root@192.168.0.180:/root
  2. mkdir.sh 100% 42 10.9KB/s 00:00
  3. [root@manager161 lianxi9]#
  4. [root@manager161 lianxi9]# scp root@192.168.0.180:/etc/passwd .
  5. passwd 100% 1792 953.1KB/s 00:00
  6. [root@manager161 lianxi9]# ls
  7. mkdir.sh passwd
  8. [root@manager161 lianxi9]#
  9. [root@manager161 lianxi9]# scp -r root@192.168.0.180:/boot . 复制文件需要接-r选项
  10. [root@manager161 lianxi9]# cat mkdir.sh 是具体需要做什么工作的脚本
  11. for i in {1..10}
  12. do
  13. mkdir -p feng$i
  14. done
  15. [root@manager161 lianxi9]# cat ctrl_mysql.sh 实现传递脚本到服务器,然后执行传递过去脚本
  16. for i in {1..100}
  17. do
  18. scp /lianxi9/mkdir.sh root@192.168.0.$i:/root
  19. ssh root@192.168.0.$i bash /root/mkdir.sh
  20. done

2台机器之间建立免密通道

密钥:
    1.公钥   可以公开的钥匙--》给其他人使用的 public key
    2.私钥   私人持有不公开的钥匙 --》自己使用  --》private key

    公钥和私钥是成对出现的,2个不同的钥匙

对称加密: 2边使用的秘钥是一样的
非对称加密 : 2边使用的钥匙不一样

service  firewalld  stop  立即停止防火墙服务
systemctl  disable  firewalld  设置防火墙服务开机不要启动

[root@sanchuang ~]# vim  /etc/ssh/sshd_config 
启用运行root用户登录
PermitRootLogin yes

[root@sanchuang ~]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service
 

2台机器默认是允许root用户登录,打开端口是22号

client: 192.168.223.205
server: 192.168.223.206

拓展:数字签名的过程

免密通道建立步骤如下:

单向信任关系

1.在主机①上使用root用户登录,产生公钥和私钥对

  1. [root@web1 ~]# ssh-keygen
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase): 注意不要设置密码
  5. Enter same passphrase again:
  6. Your identification has been saved in /root/.ssh/id_rsa.
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. SHA256:ZvOCnkzIgPloPf1NQG7G9i5NSUCGOxQ8JKUWnkSUCu8 root@web1
  10. The key's randomart image is:
  11. +---[RSA 2048]----+
  12. | o*=++o |
  13. |. o.==.. |
  14. |...=. o.. |
  15. | +o o+ . |
  16. |o.. .BS . |
  17. | oEo..++o= |
  18. |...oo.o +o. |
  19. |. .+.o+o |
  20. | +..o |
  21. +----[SHA256]-----+

查看生成的密钥对

  1. [root@web1 ~]# cd ~/.ssh
  2. [root@web1 .ssh]# ls
  3. authorized_keys boot hosts id_rsa id_rsa.pub known_hosts
  4. [root@web1 .ssh]# ll
  5. 总用量 20
  6. -rw-------. 1 root root 397 117 2022 authorized_keys
  7. dr-xr-xr-x 4 root root 246 514 15:13 boot
  8. -rw-r--r-- 1 root root 158 514 15:12 hosts
  9. -rw------- 1 root root 1679 514 16:07 id_rsa 密钥对
  10. -rw-r--r-- 1 root root 391 514 16:07 id_rsa.pub
  11. -rw-r--r-- 1 root root 184 514 15:06 known_hosts

2.上传公钥到主机②

(如果端口号不是22的话要指定端口号,例如: ssh-copy-id -p 3001 root@192.168.223.206)

方法一:生成authorized_keys文件  

cat ~/.ssh/id_dsa.pub >> ~/.ssh/authorized_keys

生成authorized_keys文件完成后主机①对自己的所有操作都是免密了,同理我们只要将authorized_keys文件拷贝至主机②中,那么主机①对主机②的操作也可以达到免密效果 

 拷贝authorized_keys到主机②

先在主机②中执行命令(该命令的目的是为了生成.ssh目录)

ssh-keygen -t dsa -P ‘’ -f ~/.ssh/id_dsa

在主机①中执行命令将authorized_keys拷贝至主机②:

scp ~/.ssh/authorized_keys root@centos2:~/.ssh

方法二:ssh-copy-id 命令会自动将公钥上传到主机②的/root/.ssh/authorized_keys

  1. [root@web1 .ssh]# ssh-copy-id root@192.168.223.206
  2. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
  3. The authenticity of host '192.168.223.206 (192.168.223.206)' can't be established.
  4. ECDSA key fingerprint is SHA256:tv3msGixbmim9N4hYLyqOqiajlci5JvPLv2gb4P7/3g.
  5. ECDSA key fingerprint is MD5:04:b8:d8:6a:71:06:26:63:0a:1f:d3:67:0d:3a:07:2a.
  6. Are you sure you want to continue connecting (yes/no)? yes
  7. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  8. /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
  9. root@192.168.223.206's password:
  10. Number of key(s) added: 1
  11. Now try logging into the machine, with: "ssh 'root@192.168.223.206'"
  12. and check to make sure that only the key(s) you wanted were added.

3.测试密钥认证,免密通道是否创建成功 (如果端口号不是22的话要指定端口号,例如: ssh  -p ‘3001’  ‘root@192.168.223.206’)

第1次ssh过去或者ssh-copy-id都是需要输入yes接收公钥,然后再要求输入root的密码

  1. [root@web1 .ssh]# ssh 'root@192.168.223.206'
  2. Last login: Sun May 14 16:01:43 2023
  3. [root@server ~]#

 退出远程登陆连接,返回本地的Linux系统命令终端,再次查看.ssh目录下的文件

 我们发现原来空空如也的.ssh目录下know_hosts文件

know_hosts : 已知的主机公钥清单

ssh命令远程连接不同服务器时可以选择接受到不同的公钥,会将这些主机的公钥都保存在这里,就是我们第一次连接远程Liunx主机的时候 ,提示给我们的一个授权信息,(就是步骤二中我们选填yes的那一项),只要我们选择yes同意授权后,该授权信息就会保存在本地主机,这样我们下一次连接该远程主机的时候就不会在终端提示是否授权的信息,直接输入密码即可!

双向信任关系

还要在服务器上执行一遍

1.生成密钥对

  1. [root@server .ssh]# ssh-keygen 
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa): 
  4. Enter passphrase (empty for no passphrase):  注意不要设置密码
  5. Enter same passphrase again: 
  6. Your identification has been saved in /root/.ssh/id_rsa.
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. SHA256:O4bm0hFhbiwfraagbg6GFxMB5QOV6HQYg1oLkQT4IaE root@server
  10. The key's randomart image is:
  11. +---[RSA 2048]----+
  12. |@%*.             |
  13. |O==o  o          |
  14. |E==o + o         |
  15. |..oo. * .        |
  16. |  o  + +S        |
  17. |. .o  =. .       |
  18. |oo.. +o.+        |
  19. |+o  oo.. .       |
  20. |+o   ..          |
  21. +----[SHA256]-----+

2.上传公钥到主机①

  1. [root@server .ssh]# ssh-copy-id root@192.168.223.205
  2. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
  3. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  4. /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
  5. root@192.168.223.205's password:
  6. Number of key(s) added: 1
  7. Now try logging into the machine, with: "ssh 'root@192.168.223.205'"
  8. and check to make sure that only the key(s) you wanted were added.

3.验证

  1. [root@server .ssh]# ssh root@192.168.223.205
  2. Last login: Sun May 14 16:21:02 2023 from 192.168.223.206
  3. [root@web1 ~]#


建立了免密通道后,有什么好处?

1.可以直接在远程机器上执行命令,不需要输入密码

2.可以直接传输文件,在2台机器直接

  1. [root@ssh-server backup]# scp -P 3001 root@192.168.2.197:/etc/passwd .
  2. 源文件 目的地
  3. passwd 100% 4385 1.6MB/s 00:00
  4. [root@ssh-server backup]# ls
  5. passwd

经常可以先编写好脚本,然后scp脚本到其他的服务器,然后再去ssh执行脚本就可以了

 举例演示

1.编写好脚本mkdir.sh

  1. [root@ssh-server backup]# ls
  2. mkdir.sh passwd sc
  3. [root@ssh-server backup]# vim mkdir.sh
  4. #!/bin/bash
  5. mkdir -p /liujun
  6. cd /liujun
  7. for i in {1..20}
  8. do
  9. touch liu$i.txt
  10. mkdir liujun$i
  11. done

2.scp脚本到另外的机器的/root目录下

  1. [root@ssh-server backup]# scp -P 3001 mkdir.sh root@192.168.2.197:/root
  2. mkdir.sh 100% 100 42.5KB/s 00:00

3.在远程服务器上执行脚本

  1. [root@ssh-server backup]# ssh root@192.168.2.197 bash /root/mkdir.sh
  2. [root@ssh-server backup]# ssh root@192.168.2.197 ls /liujun

know_hosts文件

在~/.ssh

客户机使用ssh第一次连接服务器时
将服务器上sshd守护进程的公钥复制到本地
存放到本地~/.ssh/known_hosts文件中
每行存放一台服务器的公钥
用来验证服务器的身份

第一次连接过去服务器发送过来的

数据加密+服务器和客户端身份识别(数字签名)

authorized_keys文件

在~/.ssh

存放用来进行合法身份验证 证明你是linux系统的合法用户

登录linux使用

数据加密+服务器和客户端身份识别(数字签名)

删除know_hosts文件和/root/.ssh/authorized_keys有什么破坏作用? 

认证方式:1.密码认证  2.密钥认证

密钥认证的过程

如何在xshell里生成密钥对,然后在xshell里使用密钥认证登录linux服务器?

如何在xshell里使用密钥登录你的linux 云服务器(腾讯云,阿里云等)?

 参考下面这篇博客:

https://blog.csdn.net/fengyatao00/article/details/121371408

如何做的让内外的服务器只允许堡垒机/跳板机访问?

    1.iptables
    2.tcp wrappers

SSH使用TCP Wrappers实现访问控制
主要配置文件
/etc/hosts.allow   --》白名单
/etc/hosts.deny   --》黑名单

TCP Wrappers的访问控制原则
首先检查 hosts.allow 文件,若找到相匹配的策略,则允许访问
否则继续检查 hosts.deny 文件,若找到相匹配的策略,则拒绝访问
如果两个文件中都没有相匹配的策略,则允许访问

  1. [root@sanchuang ~]# cat /etc/hosts.deny
  2. sshd:all
  3. [root@sanchuang ~]# cat /etc/hosts.allow
  4. sshd:192.168.2.*,114.11.11.1
  5. [root@sanchuang ~]#
  6. [root@sanchuang ~]# tail -f /var/log/secure
  7. Nov 8 16:22:39 sanchuang sshd[3701]: refused connect from 192.168.2.198 (192.168.2.198)
  8. Nov 8 16:22:40 sanchuang sshd[3702]: refused connect from 192.168.2.117 (192.168.2.117)
  9. [root@ssh-server .ssh]# ssh -p 3001 root@192.168.2.197
  10. ssh_exchange_identification: read: Connection reset by peer
  11. [root@ssh-server .ssh]#
  12. 不需要刷新服务,立马生效

关闭窗口,ssh远程执行的脚本是否继续执行?


    默认不运行,会停止
    因为HUP信号  ,当父进程关闭的时候,会给子进程发生hup信号,收到hup信号的进程会被内核杀死
   

信号可以被屏蔽

使用nohup去屏蔽hup信号
& 将这个命令放到后台去运行

ssh连接不上的排错思路?

排错思路:
    1.检查进程或者服务是否开启
        ps  aux|grep sshd
        service sshd status

    2.检查端口
        netstat -anplut
        查看sshd  listen状态是否存在

    3.看日志看错误提醒

[root@sanchuang ~]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service
Job for sshd.service failed because the control process exited with error code. See "systemctl status sshd.service" and "journalctl -xe" for details.
[root@sanchuang ~]#journalctl -xe
    4.网络是否通畅
        ping

     5. 查看tcp wrappers 

        /etc/hosts.allow
        /etc/hosts.deny

第1次ssh过去或者ssh-copy-id都是需要输入yes接收公钥,然后再要求输入root的密码
问题:
    需要脚本执行的时候,能自动帮助我们输入yes,然后自动输入密码

1. linux里expect 命令是可以实现交互操作,模拟人输入内容

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/461149
推荐阅读
相关标签
  

闽ICP备14008679号