当前位置: 移动技术网 > IT编程>开发语言>Java > Linux系统下ssh使用总结

Linux系统下ssh使用总结

2020年07月29日  | 移动技术网IT编程  | 我要评论

转自:https://www.cnblogs.com/kevingrace/p/6110842.html

-bash: ssh: command not found
解决办法;
yum install -y openssh-server openssh-clinets

(0)ssh登录时提示:Read from socket failed: Connection reset by peer.
尝试了很多解决方案均无效,无奈!卸载sshd,然后重新安装
# yum remove openssh*
# rm -rf /etc/ssh*
# yum install -y openssh*
# systemctl start sshd.service

(1)ssh远程登陆后的提示信息,标题信息
我们经常会使用中控机ssh信任跳转到其他机器上,但是不知道有没有运维朋友注意到ssh跳转成功后的终端显示的提示信息?
这些提示信息,是为了方便我们在第一时间知道ssh跳转到哪台目标机上,也是为了避免长期频繁跳转后由于大意造成的误入机器操作的风险,我们通常会在ssh跳转到目标机器后显示一些提示信息,在一些国家, 登入给定系统前, 给出未经授权或者用户监视警告信息, 将会受到法律的保护.如下:
[root@bastion-IDC ~]# ssh -p22 192.168.1.15
Last login: Fri Jul 15 13:26:53 2016 from 124.65.197.154
===================================
|||||||||||||||||||||||||||||||||||
===================================
HOSTNAME: monit-server
IPADDRES: 192.168.1.15
===================================
IDC监控机
===================================

那么上面红色区域的提醒信息是在哪设置的呢?
做法一:其实很简单,这些信息是在目标机器的/etc/motd文件里自定义的
[root@monit-server ~]# cat /etc/motd 
===================================
|||||||||||||||||||||||||||||||||||
===================================
HOSTNAME: monit-server
IPADDRES: 192.168.1.15
===================================
IDC监控机
===================================

做法二:在目标机器的/etc/ssh/sshd_config文件里定义,然后重启sshd服务即可。这两种做法是一致的效果!
Banner /etc/sshfile

[root@host-192-168-1-117 ~]# cat /etc/sshfile
this is 192.168.1.117

远程登陆:
[root@linux-node2 ~]# ssh 192.168.1.117
this is 192.168.1.117
[root@host-192-168-1-117 ~]#

(2)实现SSH无密码登录:使用ssh-keygen和ssh-copy-id
ssh-keygen 产生公钥与私钥对.
ssh-copy-id 将本机的公钥复制到远程机器的authorized_keys文件中,ssh-copy-id也能让你有到远程机器的/home/username/.ssh和~/.ssh/authorized_keys的权利.
操作记录:
1)第一步:在本地机器上使用ssh-keygen产生公钥私钥对
#ssh-keygen -t rsa //一路默认回车
这样就会在当前用户家目录下的.ssh目录里产生公钥和私钥文件:id_rsa.pub、id_rsa。可以将id_rsa.pub公钥文件复制成authorized_keys
2)第二步:可以手动将本机的id_rsa.pub公钥文件内容复制到远程目标机的.ssh/authorized_keys文件中,可以就可以实现ssh无密码登陆。
当然,也可以在本机直接使用ssh-copy-id将公钥复制到远程机器中
#ssh-copy-id -i /root/.ssh/id_rsa.pub user@ip [把本机的公钥拷贝到远程机器上,比如B机器]
也可以不加公钥路径,会默认加上
#ssh-copy-id user@ip 
注意: 
ssh-copy-id 将key写到远程机器的 ~/ .ssh/authorized_key.文件(文件会自动创建)中

1

2

对于非22端口(比如22222)情况下的ssh-copy-id的使用,需要这样用:

ssh-copy-id -i  /root/.ssh/id_rsa.pub  '-p 22222 root@192.168.18.18'

3)这样,本机登录到上面远程机器(B机器)就不用输入密码
#ssh user@ip

