linux中ssh远程服务

ssh定义

  • ssh是一种安全通道协议
  • 对通信的数据进行了加密处理,用于远程管理

openssh服务器

配置openssh服务端

先启动服务

[root@server ~]# netstat -anpt |grep 22
tcp        0      0 192.168.122.1:53        0.0.0.0:*               LISTEN      8791/dnsmasq        
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      17896/sshd          
tcp        0      0 192.168.3.1:22          192.168.3.7:55300       ESTABLISHED 20325/sshd: root@pt 
tcp6       0      0 :::22                   :::*                    LISTEN      17896/sshd  

服务器的主配置文件在/etc/ssh/sshd_config中

#Port 22	//默认端口
#LoginGraceTime 2m		//登录超时时间
#PermitRootLogin yes	//是否允许远程root登录
#MaxAuthTries 6			//最大输入密码次数
#MaxSessions 10			//最大的连接数
#PasswordAuthentication yes	//是否开启密码认证
#PermitEmptyPasswords no	//是否允许空密码
#UseDNS yes			//是否解析dns

如果在服务端修改了主配置文件,需要将服务重启
用systemctl restart sshd

  • 可以用sshd -t 验证sshd配置文件
[root@server ~]# sshd -t
/etc/ssh/sshd_config: line 69: Bad configuration option: hallengeResponseAuthentication
/etc/ssh/sshd_config: terminating, 1 bad configuration options

错了就会提示,如果没错的的话就不会提示

[root@server ~]# sshd -t
[root@server ~]# 
  • 修改默认端口来用ssh连接
    将端口改为2222并取消注释,然后用客户机去远程服务端
Port 2222
[root@client ~]# ssh root@192.168.3.1 -p 2222
root@192.168.3.1's password: 
Last login: Wed May 26 02:00:01 2021 from 192.168.3.7
[root@server ~]# 

白名单和黑名单

当希望只允许或禁止某些用户登录时,可以设置白名单或者黑名单
添加白名单,在 /etc/ssh/sshd_config中加一行,允许登录root,tom,其他不允许

Allowusers root tom

现在服务端上创建两个用户,tom用户可以登陆到服务端,而zhangsan用户不在白名单中所以登录不了服务端。

[root@server ~]# useradd tom
[root@server ~]# useradd zhangsan


[root@client ~]# ssh tom@192.168.3.1 -p 2222
tom@192.168.3.1's password: 
Last login: Wed May 26 02:21:31 2021 from 192.168.3.7
[tom@server ~]$ 
[root@client ~]# ssh zhangsan@192.168.3.1 -p 2222
zhangsan@192.168.3.1's password: 
Permission denied, please try again.

当白名单和黑名单中同时有tom会是什么情况?
我们现在加一条黑名单,把tom放进去,然后重启服务

Denyusers tom
[root@client ~]# ssh tom@192.168.3.1 -p 2222
tom@192.168.3.1's password: 
Permission denied, please try again.

会发现tom用户在白名单中也登陆不了,所以当白名单黑名单同时存在时,ssh为了安全考虑黑名单会优先。

ssh免密登录

ssh免密登录原理

在这里插入图片描述
①客户机会先发送公钥给服务端
②客户机发起连接请求,然后再发一次公钥给服务端
③两次公钥一致,服务端会用客户机的公钥加密一串随机数据发回给客户机
④客户机拿自己的私钥解密数据,然后发回给服务端
⑤服务端接到客户机的数据会对比自己发的数据,如果一样的就认为客户机受信任,允许免密连接

客户机配置免密登录

  • 用工具制作公钥和私钥
  • 把公钥发给服务器
    在客户机上配置ssh-keygen制作公钥和私钥
[root@client ~]# ssh-keygen 
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:SMQk4N88uEU4xRUUOJZ5dD/sPaK3FpDOn6ze5h648Kk root@client
The key's randomart image is:
+---[RSA 2048]----+
|  ...++B*o.      |
| .   =O .. o     |
|  . o.oo   .+    |
|   . B .  o. o   |
|    o * So .o o  |
|     o .  oo.. . |
|    .   . oooo   |
|         o =*o   |
|        Eo**=    |
+----[SHA256]-----+

然后推送到服务端

[root@client ~]# ssh-copy-id tom@192.168.3.1
/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: 2 key(s) remain to be installed -- if you are prompted now it is to install the new keys
tom@192.168.3.1's password: 

