Skip to content
gobomb edited this page Sep 27, 2019 · 12 revisions

ssh 常用配置


修改 ssh 端口

系统:CentOs

vim /etc/ssh/sshd_config

Port=改成其它端口

重启sshd:

systemctl restart sshd

用 ssh 做远程端口转发

机器和端口信息:

A机器:1.1.1.1:2244

B机器:2.2.2.2:22

A向B建立ssh连接,客户机器通过B使用此连接反向连接A

在A机器上:

ssh -N -f -g -R 6666:localhost:2244 root@2.2.2.2

在客户机器访问1.1.1.1:

ssh root@2.2.2.2 -p 6666

数据流向:

1.1.1.1:2244<--2.2.2.2:22<--2.2.2.2:6666<--客户机器

-N:不执行远程指令;

-f:后台执行ssh指令;

-g:允许远程主机连接主机的转发端口;

-R:指派远程上的 port 到本地地址上的 port。

在 2.2.2.2 上

$ netstat -nlp | grep 6666
tcp        0      0 0.0.0.0:6666            0.0.0.0:*               LISTEN      18290/sshd: root
tcp6       0      0 :::6666                 :::*                    LISTEN      18290/sshd: root

若监听绑定的是环回地址,需要进行如下设置:

$ vi /etc/ssh/sshd_config
...
GatewayPorts yes
...

https://www.ibm.com/developerworks/cn/linux/l-cn-sshforward/index.html


新建ssh连接很慢的问题

vim /etc/ssh/sshd_config

找到UseDNS设置为UseDNS no,然后重启

因为默认情况下,sshd会去做DNS解析,在网络不好或无网络连接会很慢或卡顿


CentOS下 systemctl start sshd.service 失败

但手动 /sbin/sshd & 可行。

可能是SELinux的问题

查看SELinux状态:

1、/usr/sbin/sestatus -v ##如果SELinux status参数为enabled即为开启状态

SELinux status: enabled

2、getenforce ##也可以用这个命令检查

关闭SELinux:

1、临时关闭(不用重启机器):

setenforce 0                  ##设置SELinux 成为permissive模式
                              ##setenforce 1 设置SELinux 成为enforcing模式

2、修改配置文件需要重启机器:

修改/etc/selinux/config 文件

SELINUX=enforcing改为SELINUX=disabled

重启机器即可

参考:https://www.jianshu.com/p/b4d7320064ad

ssh 输完密码后马上断开

Connection to xxx closed.
Transferred: sent 4256, received 2504 bytes, in 0.4 seconds
Bytes per second: sent 9616.9, received 5658.0
debug1: Exit status 254

/etc/ssh/sshd_configUsePAM yes该为UsePAM no

ssh-copy-id 过后还是需要密码

检查 ~/.ssh 的权限是否为 0700

~/.ssh/authorized_keys 和 id_rsa.pub 为 0644

id_rsa 0600

Clone this wiki locally