• [linux]、ssh服务详解


    目录

    一、引入

    二、配置

    2.1 配置文件

    2.1.1 修改ssh服务的端口

    三、命令

    3.1 ssh认证方式

    四、建立免密通道

    4.1、ssh

    4.2、scp

    4.3、sftp

    五、加固服务

    六、signal


    一、引入

    1. ssh是什么?

    secure shell,建立在应用层基础上的安全协议

    2. 作用

    远程控制服务器,使用在Linux/unix系统

    3. 如何搭建ssh服务?

    OpenSSH是使用SSH协议进行远程登录的首要连接工具。它对所有通信进行加密,以消除窃听、连接劫持和其他攻击。此外,OpenSSH提供了大量的安全隧道功能、几种身份验证方法和复杂的配置选项。

    4. rpm(redhat package managment 红帽公司出品)

    Linux中的一个软件管理命令,在redhat/centos中使用

    -q 查询query

    a 所有

    rpm -qa 查询在本机已经安装的所有软件

    1. [root@nginx-kafka01 etc]# 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

    更新

    [root@nginx-kafka01 ~]# yum update openssh openssh-clients openssh-server

    5. 配置文件

    sshd_config 服务器端的进程的配置文件

    ssh 客户端命令的配置文件

    1. [root@nginx-kafka01 etc]# cd /etc/ssh
    2. [root@nginx-kafka01 ssh]# ls
    3. moduli sshd_config ssh_host_ecdsa_key.pub ssh_host_ed25519_key.pub ssh_host_rsa_key.pub
    4. ssh_config ssh_host_ecdsa_key ssh_host_ed25519_key ssh_host_rsa_key

    6. 如何知道ssh服务是否启动

    1. 进程

    1. [root@nginx-kafka01 ssh]# ps aux|grep sshd
    2. root 986 0.0 0.2 112900 4324 ? Ss 11:57 0:00 /usr/sbin/sshd -D
    3. root 1515 0.0 0.3 161512 6084 ? Ss 11:58 0:00 sshd: root@pts/0
    4. root 1845 0.0 0.0 112828 988 pts/0 S+ 17:18 0:00 grep --color=auto sshd

    2. 端口

    1. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
    2. sshd 986 root 3u IPv4 18119 0t0 TCP *:ssh (LISTEN)
    3. sshd 986 root 4u IPv6 18121 0t0 TCP *:ssh (LISTEN)
    4. sshd 1515 root 3u IPv4 20677 0t0 TCP nginx-kafka01:ssh->192.168.182.1:58405 (ESTABLISHED)
    1. [root@nginx-kafka01 ssh]# netstat -anplut |grep sshd
    2. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 986/sshd
    3. tcp 0 36 192.168.182.132:22 192.168.182.1:58405 ESTABLISHED 1515/sshd: root@pts
    4. tcp6 0 0 :::22 :::* LISTEN 986/sshd

    3. 直接访问

    4. 日志

    7. 启动ssh服务

    systemctl enable sshd 设置开机自启动

    systemctl start sshd 启动

    systemctl restart sshd 重启

    8. sshd名字由来 ==> ssh daemon

    守护进程:daemon:一直运行在内存中,除非人为停止

    二、配置

    2.1 配置文件

    查看配置文件

    1. [root@nginx-kafka01 ~]# cd /etc/ssh
    2. [root@nginx-kafka01 ssh]# vim sshd_config

    ===文件内容===

    >默认端口 前面的数字是在文件中的行号

    17 #Port 22

    18 #AddressFamily any

    >默认监听地址,IPV4,0.0.0.0表示本机任意IP地址

    19 #ListenAddress 0.0.0.0

    >IPV6

    20 #ListenAddress ::

    >是否允许root用户ssh登录

    38 #PermitRootLogin yes

    >是否限制密码登录

    65 PasswordAuthentication yes

    2.1.1 修改ssh服务的端口

    1. [root@nginx-kafka01 ssh]# vim /etc/ssh/sshd_config # Port 2299 将端口号改为2299
    2. [root@nginx-kafka01 ssh]# systemctl restart sshd # 重启ssh服务
    3. [root@nginx-kafka01 ssh]# netstat -antplu |grep 2299
    4. tcp 0 0 0.0.0.0:2299 0.0.0.0:* LISTEN 16257/sshd
    5. tcp6 0 0 :::2299 :::* LISTEN 16257/sshd

    > 修改端口后ssh服务无法启动,可能是因为开启了selinux

    1. getenforce #查看selinux状态
    2. ->enforcing:强制模式,permissive:宽容模式,disabled:关闭
    3. setenforce 0 #临时修改为宽容模式
    4. vim /etc/selinux/config ==> SELINUX=disabled #修改配置文件--永久修改

    > 在xshell开启新的会话连接连接不上,原因有2:(1)连接端口没有改为修改后的;(2)防火墙限制,需要关闭防火墙

    1. service firewalld stop # 关闭防火墙
    2. systemctl disabled firewalld # 关闭开机自启

    2.2 限制root用户登录ssh

    1. vim /etc/ssh/sshd_config
    2. PermitRootLogin yes修改为PermitRootLogin no

    不能远程连接,但可以先用普通用户连接,再su - root 切换到root用户

    原因:su不经过ssh,可以通过pstree -p查看进程树,可以看到pid

    如何断开这个连接:杀死对应的bash进程

    2.2.3 日志

    /var/log/secure

    > 显示Faild passwd :表示登录失败

    三、命令

    3.1 ssh认证方式

    1. 密码认证

    /etc/passwd + /etc/shadow

    ssh服务依赖Linux系统的用户名和密码

    2. 密钥认证

    公钥(public key)和私钥(private key)是一对文件,文件里面是一段字符串,用来给加密算法提供参数。

    公钥存储路径:/root/.ssh/know_hosts

    将服务器上的sshd守护进程的公钥复制到本地,每行存放一台服务器的公钥,用来验证服务器的身份。

    四、建立免密通道

    A:192.168.29.155

    B:192.168.29.128

    root - root

    第一步:生成密钥对,在A机器上使用root用户生成密钥对

    1. [root@nginx-kafka02 .ssh]# ssh-keygen -t rsa # 选择不同的加密算法
    2. # 这里直接敲击回车就行了
    3. Generating public/private rsa key pair.
    4. Enter file in which to save the key (/root/.ssh/id_rsa):
    5. Enter passphrase (empty for no passphrase):
    6. Enter same passphrase again:
    7. [root@nginx-kafka02 .ssh]# cd /root/.ssh
    8. [root@nginx-kafka02 .ssh]# ls
    9. id_rsa id_rsa.pub known_hosts
    10. [root@nginx-kafka02 .ssh]# ll
    11. 总用量 12
    12. -rw-------. 1 root root 1679 727 16:46 id_rsa # 这个私钥只有自己才能使用,千万不要动它的权限
    13. -rw-r--r--. 1 root root 400 727 16:46 id_rsa.pub
    14. -rw-r--r--. 1 root root 731 727 12:00 known_hosts

    第二步:上传公钥

    有scp和ssh-copy-id两种方式上传

    1. [root@nginx-kafka02 .ssh]# ssh-copy-id -i id_rsa.pub root@192.168.29.128 # -i,指定公钥文件
    2. # 这条命令相比于scp的要指定公钥路径,可以自动在B机器上生成对应的公钥路径

    检查

    1. [root@nginx-kafaka03 ~]# cd ~/.ssh
    2. [root@nginx-kafaka03 .ssh]# ls
    3. authorized_keys known_hosts

    authorized_keys是用来linux系统登录使用的

    known_hosts是第一次连接服务器发送过来的东西

    第三步:测试

    1. [root@nginx-kafka02 .ssh]# ssh 192.168.29.128
    2. Last login: Wed Jul 27 14:52:44 2022 from 192.168.29.1
    3. # 我们现在能够使用A机器连接B机器不需要密码,但是B机器连接A机器需要密码,所以这是单向信任关系。

    4.1、ssh

    远程连接命令

    不指定登录用户时,默认使用当前用户

    1. # 不指定
    2. ssh 192.168.1.173
    3. # 指定
    4. ssh cali@192.168.1.173

    -p 指定端口号

    写脚本推荐使用ssh,建立免密通道后,无需使用密码

    1. # 登录的同时在远端机器上新建目录
    2. ssh cali@192.168.1.173 mkdir /home/cali/test -p

    退出:exit

    建立了免密通道之后,我们可以使用":ssh ip bash 命令",来操作别的机器运行代码

    4.2、scp

    远程文件传输命令

    1. # 将192.168.1.173的/etc/passwd文件复制到本机的/etc/ssh
    2. # 复制文件夹的时候需要接-r,递归复制目录
    3. [root@nginx-kafka01 ~]# scp cali@192.168.1.173:/etc/passwd /etc/ssh
    4. cali@192.168.1.173's password:
    5. passwd 100% 1217 81.0KB/s 00:00
    1. # 将本机/etc/ssh/passwd文件复制给192.168.1.173cali的家目录下
    2. [root@nginx-kafka01 ssh]# scp /etc/ssh/passwd cali@192.168.1.173:~
    3. cali@192.168.1.173's password:
    4. passwd 100% 1217 64.7KB/s 00:00

    4.3、sftp

    基于ssh协议的ftp功能,底层使用ssh协议

    提供文件的上传(put)和下载(get)

    1. [root@nginx-kafka01 ssh]# sftp cali@192.168.1.173
    2. cali@192.168.1.173's password:
    3. Connected to 192.168.1.173.
    4. # 查看在远程机器的哪个目录
    5. sftp> pwd
    6. Remote working directory: /home/cali
    7. # 查看远程机器当前目录下的文件
    8. sftp> ls
    9. fengdeyong passwd shiyq
    10. # 将本机的/var/log/secure文件传至远程机器
    11. sftp> put /var/log/secure
    12. Uploading /var/log/secure to /home/cali/secure
    13. /var/log/secure 100% 5118 200.3KB/s 00:00
    14. sftp> ls
    15. fengdeyong hosts passwd secure shiyq
    16. # 查看现在在当前机器的哪个目录
    17. sftp> lpwd
    18. Local working directory: /etc/ssh
    19. # 查看有哪些命令
    20. sftp>
    21. ?
    22. # 查看本机所在目录下有哪些文件
    23. sftp> !ls
    24. moduli ssh_config ssh_host_ecdsa_key ssh_host_ed25519_key ssh_host_rsa_key
    25. passwd sshd_config ssh_host_ecdsa_key.pub ssh_host_ed25519_key.pub ssh_host_rsa_key.pub
    26. # 进入本机的/lianxi目录
    27. sftp> lcd /lianxi
    28. sftp> !ls
    29. 0408 0617 0701 0704 75 76 code linux python-test scrip script shell-test
    30. # 下载hosts文件
    31. sftp> get hosts
    32. # 递归上传linux目录
    33. sftp> put -r linux

    五、加固服务

    六、signal

    man 7 signal

    1.        Signal     Value     Action   Comment
    2.        ──────────────────────────────────────────────────────────────────────
    3.        SIGHUP        1       Term    Hangup detected on controlling terminal
    4.                                      or death of controlling process
    5.        SIGINT        2       Term    Interrupt from keyboard
    6.        SIGQUIT       3       Core    Quit from keyboard
    7.        SIGILL        4       Core    Illegal Instruction
    8.        SIGABRT       6       Core    Abort signal from abort(3)
    9.        SIGFPE        8       Core    Floating point exception
    10.        SIGKILL       9       Term    Kill signal
    11.        SIGSEGV      11       Core    Invalid memory reference
    12.        SIGPIPE      13       Term    Broken pipe: write to pipe with no
    13.                                      readers
    14.        SIGALRM      14       Term    Timer signal from alarm(2)
    15.        SIGTERM      15       Term    Termination signal
    16.        SIGUSR1   30,10,16    Term    User-defined signal 1
    17.        SIGUSR2   31,12,17    Term    User-defined signal 2
    18.        SIGCHLD   20,17,18    Ign     Child stopped or terminated
    19.        SIGCONT   19,18,25    Cont    Continue if stopped
    20.        SIGSTOP   17,19,23    Stop    Stop process
    21.        SIGTSTP   18,20,24    Stop    Stop typed at terminal
    22.        SIGTTIN   21,21,26    Stop    Terminal input for background process
    23.        SIGTTOU   22,22,27    Stop    Terminal output for background process

  • 相关阅读:
    【电脑讲解】NVIDIA发布GeForce RTX SUPER系列显卡,游戏玩家福利来了!
    Git的安装以及基础使用方法
    2.3队列
    智能升降桌控制主板开发,解锁办公家居新场景
    [Dubbo3.0.8源码解析系列]-24- Dubbo应用级服务发现
    ElasticSearch万字入门教程 一天上手ElasticSearch
    提升网络安全防御能力的几个方面
    从零开始利用MATLAB进行FPGA设计(一):建立脉冲检测模型的Simulink模型2
    CentOS8安装部署DzzOffice协同办公平台
    公司职位描述
  • 原文地址:https://blog.csdn.net/m0_48638643/article/details/126028871