Number of key(s) added: 2

Now try logging into the machine, with:   "ssh 'tom@192.168.3.1'"
and check to make sure that only the key(s) you wanted were added.
[root@client ~]# ssh tom@192.168.3.1
Last login: Wed May 26 03:04:01 2021 from 192.168.3.7 //无需密码就能登录
[tom@server ~]$ 

设置尝试登录次数

[root@server ~]# ssh -o NumberOfPasswordPrompts=8 root@192.168.3.7
root@192.168.3.7's password: //第一次
Permission denied, please try again.
root@192.168.3.7's password: 	//第二次
Permission denied, please try again.
root@192.168.3.7's password: 	//第三次
Permission denied, please try again.
root@192.168.3.7's password: 	//第四次
Permission denied, please try again.
root@192.168.3.7's password: //第五次
Permission denied, please try again.
root@192.168.3.7's password: //第六次
Received disconnect from 192.168.3.7 port 22:2: Too many authentication failures
Authentication failed.

客户机中scp命令

在客户机中

[root@client ~]# ls
2  anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面
[root@client ~]# scp 2 root@192.168.3.1:/opt  //将客户机的文件拷贝到服务端
root@192.168.3.1's password: 
2                                                              100%    0     0.0KB/s   00:00    
[root@server ~]# ls /opt/
2  rh
[root@client ~]# scp root@192.168.3.1:1 /opt  //从服务端拷贝到客户机上
root@192.168.3.1's password: 
1                                                              100%    0     0.0KB/s   00:00    
[root@client ~]# ls /opt/
1  rh

  • scp -a 是拷贝目录

客户机中sftp命令

[root@client ~]# sftp root@192.168.3.1 //使用ftp
root@192.168.3.1's password: 
Connected to 192.168.3.1.
sftp> ls   //查看服务端的家目录
1                       anaconda-ks.cfg         initial-setup-ks.cfg    下载                  
公共                  图片                  文档                  桌面                  
模板                  视频                  音乐       
sftp> lls      //查看客户机当前目录
2  anaconda-ks.cfg  initial-setup-ks.cfg  公共	模板  视频  图片  文档	下载  音乐  桌面
sftp> put 1      //上传1文件
Uploading 1 to /root/1
1                                                              100%    0     0.0KB/s   00:00   
sftp> get /opt/2   //下载到本地
Fetching /opt/2 to 2
sftp> lls
1  2  anaconda-ks.cfg  initial-setup-ks.cfg  公共  模板  视频  图片  文档  下载  音乐  桌面

TCP wrappers

  • 通过tcpd程序对其他服务程序进行包装,代为监听 TCP 服务程序的端口
  • 外来的连接请求先会通过防火墙,然后再通过这层检测,只有两层都通过才会获得连接许可

一般是已经安装了

[root@server web]# rpm -q tcp_wrappers
tcp_wrappers-7.6-77.el7.x86_64

通常由其他网络服务程序调用 libwrap.so.*链接库比如sshd
查看是否有链接库

[root@server web]# ldd /usr/sbin/sshd |grep libwrap
	libwrap.so.0 => /lib64/libwrap.so.0 (0x00007f7a9ff86000)

tcp wrappers的两个文件路径

  • /etc/hosts.allow
  • /etc/hosts.deny
    写入文件语法格式:<服务程序列表>: <客户端地址列表>
  • 服务程序列表:ALL代表所有服务,多个服务用,隔开
  • 客户端地址列表:ALL代表所有客户端地址,local代表本机,可以写地址段例如192.168.3.0/24,* 通配符可以写成192.168.3.*代表点3网段,?可以写成192.168.3.3?代表.3.30到.3.39的地址

在这里插入图片描述
在这里插入图片描述
当我allow和deny同时写入同一条,谁会先匹配呢?

[root@client test]# ssh root@192.168.3.1
root@192.168.3.1's password: 
Last failed login: Wed May 26 03:36:14 AEST 2021 from 192.168.3.7 on ssh:notty
There were 16 failed login attempts since the last successful login.
Last login: Wed May 26 03:13:43 2021 from 192.168.3.7
[root@server ~]# 

会发现客户机能够ssh到服务器上

如果检查上述两个文件都找不到相匹配的策略, 那就默认允许访问

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值