一.SSH服务概述
- SSH(Secure Shell)是一种安全通道协议,主要用来实现字符界面的远程登录,远程复制等功能。
- SSH协议对通信双方的数据传输进行了加密处理。
- 在centos7中,openssh服务器由openssh、openssh-server等软件包提供(默认已安装),并且已将sshd添加为标准的系统服务
- sshd服务端配置文件默认位于/etc/ssh/sshd_config,服务端的主程序/usr/sbin/sshd
- sshd服务使用的默认端口是TCP协议的22
- /etc/ssh/sshd_config中的参数如下
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18[root@localhost ~]# vim /etc/ssh/sshd_config #Port 22 #AddressFamily any #ListenAddress 0.0.0.0 #ListenAddress :: HostKey /etc/ssh/ssh_host_rsa_key #HostKey /etc/ssh/ssh_host_dsa_key HostKey /etc/ssh/ssh_host_ecdsa_key HostKey /etc/ssh/ssh_host_ed25519_key # Ciphers and keying #RekeyLimit default none ......
注:telnet协议使用udp的23端口,明文传输数据;mstsc远程桌面功能使用3389端口(windows系统);VNC(跨系统远程连接);TeamVlewer(手机与PC端之间远程连接)
二.ssh命令——远程安全登录
- 用户账号的的安全控制
- sshd服务默认允许使用root用户远程登录,当在Internet中使用时这是非常不安全的,一般是先以普通用户远程登录,进入安全的shell环境之后,使用su命令切换到root
- 在/etc/ssh/sshd_config的参数中
复制代码
1
2
3
4
5
6
7
8
9PermitRootLogin no //禁止root用户远程登录 PermitEmptyPasswords no //禁止空密码用户远程登录 MaxAuthTries 6 //最大重试次数为6 AllowUsers //白名单,仅允许用户远程登录 DenyUsers //黑名单,仅拒绝用户远程登录 黑名单与白名单注意不要同时使用 AllowUsers zhangsan root@192.168.43.132 //仅允许该IP地址的主机zhangsan, root账户登录,多个用户之间用空格隔开
- sshd服务支持两种验证方式——密码验证和密钥对验证,相对来说,密钥对验证的方式比较安全。
- 密码验证的实例
复制代码
1
2
3
4
5
6
7
8
9[root@day01 ~]# ssh root@192.168.43.132 root@192.168.43.132's password: Last login: Sun Nov 24 20:57:36 2019 from 192.168.43.147 [root@day02 ~]# exit 登出 Connection to 192.168.43.132 closed. [root@day01 ~]#
- 限制root用户登录
复制代码
1
2
3
4
5
6
7在服务端设置禁止root用户被远程登录 [root@day02 ~]# vim /etc/ssh/sshd_config PermitRootLogin no [root@day02 ~]# systemctl restart sshd //重启服务,更新参数 [root@day02 ~]#
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18在客户端尝试登录root [root@day01 ~]# ssh root@192.168.43.132 root@192.168.43.132's password: Permission denied, please try again. root@192.168.43.132's password: Permission denied, please try again. root@192.168.43.132's password: [root@day01 ~]# ssh zhangsan@192.168.43.132 //登录服务端的zhangsan用户 zhangsan@192.168.43.132's password: Last login: Thu Oct 31 11:10:07 2019 [zhangsan@day02 ~]$ su - root //使用su命令切换 密码: [root@day02 ~]# vim /etc/pam.d/su //开启PAM认证,限制su命令的切换 auth required pam_wheel.so use_uid
- 基于ssh服务的免交互密钥对验证
- 在客户机创建密钥对
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27[root@day01 .ssh]# ssh-keygen -t rsa 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: SHA256:IkCe/o4389L3ZArgsUCdGYRlX+VBP8nocKPr6Okppas root@day01 The key's randomart image is: +---[RSA 2048]----+ | .+= .o+ | | o.+ = . . = . | | = + . . = = | | o . = . . | | o + . S . | | + =.. . | | ++. . o | | oB .*.+ | | EooXB.o.. | +----[SHA256]-----+ [root@day01 .ssh]# //输入密钥时要有复杂度
- 在客户机上查看密钥对
复制代码
1
2
3
4[root@day01 .ssh]# cd /root/.ssh [root@day01 .ssh]# ls id_rsa id_rsa.pub //id_rsa为私钥,id_rsa.pub为公钥
- 将公钥文件上传到服务端,且在服务端导入公钥文件
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13[root@192 .ssh]# ssh-copy-id -i id_rsa.pub root@192.168.43.134 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "id_rsa.pub" /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 root@192.168.43.134's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'root@192.168.43.134'" and check to make sure that only the key(s) you wanted were added. [root@192 .ssh]#
- 此时可以通过输入密钥验证连接服务端,也可以创建代理功能,添加私钥
复制代码
1
2
3
4
5
6[root@192 .ssh]# ssh-agent bash [root@192 .ssh]# ssh-add Enter passphrase for /root/.ssh/id_rsa: Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa) [root@192 .ssh]#
- 验证密钥免交互
复制代码
1
2
3
4
5[root@192 .ssh]# ssh root@192.168.43.134 Last login: Sun Nov 24 21:48:39 2019 from 192.168.43.148 [root@demo ~]#
注:输入密钥是需要由复杂度,即不能单纯输入数字。但是也可以不输入密钥,这样也可以实现“免交互”
三.scp命令——远程复制
- 通过scp命令可以利用SSH安全连接与远程主机相互复制文件
- 覆盖目标主机的文件
- 本地主机
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16[root@demo mnt]# ls a.txt [root@demo mnt]# cat a.txt hello world [root@demo mnt]# scp a.txt root@192.168.43.134:/mnt/a.txt root@192.168.43.134's password: a.txt 100% 13 7.1KB/s 00:00 [root@demo mnt]# ls a.txt [root@demo mnt]# cat a.txt hello world [root@demo mnt]#
- 目标主机
复制代码
1
2
3
4
5
6
7
8
9
10
11[root@demo mnt]# ls a.txt b.txt [root@demo mnt]# cat a.txt [root@demo mnt]# cat b.txt 123 [root@demo mnt]# cat a.txt hello world [root@demo mnt]#
- 从目标复制文件到本地
- 本地主机
复制代码
1
2
3
4
5
6
7
8
9
10[root@demo mnt]# scp root@192.168.43.134:/mnt/c.txt ./ root@192.168.43.134's password: c.txt 100% 3 2.7KB/s 00:00 [root@demo mnt]# ls a.txt c.txt [root@demo mnt]# cat c.txt zz [root@demo mnt]#
- 目标主机
复制代码
1
2
3
4
5
6[root@demo mnt]# vim c.txt [root@demo mnt]# ls a.txt c.txt [root@demo mnt]# cat c.txt zz [root@demo mnt]#
四.sftp命令——安全ftp
- 通过sftp命令可以利用SSH安全连接远程主机上传(put),下载(get)文件
- 本地主机
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23[root@192 mnt]# ls d.txt [root@192 mnt]# sftp root@192.168.43.134 sign_and_send_pubkey: signing failed: agent refused operation root@192.168.43.134's password: Connected to 192.168.43.134. sftp> ls anaconda-ks.cfg initial-setup-ks.cfg 下载 公共 图片 文档 桌面 模板 视频 音乐 sftp> cd /mnt sftp> ls a.txt c.txt sftp> get a.txt /mnt Fetching /mnt/a.txt to /mnt/a.txt /mnt/a.txt 100% 13 2.2KB/s 00:00 sftp> put /mnt/d.txt /mnt Uploading /mnt/d.txt to /mnt/d.txt /mnt/d.txt 100% 8 1.9KB/s 00:00 sftp> exit [root@192 mnt]# ls a.txt d.txt [root@192 mnt]#
- 目标主机
复制代码
1
2
3
4
5
6
7[root@demo mnt]# ls a.txt c.txt [root@demo mnt]# ls a.txt c.txt d.txt [root@demo mnt]#
五.TCP Wrappers访问控制
- TCP Wrappers(TCP 封套),以作为应用服务与网络之间的一道特殊防线,提供额外的安全保障
- TCP Wrappers的访问控制是基于TCP协议的应用服务,其可以记录所有企图访问被保护服务的行为
- 关于TCP Wrappers访问控制的两个配置文件/etc/hosts.allow(允许访问),/etc/hosts.deny(拒绝访问),若是在两个配置文件中都没有匹配策略,则默认允许,一般先读取/etc/hosts.allow,后读取/etc/hosts.deny
- 如下:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13[root@192 ~]# vim /etc/hosts.allow # # hosts.allow This file contains access rules which are used to # allow or deny connections to network services that # either use the tcp_wrappers library or that have been # started through a tcp_wrappers-enabled xinetd. # # See 'man 5 hosts_options' and 'man 5 hosts_access' # for information on rule syntax. # See 'man tcpd' for information on tcp_wrappers #
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15[root@192 ~]# vim /etc/hosts.deny # # hosts.deny This file contains access rules which are used to # deny connections to network services that either use # the tcp_wrappers library or that have been # started through a tcp_wrappers-enabled xinetd. # # The rules in this file can also be set up in # /etc/hosts.allow with a 'deny' option instead. # # See 'man 5 hosts_options' and 'man 5 hosts_access' # for information on rule syntax. # See 'man tcpd' for information on tcp_wrappers #
注:在这两个配置文件中可以使用通配符*和?;ALL代表所有,如sshd:ALL,即拒绝或者允许所有用户拒绝访问或者访问ssh服务;“,”代表间隔,如两个IP地址之间,两个服务之间可以用“,”隔开。
最后
以上就是平常背包最近收集整理的关于Linux网络服务-----SSH服务的全部内容,更多相关Linux网络服务-----SSH服务内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复