当前位置:   article > 正文

【Linux网络】SSH--远程控制与访问_ssh 远程

ssh 远程

目录

一、SSH远程管理

1.SSH的定义

2.远程传输的种类

3.OpensSSH

4.SSH客户端与服务端

二、配置OpenSSH服务器

1.sshd_config配置文件的常用选项设置

2.sshd 服务支持两种验证方式

1)密码验证

2)密钥对验证

三、使用 SSH 客户端程序

1.ssh 远程登录

2.scp 远程复制

3.sftp

四、SSH的验证方式

1.密码验证:使用账号和密码进行验证

2.密钥对验证:使用客户端创建的密钥对进行验证

3.案例:ssh密钥对的创建和使用

4.ssh密钥对免交互登录

五、TCP Wrappers 访问控制

1.TCP Wrappers

2.TCP Wrappers 保护机制的两种实现方式

3.TCP Wrappers的运用

4.TCP Wrappers 的访问策略

5.TCP Wrappers 机制的基本原则:

补充知识:如何查看服务器被爆破,并且禁止其访问

总结:


一、SSH远程管理

1.SSH的定义

SSH(Secure Shell)是一种安全通道协议,主要用来实现字符界面的远程登录、远程复制等功能。
SSH协议对通信双方的数据传输进行了加密处理,其中包括用户登录时输入的用户口令。因此SSH协议具有很好的安全性。

2.远程传输的种类

ssh(tcp/22端口)密文传输,加密,更安全

telnet(tcp/23端口):明文传输,数据容易被劫获;用的极少

3.OpensSSH

OpenSSH----是ssh对应的软件,一般是系统默认安装的;在ubuntu系统中需要自行安装

OpenSSH 是实现 SSH 协议的开源软件项目,适用于各种 UNIX、Linux 操作系统。

CentOS 7系统默认已安装openssh相关软件包,并已将 sshd 服务添加为开机自启动。执行“systemctl start sshd”命令即可启动 sshd 服务

sshd 服务默认使用的是TCP的 22端口

服务名称:sshd

服务端主程序:/usr/sbin/sshd

服务端配置文件:/etc/ssh/sshd_config

客户端配置文件:/etc/ssh/ssh_config

 注意:ssh_config和sshd_config都是ssh服务器的配置文件,二者区别在于前者是针对客户端的配置文件,后者则是针对服务端的配置文件。

4.SSH客户端与服务端

SSH客户端:Putty、Xshell、CRT、MobaXterm
SSH服务端:OpenSSH

    网络
SSH客户端<--------------------------------------->SSH服务端
            数据传输是加密的,可以防止信息泄漏
            数据传输是压缩的,可以提高传输速度

注意:在生产环境中,为了保密等措施,Xshell、CRT等外国软件应用较少,多使用国产或开源的终端工具

二、配置OpenSSH服务器

1.sshd_config配置文件的常用选项设置

vim /etc/ssh/sshd_config


Port 22                                 #监听端口为 22
ListenAddress 0.0.0.0          #监听地址为任意网段,也可以指定OpenSSH服务器的具体IP

LoginGraceTime 2m            #登录验证时间为 2 分钟
PermitRootLogin no             #禁止 root 用户登录
MaxAuthTries 6                    #最大重试次数为 6

PermitEmptyPasswords no        #禁止空密码用户登录
UseDNS no                                #禁用 DNS 反向解析,以加快远程连接速度


AllowUsers zhangsan lisi wangwu@192.168.10.120                     #只允许zhangsan、lisi、wangwu用户登录,且其中wangwu用户仅能够从IP地址为192.168.10.120的主机远程登录多个,用户以空格分隔
DenyUsers zhangsan        #禁止某些用户登录,用法于AllowUsers 类似(注意不要同时使用)

2.sshd 服务支持两种验证方式

1)密码验证

对服务器中本地系统用户的登录名称、密码进行验证。简便,但可能会被暴力破解

2)密钥对验证

要求提供相匹配的密钥信息才能通过验证。通常先在客户端中创建一对密钥文件(公钥、私钥),然后将公钥文件放到服务器中的指定位置。远程登录时,系统将使用公钥、私钥进行加密/解密关联验证。能增强安全性,且可以免交互登录。

