Linux SSH 服务器的一些配置

本文详细介绍了如何在Linux环境下从检查SSH服务版本开始,到完全卸载旧版本、源码安装新版本、配置服务器端及客户端的过程。同时,还提供了如何设置SSH服务随系统启动、配置文件详解以及调整连接超时时间等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

    ssh 是linux 服务器上管理必备的工具吧。经常使用,这里把一些基本的配置坐下记录,部分资料来源于网络整理。

一、 检查原有ssh服务版本号
rpm –qa|grep ssh或者rpm –qa|grep openssh
显示已安装服务端和客户端连接程序
openssh-server-3.6.1p2-33.30.1
openssh-3.6.1p2-33.30.1
openssh-clients-3.6.1p2-33.30.1
以上程序需要先删除后再安装
二、 删除原有安装的Ssh
rpm –e openssh
rpm -e netdump-0.6.11-3
rpm -e openssh-server-3.6.1p2-33.30.1
rpm -e openssh-clients-3.6.1p2-33.30.1
三、 源码安装ssh
1、 tar zxvf ssh-3.2.9.1.tar.gz
2、 cd ssh-3.2.91
3、 ./configure –enable-static
4、 make
5、 make install
四、 配置服务器端Ssh
1、 mkdir /home/bin
2、 cp /usr/local/bin/ssh-dummy-shell.static /home/bin/ ssh-dummy-shell
3、 cp /usr/local/bin/sftp-server2.static /home/bin/sftp-server
4、 vi /etc/ssh2/sshd2.config
subsystem-sftp          /usr/local/bin/sftp-server
ChRootUsers           anonymous,ftp,guest,skyzhw,aa
ChRootGroups          sftp,guest
5、 mk /home/$userDir/bin
6、 ln /home/ssh-dummy-shell /home/$userDir/bin/ ssh-dummy-shell
7、 ln /home/sftp-server    /home/$userDir/bin/sftp-server
8、 groupadd sftp
9、 useradd –s /bin/ssh-dummy-shell –g sftp $userName
五、 启动Ssh服务器
/usr/local/sbin/sshd
六、 添加Ssh服务到系统启动项
1、vi /etc/rc.d/rc.local
添加/usr/local/sbin/sshd

ssh 配置文件的一些配置:

[root@localhost ~]#  vi /etc/ssh/sshd_config   # 用vi打开SSH的配置文件 
Protocol 2 # 修改后变为此状态,仅使用SSH2
ServerKeyBits 1024 # 修改后变为此状态,将ServerKey强度改为1024比特
PermitRootLogin no  # 修改后变为此状态,不允许用root进行登录
PasswordAuthentication no # 修改后变为此状态,不允许密码方式的登录
PermitEmptyPasswords no  # 修改后变为此状态,禁止空密码进行登录
然后保存并退出。
因为我们只想让SSH服务为管理系统提供方便,所以在不通过外网远程管理系统的情况下,只允许内网客户端通过SSH登录到服务器,以最大限度减少不安全因素。设置方法如下:
[root @ localhost ~]#  vi /etc/hosts.deny   # 修改屏蔽规则,在文尾添加相应行 
#
# hosts.deny This file describes the names of the hosts which are
# *not* allowed to use the local INET services, as decided
# by the '/usr/sbin/tcpd' server.
#
# The portmap line is redundant, but it is left to remind you that
# the new secure portmap uses hosts.deny and hosts.allow. In particular
# you should know that NFS uses portmap!
sshd: ALL  # 添加这一行,屏蔽来自所有的SSH连接请求
[root @ localhost ~]#  vi /etc/hosts.allow   # 修改允许规则,在文尾添加相应行 
#
# hosts.allow This file describes the names of the hosts which are
# allowed to use the local INET services, as decided
# by the '/usr/sbin/tcpd' server.
#
sshd: 192.168.0.  # 添加这一行,只允许来自内网的SSH连接请求 
重新启动SSH服务
在修改完SSH的配置文件后,需要重新启动SSH服务才能使新的设置生效。
[root @ localhost ~]#  /etc/rc.d/init.d/sshd restart   # 重新启动SSH服务器 
Stopping sshd:             [ OK ]
Starting sshd:             [ OK ]  ← SSH服务器重新启动成功?
修改ssh过期时间
注意远程修改 ssh 后重启ssh会导致无法远程登录

连接SSH服务器刚刚离开一会就断开,不得不需要反复连接服务器,为了使SSH服务器可以保持足够的连接时间,大家可以按以下方法设置:
基于安全的理由,如果用户连线到 SSH Server 后闲置,SSH Server 会在超过特定时间后自动终止 SSH 连线。以下是设定终止连线时间的方法:

1、打开 /etc/ssh/sshd_config 文件,找到一个参数为 ClientAliveCountMax,它是设定用户端的 SSH 连线闲置多长时间后自动终止连线的数值,单位为分钟。
2、如果这一行最前面有#号,将那个#号删除,并修改想要的时间。
3、修改后保存并关闭文件,重新启动 sshd:
/etc/rc.d/init.d/sshd restart
4. vim .bash_profile
export TMOUT=1000000

转载于:https://my.oschina.net/congqian/blog/137815

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值