ssh连接不上,连接慢:

连接不上线时候先来看看网是不是通的:

ping 192.168.8.121

如果通了再来看看端口是否是通的:

telnet 192.168.8.121 22

再来看看配置文件:

host.conf    hosts        hosts.allow  hosts.deny

host.conf:用来指定如何解析主机域名。可设置网络安全。

        order 是解析顺序的参数,order hosts,bind,nis //说明先查询解析/etc/hosts文件,然后DNS,再是NIS

        multi on表示是否运行/etc/hosts文件允许主机指定多个多个地址 ,on表示运行

        nospoof on是否允许服务器对ip地址进行其欺骗,这里的on表示不允许

        rccorder如果被设置为on,那么所有查询将被重新排序。

hosts:配置本地DNS解析

hosts.allow:配置允许连接的地址

hosts.deny:配置拒绝连接的地址


ssh连接慢:改为如下

vim /etc/ssh/sshd_config
UseDNS: no
GSSAPIAuthentication: no


不输入yes登陆ssh:或修改ssh配置文件

ssh -o StrictHostKeyChecking=no 192.168.1.10


连续跳板机登陆机器:

ssh -t 192.168.1.10 ssh -t 192.168.1.11 ssh 192.168.1.12

远程图形桌面在本机显示:两台机器都是图形界面的情况下

ssh -X 192.168.1.12
gedit


非交互式修改密码:

echo -e '123456\n123456' | passwd

将交互式改为非交互式:

sshpass -p 123456 ssh -o StrictHostKeyChecking=no 192.168.1.10 'date'

非交互式拷贝公钥:

sshpass -p 123456 ssh-copy-id .ssh/id_rsa.pub -o StrictHostKeyChecking=no 192.168.1.10


批量分发公钥脚本:

HOSTS="
10.0.0.6
10.0.0.7
10.0.0.18
10.0.0.28
"
PASS=123456
ssh-keygen  -P ""  -f /root/.ssh/id_rsa &> /dev/null
rpm -q sshpass &> /dev/null || yum -y install sshpass &> /dev/null
for i in $HOSTS;do
{
    sshpass  -p $PASS ssh-copy-id -o  StrictHostKeyChecking=no -i
    /root/.ssh/id_rsa.pub $i &> /dev/null
}&
done
wait


ssh转发端口:

转发本地端口:

ssh -L localport:remotehost:remotehostport   sshserver

转发远程端口:

ssh -R sshserverport:remotehost:remotehostport sshserver

-f 后台启用

-N 不打开远程shell,处于等待状态

-g 启用网关功能


多台机器之间互相免秘钥:

ssh-keygen
ssh-copy-id 127.0.0.1
rsync -av .ssh IP1:/root
rsync -av .ssh IP2:/root
rsync -av .ssh IP3:/root


开启密钥密码代理:

ssh-agent
ssh-add # 添加ssh密码
ssh 10.0.0.8 # 这样秘钥有密码也不用输入了,ssh-agent代理的就是输秘钥的过程


ssh通过Xmanager等Xserver在用户端显示图形界面:

1、windows上安装Xmanager工具。

2、安装带有图形的软件:

yum install xorg-x11-xauth xorg-x11-fonts-* xorg-x11-font-utils  xorg-x11-fonts-Type1 -y
yum -y install xclock

3、启动软件

~]# export DISPLAY=192.168.1.10:0.0  # 这个IP是windows客户端的IP,0.0表示第几个要显示的图形界面
~]# xclock


ssh作为代理:

代理某个服务:使用场景就是远程的某台机器可以连接目标机器,但是本地无法直连目标机器。

ssh -fNL 1234:192.168.1.8:80 192.168.1.18 # 192.168.1.18是中间机器IP,192.168.1.8是最终要被代理的服务IP,1234是映射到本地的端口
curl 127.0.0.1:1234

ssh -R代理内网服务:

ssh -CqTfnN -R 0.0.0.0:7233:192.168.0.6:22 root@47.103.91.6
# 就是将192.168.0.6的22端口映射到7.103.91.6的7233端口,然后就可以在任意地方访问内网的22的服务了

配置:/etc/ssh/sshd_config

Port    #端口
ListenAddress ip
LoginGraceTime 2m
PermitRootLogin yes         #默认ubuntu不允许root远程ssh登录
StrictModes yes             #检查.ssh/文件的所有者,权限等
MaxAuthTries   6            #pecifies the maximum number of authentication 
# attempts permitted per connection. Once the number of failures reaches half this 
# value, additional failures are logged. The default is 6.
MaxSessions  10              #同一个连接最大会话
PubkeyAuthentication yes     #基于key验证
PermitEmptyPasswords no      #空密码连接
PasswordAuthentication yes   #基于用户名和密码连接
GatewayPorts no
ClientAliveInterval 10       #单位:秒
ClientAliveCountMax 3        #默认3
UseDNS yes                   #提高速度可改为no
GSSAPIAuthentication yes     #提高速度可改为no
MaxStartups                  #未认证连接最大值,默认值10
Banner /path/file

#以下可以限制可登录用户的办法:
AllowUsers user1 user2 user3
DenyUsers user1 user2 user3
AllowGroups g1 g2
DenyGroups g1 g2

sshfs挂载目录工具:

yum install sshfs -y
sshfs 192.168.199.8:/data /mnt