公钥和私钥的关系:
●公钥和私钥是成对生成的,这两个密钥互不相同,可以互相加密和解密。
●不能根据一个密钥来推算出另一个密钥。
●公钥对外公开,私钥只有私钥的持有人才知道。

当密码验证、密钥对验证都启用时,服务器将优先使用密钥对验证。可根据实际情况设置验证方式。

vim /etc/ssh/sshd_config


PasswordAuthentication yes                         #启用密码验证
PubkeyAuthentication yes                             #启用密钥对验证
AuthorizedKeysFile .ssh/authorized_keys     #指定公钥库文件

三、使用 SSH 客户端程序

1.ssh 远程登录

ssh [选项] zhangsan@192.168.10.120

当用户第一次登录SSH服务器时,必须接受服务器发来的ECDSA密钥(根据提示输入“yes”)后才能继续验证。接收的密钥信息将保存到~/.ssh/known_hosts 文件中。密码验证成功以后,即可登录目标服务器的命令行环境中了。

-p:指定非默认的端口号,缺省时默认使用 22端口
ssh -p 2345 zhangsan@192.168.80.10

  1. [root@bogon ~]# systemctl restart sshd #重启ssh服务
  2. [root@bogon ~]# ssh root@192.168.10.120 #连接请求被拒,ssh服务默认端口号为22
  3. ssh: connect to host 192.168.10.120 port 22: Connection refused
  4. [root@bogon ~]# ssh -p 2345 root@192.168.10.120 #-p指定端口号,连接成功
  5. root@192.168.10.120's password:
  6. Last login: Sun Apr 28 21:10:35 2024 from 192.168.10.1
  7. [root@bogon ~]#

2.scp 远程复制

#下行复制
scp root@192.168.80.11:/etc/passwd /root/passwd10.txt        #将远程主机中的/etc/passwd文件复制到本机

#上行复制
scp -r /etc/ssh/ root@192.168.80.10:/opt                    #将本机的/etc/ssh 目录复制到远程主机

 案例:复制文件和目录

被复制主机: 

  1. [root@bogon ~]# cd /opt
  2. [root@bogon opt]# ls
  3. rh
  4. [root@bogon opt]# echo 123 > 123.txt
  5. [root@bogon opt]# ls
  6. 123.txt rh
  7. [root@bogon opt]# mkdir xy101
  8. [root@bogon opt]# cd xy101/
  9. [root@bogon xy101]# echo 456 > 456.txt
  10. [root@bogon xy101]# ls
  11. 456.txt
  12. [root@bogon xy101]# cd ..
  13. [root@bogon opt]# ls
  14. 123.txt rh xy101

复制主机:

  1. [root@bogon opt]# scp root@192.168.10.120:/opt/123.txt /opt #复制文件
  2. The authenticity of host '192.168.10.120 (192.168.10.120)' can't be established.
  3. ECDSA key fingerprint is SHA256:L6wJ0Efd2d4p2puz1SUhKBh8PqQGVZkxLoea4Z5fSdE.
  4. ECDSA key fingerprint is MD5:50:20:53:60:a2:8b:ab:f5:db:1b:5a:29:39:8b:7d:10.
  5. Are you sure you want to continue connecting (yes/no)? yes
  6. Warning: Permanently added '192.168.10.120' (ECDSA) to the list of known hosts.
  7. root@192.168.10.120's password:
  8. 123.txt 100% 4 4.5KB/s 00:00
  9. [root@bogon opt]# ls
  10. 123.txt rh
  11. [root@bogon opt]# scp root@192.168.10.120:/opt/xy101 /opt #复制目录加上-r选项
  12. root@192.168.10.120's password:
  13. scp: /opt/xy101: not a regular file
  14. [root@bogon opt]# scp -r root@192.168.10.120:/opt/xy101 /opt
  15. root@192.168.10.120's password:
  16. 456.txt 100% 4 7.2KB/s 00:00
  17. [root@bogon opt]# ls
  18. 123.txt rh xy101
  19. [root@bogon opt]# scp -r *.txt xy101 root@192.168.10.120:/opt #同时复制文件和目录
  20. root@192.168.10.120's password:
  21. 123.txt 100% 4 7.4KB/s 00:00
  22. 456.txt 100% 4 6.3KB/s 00:00
  23. [root@bogon opt]#

3.sftp