(3)ssh登录失败,报错:Pseudo-terminal will not be allocated because stdin
现象:
需要登录线上的一台目标机器A,但是不能直接登录(没有登录权限),需要先登录B机器,然后从B机器跳转到A机器。
脚本如下:
localhost:~ root# cat IDC-7.sh
#!/bin/bash
ssh root@101.201.114.106 "ssh -p25791 root@103.10.86.7"

但是在执行脚本的时候报错如下:
Pseudo-terminal will not be allocated because stdin

原因:
伪终端将无法分配,因为标准输入不是终端。
解决办法:
需要增加-t -t参数来强制伪终端分配,即使标准输入不是终端。
在脚本里添加-t -t参数即可,如下:
localhost:~ root# cat IDC-7.sh
#!/bin/bash
ssh root@101.201.114.106 "ssh -t -t -p25791 root@103.10.86.7"

或者
localhost:~ root# cat IDC-7.sh
#!/bin/bash
ssh -t root@101.201.114.106 "ssh -t -t -p25791 root@103.10.86.7"

(4)ssh远程登陆缓慢问题
解决办法:
编译/etc/ssh/sshd_config配置文件:
UseDNS no
GSSAPIAuthentication no
然后重启sshd服务即可!

(5)ssh登录出现:permission denied(publickey.gssapi-with-mic)
解决方法:
修改/etc/ssh/sshd-config文件,将其中的:
PermitRootLogin no修改为yes
PubkeyAuthentication yes 
AuthorizedKeysFile .ssh/authorized_keys前面加上#屏蔽掉
PasswordAuthentication no修改为yes
最后重启sshd服务即可!

(6)ssh连接错误问题
1)在使用ssh或scp或rsync远程连接的时候,出现如下报错:
Address **** maps to localhost, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
解决方法:
修改本机ssh_config文件 
[root@kvmserver ~]# vim /etc/ssh/ssh_config 
GSSAPIAuthentication no
[root@kvmserver ~]#/etc/init.d/sshd restart

问题迎刃而解~~

2)本机scp、rsync命令都已具备,但是在使用scp或rsync远程同步的时候报错:
bash: scp: command not found
bash: rsync: command not found
原因:是由于远程机器上没有安装scp或rsync造成的!安装这两个命令即可~
yum install openssh-clients
yum install rsync

3)远程ssh连接时错误“ The X11 forwarding request was rejected!”
解决方法:
将sshd_config中 设置 X11Forwarding yes
重启sshd服务。

(7)ssh连接超时被踢出问题解决
当使用xshell,SecureCRT等客户端访问linux服务器,有时候会出现终端定期超时被踢出的情况。
下面介绍三种方法来防止超时被踢出的方法,后两种情况的设置方法以及通过设置shell变量来达到此目的的方法:

1、 配置服务器
#vi /etc/ssh/sshd_config
1)找到 ClientAliveInterval参数,如果没有就自己加一行
数值是秒,比如你设置为120 ,则是2分钟
ClientAliveInterval 120
2)ClientAliveCountMax
指如果发现客户端没有响应,则判断一次超时,这个参数设置允许超时的次数。如3 、5等自定义

修改两项参数后如下:
----------------------------
ClientAliveInterval 120
ClientAliveCountMax 3                      //0 不允许超时次数
修改/etc/ssh/sshd_config文件,将 ClientAliveInterval 0和ClientAliveCountMax 3的注释符号去掉,将ClientAliveInterval对应的0改成60,没有就自己输入。
ClientAliveInterval指定了服务器端向客户端请求消息 的时间间隔, 默认是0, 不发送.而ClientAliveInterval 60表示每分钟发送一次, 然后客户端响应, 这样就保持长连接了.ClientAliveCountMax, 使用默认值3即可.ClientAliveCountMax表示服务器发出请求后客户端没有响应的次数达到一定值, 就自动断开. 正常情况下, 客户端不会不响应.
重新加载sshd服务。退出客户端,再次登陆即可验证。
3)重启sshd service
sudo /etc/init.d/ssh restart

2、 配置客户端
#vim /etc/ssh/ssh_config
然后找到里面的
ServerAliveInterval
参数,如果没有你同样自己加一个就好了
参数意义相同,都是秒数,比如5分钟等
ServerAliveInterval 300

