linux的 ssh服务

传输分为明文和密文传输,从名字就可以很好的理解了,密文就是正常人看不懂的传输方式,明文就是都可以看懂的。

所以明文是不太安全的。在linux中有一种连接方式ssh 全名Secure SHell是一种加密的传输方式,较为安全,主要是实现远程加密连接。默认的端口是udp22号端口。

也有很多额外的远程连接工具,比如xshell ,xmanager…等等

一般安装操作系统是自动安装了 ssh软件,没有的话也可以自己安装。

软件名如下:

[root@geili .ssh]# rpm -qa | grep ssh

openssh-7.4p1-11.el7.x86_64
openssh-server-7.4p1-11.el7.x86_64
openssh-clients-7.4p1-11.el7.x86_64
libssh2-1.4.3-10.el7_2.1.x86_64

openssh。

ssh有很多配置文件,但是一般都不用修改,除非有特定需求。

# To disable tunneled clear text passwords, change to no here!
#PasswordAuthentication yes
#PermitEmptyPasswords no
PasswordAuthentication yes

这一行  默认是密码登录。后边有用处。

基本使用方法
1、远程登录:ssh 172.16.12.10

第一次访问时会提示是否进行用户指纹信息的记录,

The authenticity of host '172.16.12.15 (172.16.12.15)' can't be established.
ECDSA key fingerprint is SHA256:Vz0sCMqwucPtAO9Jjch99je46OarrJ886qKUU1umQSA.
ECDSA key fingerprint is MD5:f5:6b:be:20:9c:0e:25:65:e5:c1:b4:02:87:26:18:c0.
Are you sure you want to continue connecting (yes/no)? 

这里要写 yes 全称。然后输入密码就可以进行访问了。

第二次访问时就不会提示是否记录指纹信息,因为已经记载过了。