由于使用了加密/解密技术,所以传输效率比普通的FTP要低,但安全性更高。操作语法sftp与ftp几乎一样。

sftp zhangsan@192.168.80.10
Connecting to 192.168.80.10...
tsengyia@172.16.16.22's password:            #输入密码
sftp> ls
sftp> get 文件名        #下载文件到ftp目录
sftp> put 文件名        #上传文件到ftp目录
sftp> quit                #退出

  1. [root@bogon ~]# sftp root@192.168.10.120
  2. root@192.168.10.120's password:
  3. Connected to 192.168.10.120.
  4. sftp> ls
  5. anaconda-ks.cfg initial-setup-ks.cfg
  6. sftp> cd /opt
  7. sftp> ls
  8. 123.txt rh xy101
  9. sftp> get 123.txt #传输文件
  10. Fetching /opt/123.txt to 123.txt
  11. /opt/123.txt 100% 4 6.7KB/s 00:00
  12. sftp> get xy101 #sftp是文件传输,并不能传输目录
  13. Fetching /opt/xy101/ to xy101
  14. Cannot download non-regular file: /opt/xy101/
  15. sftp> cd /opt/xy101
  16. sftp> get 456.txt
  17. Fetching /opt/xy101/456.txt to 456.txt
  18. /opt/xy101/456.txt 100% 4 7.6KB/s 00:00
  19. sftp>

四、SSH的验证方式

1.密码验证:使用账号和密码进行验证

PasswordAuthentication yes

2.密钥对验证:使用客户端创建的密钥对进行验证

PubkeyAuthentication yes
AuthorizedKeysFile      .ssh/authorized_keys           #服务端的公钥文件默认保存路径

3.案例:ssh密钥对的创建和使用

1)确保服务端开启了ssh密钥对验证     /etc/ssh/sshd_config   -->  PubkeyAuthentication yes
2)在客户端创建密钥对       ssh-keygen [-t rsa/dsa/ecdsa]   #生成的密钥对默认存放在当前用户家目录的 .ssh/ 目录中,私钥是留给自己使用的,公钥(XXX.pub)是给服务端使用的
3)上传公钥至服务端         ssh-copy-id [-i 公钥文件]  用户名@目标主机IP    #公钥信息会自动保存到服务端的 ~/.ssh/authorized_keys 文件里
4)客户端 ssh 远程登录,使用密钥对的密码进行验证

注意:密钥对在客户端是谁创建的,只能谁使用;密钥对上传给服务端的哪个用户,那么就只能使用那个用户去登录

在客户端创建密钥对 

  1. [root@bogon ~]# ssh-keygen -t rsa #在客户端创建密钥对
  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:2i1XSm6tbPDVkH7JjvDCIxNZEO/6Kxp+bN7duSyvuro root@bogon
  10. The key's randomart image is:
  11. +---[RSA 2048]----+
  12. | .. |
  13. | .. |
  14. | .. . |
  15. | .. o |
  16. | Soo..+ . |
  17. | o+=.+o = |
  18. | o.+=*+.+ |
  19. | . .BB*o+o.. |
  20. | o=EBO*o+*. |
  21. +----[SHA256]-----+
  22. [root@bogon ~]# cd /root
  23. [root@bogon ~]# ls -A
  24. 123.txt .bash_logout .config .ICEauthority .tcshrc 模板 下载
  25. 456.txt .bash_profile .cshrc initial-setup-ks.cfg .viminfo 视频 音乐
  26. anaconda-ks.cfg .bashrc .dbus .local .Xauthority 图片 桌面
  27. .bash_history .cache .esd_auth .ssh 公共 文档
  28. [root@bogon ~]# cd .ssh/
  29. [root@bogon .ssh]# ls
  30. id_rsa id_rsa.pub known_hosts #私钥id_rsa是留给自己使用的,公钥id_rsa.pub是给服务端使用的
  31. [root@bogon .ssh]# ssh-copy-id -i id_rsa.pub root@192.168.10.120 #上传公钥至服务端
  32. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub"
  33. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
  34. /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
  35. root@192.168.10.120's password:
  36. Number of key(s) added: 1
  37. Now try logging into the machine, with: "ssh 'root@192.168.10.120'"
  38. and check to make sure that only the key(s) you wanted were added.
  39. [root@bogon .ssh]#