3、echo export TMOUT=1000000 >> /root/.bash_profile; source .bash_profile
在Linux 终端的shell环境中通过设置环境变量TMOUT来阻止超时。如果显示空白,表示没有设置, 等于使用默认值0, 一般情况下应该是不超时. 如果大于0, 可以在如/etc/profile之类文件中设置它为0.

(8)ssh远程登陆,公钥授权不通过:Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
公司IDC机房服务器,之前做了跳板机环境,其他机器只允许从跳板机ssh无密码信任过去,并且在信任关系做好后,禁用了其他机器的密码登陆功能(sshd_config文件里设置“PermitEmptyPasswords no”)

后来跳板机出现了问题,打算重装这台机器,重装前取消了其他机器里只允许跳板机ssh信任关系,并且恢复了密码登陆功能:
[root@bastion-IDC ssh]# vim /etc/ssh/sshd_config
PermitEmptyPasswords yes
[root@bastion-IDC ssh]# service sshd restart

修改后,当时在其他机器间是可以ssh相互登陆,当时没在意,以为一切ok了。
可是,到了第二天,再次ssh登陆时,尼玛,居然报错了~~
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

最后发现是selinux惹的祸!关闭它即可。
1)临时关闭selinux
[root@bastion-IDC ssh]# setenforce 0
[root@bastion-IDC ssh]# getenforce 
Permissive
2)永久关闭
[root@bastion-IDC ssh]# vim /etc/sysconfig/selinux 
SELINUX=disabled
[root@bastion-IDC ssh]# reboot #重启系统才能生效

说明:
1)ssh可同时支持publickey和password两种授权方式,publickey默认不开启,需要配置为yes。 
如果客户端不存在.ssh/id_rsa,则使用password授权;存在则使用publickey授权;如果publickey授权失败,依然会继续使用password授权。

2)GSSAPI身份验证.
GSSAPIAuthentication 是否允许使用基于 GSSAPI 的用户认证.默认值为"no".仅用于SSH-2.
GSSAPICleanupCredentials 是否在用户退出登录后自动销毁用户凭证缓存。默认值是"yes".仅用于SSH-2.
需要特别注意的是:
GSSAPI是公共安全事务应用程序接口(GSS-API)
公共安全事务应用程序接口以一种统一的模式为使用者提供安全事务,由于它支持最基本的机制和技术,所以保证不同的应用环境下的可移植性.
该规范定义了GSS-API事务和基本元素,并独立于基本的机制和程序设计语言环境,并借助于其它相关的文档规范实现.

如果我们在服务端打开GSSAPIAuthentication配置项,如下:
[root@server ~]#vim /etc/ssh/sshd_config
........
GSSAPIAuthentication yes
GSSAPICleanupCredentials yes

那么在客户端登录服务端会用gssapi-keyex,gssapi-with-mic进行身份校验,同样客户端也要支持这种身份验证,如下:

[root@client ~]#vim /etc/ssh/ssh_config
GSSAPIAuthentication yes
GSSAPIDelegateCredentials yes

我们在客户端连接SSH服务端,如下:
ssh -v 192.168.1.11
.................
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password

我们看到如下的信息:
debug1: Unspecified GSS failure. Minor code may provide more information
No credentials cache found
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Next authentication method: gssapi-keyex
debug1: No valid Key exchange context
说明SSH登录时采用GSSAPI的方式进行身份验证,但我们的系统不支持.

最后如果我们不用这种方式进行身份验证的话,建议关闭这个选项,这样可以提高验证时的速度.

(9)ssh自定义安全设置
1)为了ssh登陆的时候加一层保护,可以修改默认端口。修改ssh服务配置文件/etc/ssh/sshd_config
port 2222

这样远程连接时加短裤
#ssh 192.168.1.83 -p 2222

2)ssh使用时加-l后面跟用户名,表示登陆到对方的这个用户下面。
#ssh -l wangshibo 192.168.1.83 -p 2222
等同于
#ssh wangshibo@192.168.1.83 -p 2222

