目录

ssh服务

ssh服务的配置文件

配置文件常用配置详解:

加密方法

登陆验证原理

实现公钥认证,免密码登录的步骤

公钥认证排错

ssh登录方法

客户端配置


ssh服务

ssh是什么?

ssh --》 secure shell
   remote login program  --远程登陆程序
   对数据进行加密传输的服务,主要用在远程登陆

ssh协议是应用层协议
ssh服务主要是在HP-UX,LINUX,AIX,UNIX系统上都有,windows上没有

ssh常见的两种登陆方式:

1、密码登陆
2、密钥登陆(免密码登录)   ---公钥认证

ssh远程登录其他主机的大前提必须是两台机器可以互相通信(互相可以ping通) 

远程Shell应用程序
允许用户在远程机器上执行任意命令
让标准输出在本地
早期明文远程协议:telnet

SSH(Secure Shell,安全的外壳)
为客户机提供安全的Shell环境,用于远程管理
默认端口:22
依赖TCP协议

SSH基于公钥加密(非对称加密)技术
数据加密传输
客户端和服务器的身份验证

#查看ssh服务是否启动

[root@sanchuang ~]# ps -ef |grep ssh
[root@sanchuang ~]# pidof sshd
2742 2736 867[root@sanchuang ~]# netstat -aptln |grep ssh
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      867/sshd
tcp        0     36 192.168.49.135:22       192.168.49.1:51429      ESTABLISHED 2736/sshd: root [pr
tcp6       0      0 :::22                   :::*                    LISTEN      867/sshd  #0.0.0.0:22表示在本机所有ip上监听22端口
#0.0.0.0:*  表示允许任意ip,任意端口客户端来连接[root@sanchuang ~]# lsof -i:22
COMMAND  PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd     867 root    5u  IPv4  25177      0t0  TCP *:ssh (LISTEN)
sshd     867 root    7u  IPv6  25185      0t0  TCP *:ssh (LISTEN)
sshd    2736 root    5u  IPv4  90178      0t0  TCP sanchuang:ssh->192.168.49.1:51429 (ESTABLISHED)
sshd    2742 root    5u  IPv4  90178      0t0  TCP sanchuang:ssh->192.168.49.1:51429 (ESTABLISHED)

 
#查看命令属于哪个包

[root@sanchuang ~]# which netstat  #找到命令的绝对路径
/usr/bin/netstat
[root@sanchuang ~]# rpm -qf /usr/bin/netstat  #查看这个绝对路径执行文件属于哪个包
net-tools-2.0-0.51.20160912git.el8.x86_64[root@kafka-1 etc]# yum provides dig
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile* base: mirrors.ustc.edu.cn* extras: mirrors.ustc.edu.cn* updates: mirrors.nju.edu.cn
elastic-7.x/filelists                           |  40 MB     00:04
updates/7/x86_64/filelists_db                   | 7.4 MB     00:00
32:bind-utils-9.11.4-26.P2.el7.x86_64 : Utilities for querying DNS name: servers
Repo        : base
Matched from:
Filename    : /usr/bin/dig

/etc/ssh/ssh_config是客户端的配置文件
/etc/ssh/sshd_config是服务端的配置文件
用ssh连接到别的机器上的时候,ssh就是客户端

将sshd_config文件的port改为2233后,需要重启服务使文件生效

[root@kafka-1 ssh]# systemctl restart sshd
[root@kafka-1 ssh]# lsof -i:2233
COMMAND   PID USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
sshd    14685 root    3u  IPv4 346863      0t0  TCP *:infocrypt (LISTEN)
sshd    14685 root    4u  IPv6 346865      0t0  TCP *:infocrypt (LISTEN)

加固系统
1.改ssh端口
2.禁用root用户
3.禁止密码登录
4.防火墙

ssh服务的配置文件

官方站点:http://www.openssh.com
主要软件包:openssh-server、openssh-clients
服务名:sshd
服务端主程序:/usr/sbin/sshd
客户端主程序:/usr/bin/ssh
服务端配置文件:/etc/ssh/sshd_config
客户端配置文件:/etc/ssh/ssh_config

#查看命令属于哪个包下载的

[sanchuang@mysql-binary ssh]$ which ssh
/bin/ssh
[sanchuang@mysql-binary ssh]$ rpm -qf /bin/ssh
openssh-clients-7.4p1-21.el7.x86_64
[root@mysql-binary ~]# rpm -qf /usr/sbin/sshd
openssh-server-7.4p1-21.el7.x86_64

配置文件常用配置详解:

Port 2233  #修改默认监听端口(默认22端口)
#AddressFamily any
ListenAddress 192.168.0.132  #设置本机监听ip地址,默认为0.0.0.0(表示在本机任意ip地址上监听)
PermitRootLogin no  #不允许root用户登陆,默认为yes
PubkeyAuthentication yes #是否开启公钥认证
AuthorizedKeysFile  .ssh/authorized_keys  #配置公钥认证的文件,存放公钥的文件,此文件权限必须为600,否则认证不通过
PasswordAuthentication no   #是否开启密码认证,默认为yes
UsePAM yes #使用pam认证
#pam认证模块 --》配置路径/etc/pam.d 这个目录下面存放的是每个需要认证的服务的配置,文件名就是服务名
UseDNS yes  #是否将客户端主机名解析为ip#此过程不顺利的话,会非常的慢,会影响登陆认证的速度,可以将其设置为no#禁止root用户登录后,root文件夹用sudo也进不去,但是可以切换到root用户
[sanchuang@kafka-2 etc]$ sudo -i   #不需要密码,不接用户名默认切换到root用户
[root@kafka-2 ~]#
#su root 切换需要密码

修改完成之后,重新加载配置的方法

[root@mysql-binary ssh]# kill -HUP 23380
[root@mysql-binary ssh]# kill -1 23380
[root@mysql-binary ssh]# service sshd restart
Redirecting to /bin/systemctl restart sshd.service
[root@mysql-binary ssh]# service sshd reload
Redirecting to /bin/systemctl reload sshd.service
[root@mysql-binary ssh]# [root@mysql-rpm .ssh]# man ssh
[root@mysql-rpm .ssh]# man 5 /etc/ssh/sshd_config  #查看配置文件帮助文档

加密方法

1、对称加密     
   加解密的钥匙都是同一把,双方都知道密钥
   #怎么样安全的保存这个密钥,这个密钥在需要加密的机器之前都是共享?
     很难保证这个密钥不被泄漏
2、非对称加密
   生成一对公私钥,私钥自己保管,公钥可以给其他人。
   公私钥对是成对存在的,一个用于加密,一个用于解密。具体哪个为私钥,哪个为公钥就看使用者自己管理。自己拿着的就是私钥,给别人的就是公钥。

注意:
      使用公钥进行加密,私钥解密,基本用于数据加密
      使用私钥加密公钥解密,用于认证

登陆验证原理

1、密码登录
   client向server发送登陆请求,server将自己的公钥发送给client。
   client使用这个公钥,将密码进行加密,发送给server
   server用私钥解密登陆密码,验证合法性
   server返回验证结果给client

这个流程有一个问题, 怎么保证收到的公钥就是目标server的公钥?(中间人攻击)
如果一个攻击者中途拦截了client的登陆请求,发送自己的公钥给client,client端就会用攻击者的公钥进行数据加密,攻击者接收到信息后,用自己的私钥就可以解密了,这就窃取了client的登陆信息了。

为了解决这个问题,client端第一次登陆的时候,会进行一个登陆公钥确认。

[sanchuang@mysql-binary .ssh]$ ssh 192.168.0.35
The authenticity of host '192.168.0.35 (192.168.0.35)' can't be established.
ECDSA key fingerprint is SHA256:6V02tsAzBmVJ7yEbppVkISnSEyvf+HFWbzDbIPwmG84.
ECDSA key fingerprint is MD5:6c:bd:a9:37:af:ba:f1:53:dd:c8:d2:d9:16:44:c9:9e.
Are you sure you want to continue connecting (yes/no)?

确认server服务端的这个host主机摘要,确认成功之后就会将server端的pubkey保存在~/.ssh/know_hosts里面
以后每次连接都会验证这个know_hosts里的key和收到的pubkey是否一致。

server主机的pubkey保存在/etc/ssh/目录下,默认使用 ssh_host_ecdsa_key.pub

[root@mysql-rpm ssh]# cd /etc/ssh
[root@mysql-rpm ssh]# ls
moduli      sshd_config         ssh_host_ecdsa_key.pub  ssh_host_ed25519_key.pub  ssh_host_rsa_key.pub
ssh_config  ssh_host_ecdsa_key  ssh_host_ed25519_key    ssh_host_rsa_key

2、公钥认证登陆
   client端生成公钥对,将公钥追加在server端的~/.ssh/authorized_keys
   发送登陆请求,server收到请求之后,生成随机字符串发送给client
   client用自己的私钥对字符串进行加密,发送给server。
   server收到加密字符串之后用公钥解密,比较解密出的字符串和之前生成的字符串事发后一致。
   返回结果给client

公钥(Public Key)和私钥(Private Key)
公钥和私钥是成对生成的,这两个密钥互不相同,两个密钥可以互相加密和解密 
不能根据一个密钥而推算出另外一个密钥
公钥对外公开,私钥只有私钥的持有人才知道 
私钥应该由密钥的持有人妥善保管 
根据实现的功能不同,可以分为数据加密和数字签名

实现公钥认证,免密码登录的步骤

1、在A机器上生成公钥对,(如果有公钥对,则不需要重新生成),默认会放在当前用户家目录下的.ssh/文件下
   生成一个id_rsa(私钥),id_rsa.pub(公钥)
   [root@mysql-binary ~]# ssh-keygen  #生成,中间一直敲回车,选择默认

[root@mysql-binary ~]# 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:iuEkRdZ2Gz/PutGHRUs0kXO9Cwgjjlq1ish+n0i7lOU root@mysql-binary
The key's randomart image is:
+---[RSA 2048]----+
|    o.        +o.|
|   o  oooo   .o.o|
|    ..+.o+o . oo.|
|   . o o. o. + o |
|. o *.. S  +  + .|
| o *++ .   .oo . |
|.  +oE.   ..o .  |
| .o.o .   .. .   |
|  .+oo    ..     |
+----[SHA256]-----+

2、在B机器上目标用户的家目录下面~/.ssh/authorized_keys文件里将A机器的公钥复制粘贴过来,

没有此文件就创建,并且确保这个文件的权限设为600
3、查看公钥认证是否成功
   在A机器上执行  ssh root@B机器的ip
   不需要输入密码就可以登陆到B机器,则表示免密码登陆成功

公钥认证排错

1、确保公钥正确
2、确保~/.ssh/authorized_keys文件权限为600
3、确保家目录以及.ssh目录权限为755以下权限,即属组和其他人没有7的权限

ssh登录方法

[root@mysql-binary .ssh]# ssh wy@192.168.0.35
Last failed login: Sat Nov 14 09:59:11 CST 2020 from 192.168.0.132 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Sat Nov 14 09:53:11 2020 from 192.168.0.132
[wy@mysql-rpm ~]$ 登出
Connection to 192.168.0.35 closed.[root@mysql-binary .ssh]# ssh 192.168.0.35 -l wy
Last login: Sat Nov 14 10:01:28 2020 from 192.168.0.132
[wy@mysql-rpm ~]$ 登出
Connection to 192.168.0.35 closed.[root@mysql-binary .ssh]# ssh -vvv 192.168.0.35 wy #######查看登陆过程详细信息
...
debug1: Trying private key: /home/sanchuang/.ssh/id_rsa
debug3: no such identity: /home/sanchuang/.ssh/id_rsa: No such file or directory
debug1: Trying private key: /home/sanchuang/.ssh/id_dsa
debug3: no such identity: /home/sanchuang/.ssh/id_dsa: No such file or directory
debug1: Trying private key: /home/sanchuang/.ssh/id_ecdsa
debug3: no such identity: /home/sanchuang/.ssh/id_ecdsa: No such file or directory
debug1: Trying private key: /home/sanchuang/.ssh/id_ed25519
debug3: no such identity: /home/sanchuang/.ssh/id_ed25519: No such file or directory
...[root@mysql-binary .ssh]# ssh 192.168.0.35 -l wy -p 22  #指定用户,指定端口
Last login: Sat Nov 14 10:01:37 2020 from 192.168.0.132[sanchuang@mysql-binary ~]$ ssh 192.168.0.35  #不接任何用户名,会默认以当前A机器所在用户登陆B机器同名的用户,不管B机器有没有这个用户
sanchuang@192.168.0.35's password: 

######注意:
登陆时默认会去寻找家目录下的~/.ssh/id_rsa去进行验证,所以尽量不要在生成key的时候将它的默认路径更改。

ssh命令参数登录

  • -l  指定登陆的用户名
  • -p  指定server的端口
  • -i  指定私钥文件,默认会在~/.ssh/去找私钥
  • -o  接特定设置选项

   #无需输入yes,自动保存hostkey
1. ssh -o StrictHostKeyChecking=no 192.168.0.132 -p 223

2.修改/etc/ssh/sshd_config文件,加入StrictHostKeyChecking no

3.修改客户端配置,在/root/.ssh/config文件中加入StrictHostKeyChecking no

客户端配置

#配置文件: ~/.ssh/config
[sanchuang@a ~]$ cat .ssh/config
##############################
ForwardAgent yes
StrictHostKeyChecking no
ServerAliveInterval 60
IdentityFile  ~/.ssh/id_rsa
#############################
Host B    #相当于起别名为BHostName 192.168.0.39User   sanchuangPort   2233Host 10.*User  sanchuangPort 2233ProxyCommand  ssh 192.168.0.39 -W %h:%p -l sanchuang -p 2233

 远程执行命令

[root@kafka-2 ~]# ssh B "/usr/sbin/ip a"[root@kafka-2 ~]# ssh 192.168.2.10 "mkdir yuantong"#基于免密登录的远程复制
[root@kafka-2 ~]# scp alias.sh root@192.168.2.10:/opt
alias.sh                            100%   16     1.0KB/s   00:00
#将alias.sh文件复制到192.168.2.10主机上的root用户的/opt文件夹中#指定端口 -P 22#传输目录
[sanchuang@a ~]$ scp -r adir B:  (若:后不接目录,则默认复制到与本机当前目录相同的目录,但:不可省略)
adir-1                                                                                                   100%    0     0.0KB/s   00:00
adir-2                                                                                                   100%    0     0.0KB/s   00:00  
[root@kafka-1 ~]# echo adsf>/dev/pts/0         发送消息显示在0终端
[root@kafka-1 ~]# wall sfjaks            给所有人发送消息

批量处理的命令

pssh命令

选项

  • -h  指定主机文件列表,内容格式"[user@]host[:port]"
  • -i  指定每个服务器的处理信息
[sanchuang@a ~]$ pssh -h ip.txt  -i "/usr/sbin/ip a" 

pscp命令

[sanchuang@a ~]$ pscp.pssh -h ip.txt pscptest /tmp  #把当前目录下的pscptest文件传送到目标主机的/tmp目录下
[1] 17:37:21 [SUCCESS] sanchuang@192.168.0.48:2233
[2] 17:37:22 [SUCCESS] sanchuang@192.168.0.39:2233

fping命令

#根据文件指定ip去ping
[sanchuang@a ~]$ fping -f ip-2.txt
192.168.0.39 is alive
192.168.0.48 is alive
[sanchuang@a ~]$ cat ip-2.txt
192.168.0.39
192.168.0.48#根据网段去ping
[sanchuang@a ~]$ fping -g 192.168.0.0/24

Linux | ssh服务原理、配置及操作相关推荐

  1. ssh linux 配置文件详解,Linux ssh服务常用配置的详细描述及建议配置

    SSH服务常用选项描述 配置文件: /etc/ssh/sshd_config 1.AddressFamliy any:支持那IP协议:比如ipv4,ipv6,:默认 any: 2.Port 22 :S ...

  2. 【Linux】SSH远程终端连接工具(SSH基本用法、ssh服务认证类型、ssh服务常见配置、常见配置总结、远程拷贝scp命令)

    一.SSH远程终端连接工具 1.SSH简介 SSH是一种网络协议,用于计算机之间的加密登录.最早的时候,互联网通信都是明 文通信,一旦被截获,内容就暴露无疑.1995年,芬兰学者Tatu Ylonen ...

  3. linux ssh服务状态,查看linux ssh服务信息及运行状态方法

    关于ssh服务端配置有不少文章,例如 linux下ssh服务配置,这里仅列举出一些查看ssh服务相关信息的常用命令. 1 安装 apt-get install openssh-server 2 完成后 ...

  4. DCN-s4600 ssh服务登录配置:

    DCN-s4600 ssh服务登录配置: 命名交换机和配置交换机远程登录账号与密码: 配置vlan1IP地址: 将et1/0/1端口加入vlan1: 开启交换机远程登录认证方式: 配置交换机全局模式多 ...

  5. 最新华为交换机配置ssh服务端配置案例

    最新华为交换机配置ssh服务端配置案例 # aaa local-user dcrmyy password irreversible-cipher Huawei@123local-user dcrmyy ...

  6. linux ssh服务,Linux配置SSH服务以便实现远程连接

    Linux用户们一定想要知道该怎么开启SSH服务吧,SSH服务是Linux系统远程连接的重要方式,所以如何配置SHH服务也让很多用户感到纠结.现在小编就帮大家解决这个问题. 配置方法: 查询\安装SS ...

  7. Linux之SSH服务端配置文件安全设置

    远程访问安全-SSH 如何才能让ssh更加安全? ssh安全性和配置最佳实践: * 将root账户仅限制为控制台访问,不允许ssh登录 # vim /etc/ssh/sshd_config Permi ...

  8. linux ssh服务的优化,SSH服务端配置、优化加速、安全防护

    CentOS7自带的SSH服务是OpenSSH中的一个独立守护进程SSHD.由于使用telnet在网络中是明文传输所以用其管理服务器是非常不安全的不安全,SSH协议族可以用来对服务器的管理以及在计算机 ...

  9. linux ssh密钥登录配置

    首先确保服务器ssh服务已启动,用户能够正常登录,然后配置客户端,过程如下: 一.先用自已的用户登录到服务器,比如我用 uplinux 登录到服务器 二.运行 SSH Secure Shell 工具中 ...

最新文章

  1. CentOS 6.2 下samba 服务的配置
  2. [php-src]扩展中封装业务与 call_user_function 的使用建议
  3. Android 学习笔记 BroadcastReceiver广播...
  4. 裁员纪实——联想不是我的家(转帖)
  5. jackson.ObjectMapper里enableDefaultTyping方法过期
  6. HTML文本框中只能输入纯数字
  7. [C++程序设计]指针总结
  8. 恒驰机器人_机器人如何造恒驰?探秘最牛汽车生产基地
  9. Ubuntu 16.04安装Tomcat 8 图解
  10. AGC002(D~F)【Kruskal重构树,博弈论,dp】
  11. python裁剪图片并保存_python – 如何从图像中剪切轮廓并将其保存到新文件中
  12. 华兴源创成科创板第一股 6月21日起初步询价
  13. Matlab - Matlab 2016a 安装破解教程
  14. iMeta | FSCapture报告录屏和视频剪辑(视频教程)
  15. candence的图纸大小设置_标准制图图纸尺寸大小
  16. Ubuntu永久修改IP、临时修改IP
  17. 记一次https的免费ssl证书安装及配置过程--window系统
  18. 微商史上最全软文标题写作套路(收藏版)
  19. ffmpeg批量将图片合并为视频
  20. laravel5.5 sendCloud 发送邮件(sendCloud Api and naux/sendcloud )

热门文章

  1. 手机直播服务器系统,手机直播服务器
  2. 使用计算机时什么是死机,电脑经常死机是什么原因造成的,应该怎么解决?
  3. 论道AIGC:如何看待用于内容生成的永动机?| 大咖思辨-38
  4. 为什么梦幻西游服务器名字有显示但是区名字没有了,梦幻西游:免费改名的BUG被发现了,转区名字重复,但不能免费改名...
  5. 一些相见恨晚的 JavaScript 技巧 脚本之家
  6. 为什么有人说CPU是人造物的巅峰?
  7. 压缩包加密后门_什么是加密后门?
  8. JS中的Math 常用API
  9. 基于Web开发的房产系统源码
  10. 远翔降压FP6150B,36V输入,3A输出