查看服务端秘钥对信息

客户端 ssh 远程登录,使用密钥对的密码进行验证

4.ssh密钥对免交互登录

1)使用空密码的密钥对
ssh-keygen      一路回车,不设置密码
ssh-copy-id

2)创建ssh会话代理(只能在当前会话中有效)
ssh-agent bash
ssh-add

  1. [root@bogon ~]# ssh-keygen
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Created directory '/root/.ssh'.
  5. Enter passphrase (empty for no passphrase):
  6. Enter same passphrase again:
  7. Your identification has been saved in /root/.ssh/id_rsa.
  8. Your public key has been saved in /root/.ssh/id_rsa.pub.
  9. The key fingerprint is:
  10. SHA256:M1bqICRSCzSTcWYGFGZ8nq2SEMcnB28V8oxsR5sgnPg root@bogon
  11. The key's randomart image is:
  12. +---[RSA 2048]----+
  13. |=#OBo +. |
  14. |==&*oO o |
  15. |.+=+X * . |
  16. |..EB o o |
  17. |. . o . S |
  18. | o . . + o |
  19. | . . |
  20. | |
  21. | |
  22. +----[SHA256]-----+
  23. [root@bogon ~]# cd ~/.ssh/
  24. [root@bogon .ssh]# ll
  25. 总用量 8
  26. -rw-------. 1 root root 1679 4月 28 22:40 id_rsa
  27. -rw-r--r--. 1 root root 392 4月 28 22:40 id_rsa.pub
  28. [rcot@bogon ~l# ssh-agent bash
  29. [root@bogon ~]# ssh-add
  30. Enter passphrase for /root/.ssh/id ecdsa:
  31. Identity added:/root/.ssh/id_ecdsa (/root/.ssh/id_ecdsa)
  32. [root@bogon ~]#ssh root@192.168.10.120
  33. Last login: Fri Apr 28 23:20:18 2024 from 192.168.10.120
  34. [root@bogon ~]#

注意:ssh密钥对免交互登录时,创建ssh会话代理(只能在当前会话中有效)

五、TCP Wrappers 访问控制

1.TCP Wrappers

TCP Wrappers(TCP封套) 是一个简单的防火墙机制。用来将TCP服务程序“包裹”起来,代为监听TCP服务程序的端口,增加了一个安全检测过程,外来的连接请求必须先通过这层安全检测,获得许可后才能访问真正的服务程序。
大多数 Linux 发行版,TCP Wrappers 是默认提供的功能。rpm -q tcp_wrappers

2.TCP Wrappers 保护机制的两种实现方式

1)直接使用 tcpd 程序对其他服务程序进行保护,需要运行 tcpd 程序。
2)由其他网络服务程序调用 libwrap.so.* 动态函数库,不需要运行 tcpd 程序。此方式的应用更加广泛,也更有效率。

由于支持 TCP Wrappers 的服务必定包含 libwrap 这一个动态函数库,因此可以使用 ldd 来查看服务即可
ldd $(which sshd) | grep libwrap
    libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f91d0bdb000)

ldd $(which vsftpd) | grep libwrap
    libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f4304f3b000)
    
ldd $(which xinetd) | grep libwrap
    libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f8b17ec6000)

3.TCP Wrappers的运用

/etc/hosts.allow 设置允许访问 tcp 服务程序的策略(白名单)

/etc/hosts.deny 设置禁止访问 tcp 服务程序的策略    (黑名单)

假设引用
只允许IP地址为192.168.73.110的用户访问sshd服务,其他地址被拒绝。

  1. [root@bogon ~]# vim /etc/hosts.allow
  2. [root@bogon ~]# vim /etc/hosts.deny
  3. [root@bogon ~]# cat /etc/hosts.allow
  4. #
  5. # hosts.allow This file contains access rules which are used to
  6. # allow or deny connections to network services that
  7. # either use the tcp_wrappers library or that have been
  8. # started through a tcp_wrappers-enabled xinetd.
  9. #
  10. # See 'man 5 hosts_options' and 'man 5 hosts_access'
  11. # for information on rule syntax.
  12. # See 'man tcpd' for information on tcp_wrappers
  13. #
  14. sshd:192.168.10.120 #设置允许访问 tcp 服务程序的策略
  15. [root@bogon ~]# cat /etc/hosts.deny
  16. #
  17. # hosts.deny This file contains access rules which are used to
  18. # deny connections to network services that either use
  19. # the tcp_wrappers library or that have been
  20. # started through a tcp_wrappers-enabled xinetd.
  21. #
  22. # The rules in this file can also be set up in
  23. # /etc/hosts.allow with a 'deny' option instead.
  24. #
  25. # See 'man 5 hosts_options' and 'man 5 hosts_access'
  26. # for information on rule syntax.
  27. # See 'man tcpd' for information on tcp_wrappers
  28. #
  29. sshd:192.168.10.122 #设置拒绝访问 tcp 服务程序的策略
  30. [root@bogon ~]#