3)限制ssh登陆的来源ip,白名单设置(hosts.allow优先级最高,具体参考:)
一是通过iptables设置ssh端口的白名单,如下设置只允许192.168.1.0/24网段的客户机可以远程连接本机
#vim /etc/sysconfig/iptables
-A INPUT -s 192.168.1.0/24 -p tcp -m state --state NEW -m tcp --dport 2222 -j ACCEPT
二是通过/etc/hosts.allow里面进行限制(如下),/etc/hosts.deny文件不要任何内容编辑,保持默认!
#vim /etc/hosts.allow
sshd:192.168.1.*,192.168.9.*,124.65.197.154,61.148.60.42,103.10.86.7:allow
sshd:all:deny

4)仅允许特定的用户通过SSH登陆
如不允许root用户登录;
只允许几个指定的用户登录(比如wangshibo、guohuihui、liuxing用户)
禁止某些指定的用户登录(比如zhangda,liqin用户)
但是要注意:设置的这几个用户必须同时存在于本机和对方机器上
修改ssh服务配置文件/etc/ssh/sshd_config
PermitRootLogin no //将yes修改为no
AllowUsers      wangshibo guohuihui liuxing //这个参数AllowUsers如果不存在,需要手动创建,用户之间空格隔开
DenyUsers      zhagnda liqin //这个参数DenyUsers如果不存在,需要手动创建,用户之间空格隔开

也可以设置仅允许某个组的成员通过ssh访问主机。
AllowGroups wheel ops

实例说明:

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

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

1)只允许指定用户进行登录(白名单):

在 /etc/ssh/sshd_config 配置文件中设置 AllowUsers 选项。格式如下:

 

AllowUsers    root grace kevin app         

表示只允许grace用户、kevin用户通过ssh登录本机。

 

AllowUsers    root@192.168.10.10 app@192.168.10.11 kevin@192.168.10.13         

表示只允许从192.168.10.10登录的root用户、从192.168.10.11登录的app用户、从192.168.10.13登录的kevin用户可以通过ssh登录本机。

 

 

2)只拒绝指定用户进行登录(黑名单):)

/etc/ssh/sshd_config配置文件中设置DenyUsers选项。格式如下:  

 

DenyUsers    wangbo linan zhangyang    

表示拒绝wangbo、linan和zhangyang用户通过ssh登录本机。

 

需要注意的是:

- AllowUsers、DenyUsers跟后面的配置之间使用TAB键进行隔开

- 多个百名单或黑名单之间使用空格隔开

 

 

例子:

[root@Centos6 ~]# cat /etc/ssh/sshd_config

.......

AllowUsers    root@192.168.10.202 app@192.168.10.200 kevin@192.168.10.202

[root@Centos6 ~]# /etc/init.d/sshd restart

 

 

[root@Centos6 ~]# cat /etc/ssh/sshd_config

.......

AllowUsers    root app kevin

[root@Centos6 ~]# /etc/init.d/sshd restart

 

 

[root@Centos6 ~]# cat /etc/ssh/sshd_config

.......

DenyUsers    wangbo linan zhangyang

[root@Centos6 ~]# /etc/init.d/sshd restart

 

如下,由于已经允许了app和root登录,则后面针对root@192.168.10.202和app@192.168.10.200的限制就无效了(两者别放在一起配置)

[root@Centos6 ~]# cat /etc/ssh/sshd_config

.......

AllowUsers    app root root@192.168.10.202 app@192.168.10.200

[root@Centos6 ~]# /etc/init.d/sshd restart

================================================================
除了上面的方法可以限制ssh指定用户登录外,还可以使用pam规则进行设置。

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

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

1)允许指定的用户(比如kevin、grace账号)进行登录

/etc/pam.d/sshd文件第一行加入,一定要在第一行,因为规则是自上而下进行匹配的。

auth required pam_listfile.so item=user sense=allow file=/etc/sshusers onerr=fail

 

然后在/etc下建立sshusers文件,编辑这个文件,加入你允许使用ssh服务的用户名,不用重新启动sshd服务。