[root@geili .ssh]# ssh 172.16.12.15
root@172.16.12.15's password: 
[root@geili .ssh]# ssh 172.16.12.15
root@172.16.12.15's password: 
Last login: Fri Nov  1 18:12:18 2019 from 172.16.12.10
[root@www ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.12.15  netmask 255.255.0.0  broadcast 172.16.255.255
        inet6 fe80::7fa0:1dbc:69ce:177a  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:52:bd:8f  txqueuelen 1000  (Ethernet)
        RX packets 34864  bytes 2943941 (2.8 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 18617  bytes 8587301 (8.1 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

记载过的指纹信息会存储在用户当前用户家目录的隐藏目录.ssh目录的knows_hosts文件中。

[root@geili .ssh]# vi known_hosts 

172.16.12.15 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBF1zGLILaK5J7/ZnCJzULpjWxSyPNENPZlwu7bpNptI0mcQCiBFvkiA4pxJsf8xfuZ9YQVDLzKfXjuxxp+WWVJU=

这个东西是固定的,记录的是另一台主机的硬件信息,如果另一硬件信息发生修改,就匹配不上,不能登陆。在确定修改是安全的,可以清空这个文件中的内容在进行访问,就可以了。

> .ssh/knows_hosts

再次访问时就是第一次访问了。

2、用指定用户远程访问
那个用户需要密码。

[root@geili .ssh]# ssh u1@172.16.12.15
u1@172.16.12.15's password: 
Last failed login: Fri Nov  1 18:23:36 CST 2019 from 172.16.12.10 on ssh:notty
There was 1 failed login attempt since the last successful login.
Last login: Fri Nov  1 14:44:27 2019
[u1@www ~]$ 

3、远程执行命令。

远程让那个主机关机。

[root@geili .ssh]# ssh 221.122.126.11 poweroff
The authenticity of host '221.122.126.11 (221.122.126.11)' can't be established.
ECDSA key fingerprint is SHA256:9vXMpp3GZpCU9+0iIkJOmsTTWdcIjf24WoKGq5c/SQ8.
ECDSA key fingerprint is MD5:36:9e:8e:25:89:6d:ce:1c:3c:88:7a:b1:a2:ab:47:e9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '221.122.126.11' (ECDSA) to the list of known hosts.
root@221.122.126.11's password: 
Connection to 221.122.126.11 closed by remote host.
[root@geili .ssh]# 

4、图形化连接 -X

5、远程拷贝文件。
本地给远程。

[root@geili gsc-test]# ls
ggg.txt  index.html  lkkk
[root@geili gsc-test]# scp ggg.txt 172.16.12.15:/tmp/gsc-test-ssh/
root@172.16.12.15's password: 
ggg.txt                                                                               100%    6     0.1KB/s   00:00
输入密码,完成

另一主机。

[root@www gsc-test-ssh]#  ls
ggg.txt

从远程拷到本地

远端主机:

[root@www gsc-test-ssh]# touch 12.15file.txt
[root@www gsc-test-ssh]# cat 12.15file.txt 
i am 172.16.12.15

[root@geili gsc-test]# scp 172.16.12.15:/tmp/gsc-test-ssh/12.15file.txt ./
root@172.16.12.15's password: 
12.15file.txt                                                                         100%   18     0.4KB/s   00:00    
[root@geili gsc-test]# ls
12.15file.txt  ggg.txt  index.html  lkkk
[root@geili gsc-test]# cat 12.15file.txt 
i am 172.16.12.15

和正常的cp 操作一样 -r 递归,可以拷贝目录,-p port 指定端口。

6、无密码登录。

秘钥系统,这个大家都不陌生,比如Alipay 就有公钥和私钥,linux这个也类似。

其中如果生成了公钥并做了交换再访问远程主机时就不要输入密码,当然是对用户来说,实际上系统已经自动做了秘钥的认证。

公钥是实现无密码登录的核心,公钥给谁,我就可以登录谁了,如果对方把公钥发给别人,那我也可以访问。

生成秘钥:

[root@geili gsc-test]# 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:CbojdM3beQyckymVpK94Cw/EvxGe6tPHmrCTmgUcKx0 root@geili
The key's randomart image is:
+---[RSA 2048]----+
|        .        |
|       o .       |
|  E   o o        |
| o = + = =       |
|. * = = S        |
| o + = B =       |
|  . OoO.o o      |
|   ++X.=o.       |
|  oo+o*o         |
+----[SHA256]-----+
[root@geili gsc-test]# 

看提示:

/root/.ssh/id_rsa. 这个是私钥 的文件。
/root/.ssh/id_rsa.pub. 这个是公钥的文件

当然这个是再生成是可以改的,但是最好默认,因为一定会忘,甚至root密码也一定会忘QAQ。。。。。。。。。

公钥发给远程主机。

[root@geili gsc-test]# ssh-copy-id -i /root/.ssh/id_rsa.pub 172.16.12.15
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/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@172.16.12.15's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '172.16.12.15'"
and check to make sure that only the key(s) you wanted were added.

[root@geili gsc-test]# ssh 172.16.12.15
Last failed login: Fri Nov  1 18:35:24 CST 2019 from 172.16.12.10 on ssh:notty
There were 2 failed login attempts since the last successful login.
Last login: Fri Nov  1 18:16:32 2019 from 172.16.12.10

如上,无密码访问了。

当然,也可以从别人那里接收到公钥,

 scp .ssh/id_rsa.pub 172.16.254.200:/root/.ssh/authorized_keys

这个指令,后边已经很清楚了,.ssh/authorized_keys那个就是公钥存放位置.

所以原理上,将本机的id_rsa.pub中内容,拷贝到远程主机的authorized_keys文件中,即可实现本机无密码访问远程主机。

ssh-agent
ssh-agent是ssh代理程序,用于管理 ssh private keys,目的是对解密的私钥进行高速缓存。
ssh-add 提示并将用户使用的私钥添加到 ssh-agent 维护列表中,此后当公钥连接到远程 SSH 或 SCP 主机时,不再提示信息。

1、使用不同密钥访问不同主机时,需要手动指定相应密钥,ssh代理会帮助我们选择对应的密钥,不用手动选择。
2、当私钥设置了密码,我们又需要频繁的使用私钥进行认证时,ssh代理可以帮助我们免去重复的输入密码的操作。

方式一:创建子shell,在子shell中运行ssh-agent进程,退出子shell自动结束代理。

ssh-agent $SHELL

方式二:单独启动一个代理进程,退出当前shell时最好使用ssh-agent -k关闭对应代理

eval `ssh-agent`

关闭agent

ssh-agent -k

将私钥添加到代理

ssh-add ~/.ssh/key_name

查看代理中的私钥

ssh-add -l

查看代理中的公钥

ssh-add -l

移除指定的私钥

ssh-add -d /path/of/key/key_name

移除所有私钥

ssh-add -D

ssh的配置:

主要操作都针对于 /etc/ssh/sshd-_cponfig配置文件

1、修改默认端口,禁止root直连:

Port 54321   		//端口

PermitRootLogin no   //禁止root用户直连

这个是默认开启,将这个改成no就不能用root直连了。

这种情况,访问的方法:
xshell:

[C:\~]$ ssh user1@172.16.12.92 54321

然后输入用户的密码。
再切换为root

[user1@zabbix-ms ~]$ su - 
Password: 

centos:

[root@git_server ~]# ssh -p 54321 user1@172.16.12.92
[root@git_server ~]# scp -P 24678 /etc/passwd user1@172.16.12.21:/tmp/

2、普通用户的管理

熵池设置密码

[root@git_server ~]# tr -dc a-zA-Z0-9_# < /dev/urandom | head -c 10 | xargs
af8Rx6Adfd

3、配置空闲超时时间

ClientAliveInterval 10   	//客户端活动时间 10秒
ClientAliveCountMax 0		//最大连接数0是没有上限

时间内不做操作自动断开。

4、约束可以访问的用户

白名单

Allowusers user1 user2

就只有user1 和 user2 能访问。

5、登录提示

用到了/etc/motd文件,就是在用户登录时提示一些信息。

6、公钥指纹

用来确定法服务器是否真是存在。

抽取指纹信息:
(1)ecdsa
[root@node1 /etc/ssh]# ssh-keygen -l -f ssh_host_ecdsa_key
256 SHA256:jOEXinqolv98uWCOmX36+5aUx/c3fvwUcTpTxMTEEvI ssh_host_ecdsa_key.pub (ECDSA)
[root@node1 /etc/ssh]# ssh-keygen -l -E md5 -f ssh_host_ecdsa_key
256 MD5:e0:99:39:73:83:c0:47:e6:4c:7f:73:d3:ce:96:8f:e2 ssh_host_ecdsa_key.pub (ECDSA)

(2)ed25519
[root@node1 /etc/ssh]# ssh-keygen -l -f ssh_host_ed25519_key
256 SHA256:ZAjGP6ZLLCGFt7T6441wck6OK1kIX9qhjsu3HCY0HA0 ssh_host_ed25519_key.pub (ED25519)
[root@node1 /etc/ssh]# ssh-keygen -l -E md5 -f ssh_host_ed25519_key
256 MD5:b8:09:6d:c9:32:6b:d2:6b:04:3d:61:2f:a4:8d:0d:3b ssh_host_ed25519_key.pub (ED25519)

(3)rsa
[root@node1 /etc/ssh]# ssh-keygen -l -f ssh_host_rsa_key
2048 SHA256:TywqvmVFbrETMsr0WESc2Jmvr6zUCB7Aa3f9FvNg3eA ssh_host_rsa_key.pub (RSA)
[root@node1 /etc/ssh]# ssh-keygen -l -E md5 -f ssh_host_rsa_key
2048 MD5:b9:91:a0:6d:bf:8e:97:fc:5c:7f:f3:c7:dc:40:08:84 ssh_host_rsa_key.pub (RSA)

将公钥指纹保存到文档。

[root@node2 ~]# ssh -p 24678 user1@172.16.0.21
The authenticity of host '[172.16.0.21]:24678 ([172.16.0.21]:24678)' can't be established.
ECDSA key fingerprint is SHA256:jOEXinqolv98uWCOmX36+5aUx/c3fvwUcTpTxMTEEvI.
ECDSA key fingerprint is MD5:e0:99:39:73:83:c0:47:e6:4c:7f:73:d3:ce:96:8f:e2.
Are you sure you want to continue connecting (yes/no)? 

7、ssh+while

while read r
do
	##r 就是IP
	sshpass -p 123456 ssh -o StrictHostKeyChecking=no user1@${r} touch /tmp/f1
done  < /root/ip.txt

上边这个只会执行一次然后就停止了。

ssh会吃掉剩下的,要这样做才行。

sshpass -p 123456 ssh -o StrictHostKeyChecking=no user1@${r} touch /tmp/f1 < /dev/null

后边加上这个/dev/null

8、ssh密钥:

PasswordAuthentication no改为yes

这个选项centos默认是yes的,这时是可以用密码进行登录。
但是在SuSE_linux等中,会默认为no,如果为no时不能用密码登录而是要密钥进行登录。

如下图:

在这里插入图片描述

改成yes就可以通过密码登录了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值