4.TCP Wrappers 的访问策略

TCP Wrappers 机制的保护对象为各种网络服务程序,针对访问服务的客户端地址进行访问控制。
通过 /etc/hosts.allow,/etc/hosts.deny 这两个文件来管理的一个类似防火墙的机制,分别用来设置允许和拒绝的策略。

格式:        <服务程序列表>:<客户端地址列表>

1)服务程序列表
ALL:代表所有的服务。
单个服务程序:如“vsftpd”。
多个服务程序组成的列表:如“vsftpd,sshd”。

2)客户端地址列表
ALL:代表任何客户端地址。
LOCAL:代表本机地址。
多个地址以逗号分隔
允许使用通配符 “*” 和 “?” ,前者代表任意长度字符,后者仅代表一个字符
网段地址,如 “192.168.80.” 或者 192.168.80.0/255.255.255.0 (掩码只支持 netmask 的地址显示方式)
区域地址,如 “.benet.com”匹配 benet.com 域中的所有主机。

5.TCP Wrappers 机制的基本原则:

首先检查/etc/hosts.allow文件,如果找到相匹配的策略,则允许访问;
否则继续检查/etc/hosts.deny文件,如果找到相匹配的策略,则拒绝访问;
如果检查上述两个文件都找不到相匹配的策略,则允许访问。

“允许所有,拒绝个别”
只需在/etc/hosts.deny文件中添加相应的拒绝策略

“允许个别,拒绝所有”
除了在/etc/hosts.allow中添加允许策略之外,还需要在/etc/hosts.deny文件中设置“ALL:ALL”的拒绝策略。

  1. #同时设置黑白名单,还是会放通,原理是从上往下依次匹配,匹配到即停止
  2. [root@bogon ~]# cat /etc/hosts.allow
  3. #
  4. # hosts.allow This file contains access rules which are used to
  5. # allow or deny connections to network services that
  6. # either use the tcp_wrappers library or that have been
  7. # started through a tcp_wrappers-enabled xinetd.
  8. #
  9. # See 'man 5 hosts_options' and 'man 5 hosts_access'
  10. # for information on rule syntax.
  11. # See 'man tcpd' for information on tcp_wrappers
  12. #
  13. sshd:192.168.10.120
  14. [root@bogon ~]# cat /etc/hosts.deny
  15. #
  16. # hosts.deny This file contains access rules which are used to
  17. # deny connections to network services that either use
  18. # the tcp_wrappers library or that have been
  19. # started through a tcp_wrappers-enabled xinetd.
  20. #
  21. # The rules in this file can also be set up in
  22. # /etc/hosts.allow with a 'deny' option instead.
  23. #
  24. # See 'man 5 hosts_options' and 'man 5 hosts_access'
  25. # for information on rule syntax.
  26. # See 'man tcpd' for information on tcp_wrappers
  27. #
  28. sshd:192.168.10.120
  29. [root@bogon ~]#

注意:从上往下依次匹配,匹配到即停止,如果放通,后面的策略就不会进行匹配了

补充知识:如何查看服务器被爆破,并且禁止其访问

如何查看服务器被爆破:查看 /var/log/secure文件,如果出现大量failed password时说明正在被爆破;

 解决措施:在/etc/hosts.deny文件中禁止访问策略:sshd:ip

总结:

配置OpenSSH服务端:监听选项、用户登录控制、登录验证方式

使用SSH客户端程序: ssh、scp、sftp、Xshell

构建密钥对验证的SSH体系的步骤

TCP Wrappers访问控制

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

闽ICP备14008679号