最后重启sshd服务即可!

 

操作如下:

[root@docker-test1 ~]# vim /etc/pam.d/sshd

#%PAM-1.0

auth required pam_listfile.so item=user sense=allow file=/etc/sshusers onerr=fail

........

 

[root@docker-test1 ~]# touch /etc/sshusers

[root@docker-test1 ~]# vim /etc/sshusers

kevin

grace

 

[root@docker-test1 ~]# /etc/init.d/sshd restart

 

 

2)pam规则也可以写成deny的。比如拒绝kevin、grace账号进行登录

操作如下:

[root@docker-test1 ~]# vim /etc/pam.d/sshd

#%PAM-1.0

auth required pam_listfile.so item=user sense=deny file=/etc/sshusers onerr=succeed

........

 

[root@docker-test1 ~]# touch /etc/sshusers

[root@docker-test1 ~]# vim /etc/sshusers

kevin

grace

 

[root@docker-test1 ~]# /etc/init.d/sshd restart

 

3)pam规则可以使用group限制。

 

允许规则:

auth required pam_listfile.so item=group sense=allow file=/etc/security/allow_groups onerr=fail

 

禁止规则:

auth required pam_listfile.so item=group sense=deny file=/etc/security/deny_groups onerr=succeed

 

 

操作如下:

[root@docker-test1 ~]# vim /etc/pam.d/sshd  

#%PAM-1.0

auth required pam_listfile.so item=group sense=allow file=/etc/security/allow_groups onerr=fail

 

新建一个组,组名为bobo,然后将kevin和grace添加到这个bobo组内

[root@docker-test1 ~]# groupadd bobo

[root@docker-test1 ~]# gpasswd -a kevin bobo

Adding user kevin to group bobo

[root@docker-test1 ~]# usermod -G bobo grace

[root@docker-test1 ~]# id kevin

uid=1000(kevin) gid=1000(kevin) groups=1000(kevin),1002(bobo)

[root@docker-test1 ~]# id grace

uid=1001(grace) gid=1001(grace) groups=1001(grace),1002(bobo)

 

/etc/security/allow_groups文件按中加入组名(注意如果不加root,则root就不能被允许登录了)

[root@docker-test1 ~]# vim /etc/security/allow_groups

bobo

 

[root@docker-test1 ~]# /etc/init.d/sshd restart

 

如上设置后,则只有kevin用户能被允许登录!

如果是禁止规则,则第一行改为下面内容:

auth required pam_listfile.so item=group sense=deny file=/etc/security/deny_groups onerr=succeed

================================================================
除此之外,禁止某些用户ssh登录,可以使用passwd或usermod命令进行账号锁定。
https://www.cnblogs.com/kevingrace/p/6109818.html

5)取消密码验证,只用密钥对验证
修改ssh服务配置文件/etc/ssh/sshd_config
PasswordAuthentication no
PubkeyAuthentication yes

6)给账号设置强壮的密码:将密码保存到文本进行复制和粘帖就可以了
# rpm -ivh expect-5.43.0-5.1.i386.rpm| yum -y install expect
# mkpasswd -l 128 -d 8 -C 15 -s 10                  //将下面密码保存到文本进行复制、粘贴即可
lVj.jg&sKrf0cvtgmydqo7qPotxzxen9mefy?ej!kcaX2gQrcu2ndftkeamllznx>iHikTagiVz0$cMtqOcIypkpd,vvD*kJhs3q@sb:CiCqgtqdqvse5lssfmranbtx
参数说明:
-l 密码长度
-d 多少个数字
-C 大写字母个数
-s 特殊符号的个数

7)只允许通过指定的网络接口来访问SSH服务,(如果本服务器有多个IP的时候)
仍然是修改/etc/ssh/sshd_config,如下:
ListenAddress 192.168.1.15                //默认监听的是0.0.0.0

这样,就只允许远程机器通过ssh连接本机的192.168.1.15内网ip来进行登陆了。

8)禁止空密码登录
如果本机系统有些账号没有设置密码,而ssh配置文件里又没做限制,那么远程通过这个空密码账号就可以登陆了,这是及其不安全的!
所以一定要禁止空密码登陆。修改/etc/ssh/sshd_config,如下:
PermitEmptyPasswords no //这一项,默认就是禁用空密码登陆

9) ssh_config和sshd_config
ssh_config和sshd_config都是ssh服务器的配置文件,二者区别在于,前者是针对客户端的配置文件,后者则是针对服务端的配置文件。两个配置文件都允许你通过设置不同的选项来改变客户端程序的运行方式。sshd_config的配置一般都比较熟悉,下面单独说下ssh_config针对客户端的配置文件:

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

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

[root@dns01 dns_rsync]# cat /etc/ssh/ssh_config

# Site-wide defaults for various options

   Host *

        ForwardAgent no

        ForwardX11 no

        RhostsAuthentication no

        RhostsRSAAuthentication no

    ‍    RSAAuthentication yes

        PasswordAuthentication yes

        FallBackToRsh no

        UseRsh no

        BatchMode no

        CheckHostIP yes

        StrictHostKeyChecking no

        IdentityFile ~/.ssh/identity

        Port 22

        Cipher blowfish

        EscapeChar ~

 

下面对上述选项参数逐进行解释:

# Site-wide defaults for various options

带“#”表示该句为注释不起作,该句不属于配置文件原文,意在说明下面选项均为系统初始默认的选项。说明一下,实际配置文件中也有很多选项前面加有“#”注释,虽然表示不起作用,其实是说明此为系统默认的初始化设置。

Host *

"Host"只对匹配后面字串的计算机有效,“*”表示所有的计算机。从该项格式前置一些可以看出,这是一个类似于全局的选项,表示下面缩进的选项都适用于该设置,可以指定某计算机替换*号使下面选项只针对该算机器生效。

ForwardAgent no

"ForwardAgent"设置连接是否经过验证代理(如果存在)转发给远程计算机。

ForwardX11 no

"ForwardX11"设置X11连接是否被自动重定向到安全的通道和显示集(DISPLAY set)。

RhostsAuthentication no

"RhostsAuthentication"设置是否使用基于rhosts的安全验证。

RhostsRSAAuthentication no

"RhostsRSAAuthentication"设置是否使用用RSA算法的基于rhosts的安全验证。

RSAAuthentication yes

"RSAAuthentication"设置是否使用RSA算法进行安全验证。

PasswordAuthentication yes

"PasswordAuthentication"设置是否使用口令验证。

FallBackToRsh no

"FallBackToRsh"设置如果用ssh连接出现错误是否自动使用rsh,由于rsh并不安全,所以此选项应当设置为"no"

UseRsh no

"UseRsh"设置是否在这台计算机上使用"rlogin/rsh",原因同上,设为"no"

BatchMode no

"BatchMode":批处理模式,一般设为"no";如果设为"yes",交互式输入口令的提示将被禁止,这个选项对脚本文件和批处理任务十分有用。

CheckHostIP yes

"CheckHostIP"设置ssh是否查看连接到服务器的主机的IP地址以防止DNS欺骗。建议设置为"yes"

StrictHostKeyChecking no

"StrictHostKeyChecking"如果设为"yes"ssh将不会自动把计算机的密匙加入"$HOME/.ssh/known_hosts"文件,且一旦计算机的密匙发生了变化,就拒绝连接。

IdentityFile ~/.ssh/identity

"IdentityFile"设置读取用户的RSA安全验证标识。

Port 22

"Port"设置连接到远程主机的端口,ssh默认端口为22。

Cipher blowfish

“Cipher”设置加密用的密钥,blowfish可以自己随意设置。

EscapeChar ~

“EscapeChar”设置escape字符。

======================================================

比如说,A机器的ssh端口是22,B机器的端口是22222,一般来说A机器ssh连接B机器的时候是使用-p22222指定端口。但是可以修改A机器的/etc/ssh/ssh_config文件中的

Port为22222,这样A机器ssh连接的时候就默认使用22222端口了。

centos7下修改ssh默认的22端口后,出现重启ssh失败的现象。可能原因:
selinux没有关闭
# setenforce 0
# sed -i 's/SELINUX=enforcing/SELINUX=disabled' /etc/sysconfig/selinux
# reboot

修改ssh端口后,远程ssh连接不上。可能原因:
firewalld防火墙没有关闭,默认的22端口是开启的,如果防火墙没有关闭,则ssh修改后的新端口是不通的。
# systemctl stop firewalld
# systemctl disable firewalld
# firewall-cmd --state

-------------------------------------------去掉SSH公钥检查的方法(交互式yes/no)------------------------------------------------

SSH公钥检查是一个重要的安全机制,可以防范中间人劫持等黑客攻击。但是在特定情况下,严格的 SSH 公钥检查会破坏一些依赖SSH协议的自动化任务,就需要一种手段能够绕过SSH的公钥检查。
SSH连接远程主机时,会检查主机的公钥。如果是第一次连接该主机,会显示该主机的公钥摘要,弹出公钥确认的提示,提示用户是否信任该主机(Yes/no)。当选择Yes接受,就会将该主机的公钥追加到文件 ~/.ssh/known_hosts 中。当再次连接该主机时,就不会再提示该问题了。
SSH公钥检查有好处,但首次连接时会导致某些自动化任务中断,或者由于 ~/.ssh/known_hosts 文件内容清空,导致自动化任务中断。

去掉SSH公钥检查的方法:
1)SSH客户端的StrictHostKeyChecking 配置指令,可以实现当第一次连接服务器时,自动接受新的公钥。只需要修改 /etc/ssh/ssh_config 文件,包含下列语句:
StrictHostKeyChecking no

2)或者在ssh连接命令中使用-oStrictHostKeyChecking=no参数
[root@puppet ~]# ssh -p22222 172.168.1.33 -oStrictHostKeyChecking=no 
或者
[root@puppet ~]# ssh -p22222 172.168.1.33 -oUserKnownHostsFile=/dev/null -oStrictHostKeyChecking=no

====================ansible中取消ssh交换式yes/no==================
配置文件/etc/ansible/ansible.cfg的[defaults]中(打开注释)
# uncomment this to disable SSH key host checking
host_key_checking = False

=========================设置终端登录超时时间====================

1

2

3

4

5

6

7

远程登录linux服务器,如何设置终端失效时间(即过了多久不操作,终端即将失效)。方法如下:

[root@mq-console-nameserver ~]# vim /etc/profile

......

export TMOUT=600

[root@mq-console-nameserver ~]# source /etc/profile

 

如上设置后,登录这台服务器的终端在10分钟内不做操作,则该终端就将失效!

=======================SSH服务启动报错案例=======================
在某台服务器上部署了sftp服务,最后发现sftp远程登录正常,但是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

[root@kevin ssh]# service sshd restart

Stopping sshd: [ OK ]

Starting sshd:/etc/ssh/sshd_config line 81: Unsupported option GSSAPIAuthentication

/etc/ssh/sshd_config line 83: Unsupported option GSSAPICleanupCredentials

Starting sshd: [ OK ]

 

如上启动后,远程ssh登录这台机器,输入正确的用户名和密码,则会登录失败!!

 

[root@kevin ssh]# ssh -V

OpenSSH_7.6p1, OpenSSL 1.0.1e-fips 11 Feb 2013

 

原因是新版本的openssh不支持以上参数,需要修改sshd的配置文件。

修改内容如下,否则还是无法通过ssh登录这台服务器:

[root@kevin ssh]# vim /etc/ssh/sshd_config

.......

##去掉前面的注释,允许root通过ssh登录

PermitRootLogin yes

##注释掉下面三个参数

#GSSAPIAuthentication yes

#GSSAPICleanupCredentials yes

#UsePAM yes

 

再次重启ssh,上面的报错信息就没有了。此时远程ssh登录就OK了!

[root@kevin ssh]# service sshd restart

Stopping sshd: [ OK ]

Starting sshd: [ OK ]

本文地址:https://blog.csdn.net/weixin_40596016/article/details/85984644

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网