centos 7 修改sshd服务默认端口号

由于最近sshd服务默认端口号22被匿名进行试探性的进行登入,为防止匿名用户再次进行试探性的登入,将sshd服务的默认端口号进行修改。

环境:centos 7.3  xshell

思路:先将sshd的被指文件进行修改,把sshd服务的默认端口号修改为所需要的端口号,然后重启sshd服务,发现重启报错。找其原因是应为selinux不允许自定义sshd服务设置端口放行,然后进行为sshd服务添加selinux放行端口。

实验:

一、使用sshd服务默认端口22进行访问,修改端口默认端口号,重启sshd服务

a)  登入

Connecting to 192.168.204.26:22…

Connection established.

To escape to local shell, press ‘Ctrl+Alt+]’.

        

WARNING! The remote SSH server rejected X11 forwarding request.

Last login: Thu Apr  6
20:26:28 2017 from 192.168.204.1

[chen@192 ~]$

b) 切换root,更改sshd默认端口号

[chen@192 ~]$ su

Password:

[root@192 chen]# vim /etc/ssh/sshd_config

#Port 22

#AddressFamily any

#ListenAddress 0.0.0.0

#ListenAddress ::

#Port 22这行修改为Port 22222,如:

Port 22222

#AddressFamily any

#ListenAddress 0.0.0.0

#ListenAddress ::

c)重启sshd服务

[root@192
chen]# systemctl restart sshd

Job for sshd.service failed because the control process exited with
error code. See “systemctl status sshd.service” and “journalctl
-xe” for details.

二、排错

a)查看防火墙配置

[root@192 conf]# iptables -L

Chain INPUT (policy ACCEPT)

target     prot opt
source               destination        

没有设置防火墙策略。

b)查看shelinux状态,并关闭selinux,尝试重启sshd服务

[root@192
conf]# getenforce

Enforcing

[root@192
conf]# setenforce 0

[root@192
conf]# getenforce

Permissive

[root@192
conf]# systemctl restart sshd

重启成功。

c) 使用自定义sshd端口号远程连接linux主机

Connecting
to 192.168.204.26:22222…

Connection
established.

To escape
to local shell, press ‘Ctrl+Alt+]’.

 

WARNING!
The remote SSH server rejected X11 forwarding request.

Last
login: Thu Apr  6 21:50:15 2017 from
192.168.204.1

[chen@192 ~]$

登入成功,但不能把selinux关闭了,会造成linux主机安全性能下降。

三、修改selinux端口标签

a)启用selinux

[root@192 conf]# setenforce 1

查看selinux状态

[root@192 conf]# getenforce

Enforcing

b) 查看selinuxsshd服务放行的端口号

[root@192
conf]# semanage port -l | grep ssh

ssh_port_t                     tcp      22

c)添加自定义sshd服务端口至selinux

[root@192
conf]# semanage port -a -t ssh_port_t -p tcp 22222

[root@192
conf]# semanage port -l | grep ssh

ssh_port_t                    
tcp      22222, 22

d)测试

 

注意:中途不要断开ssh服务,导致出现的一些问题。

 

 

原创文章,作者:chenbin,如若转载,请注明出处:http://www.178linux.com/72697

(2)
chenbinchenbin
上一篇 2017-04-06 21:49
下一篇 2017-04-07 08:42

相关推荐

  • SELinux详解及其优缺点

    SELinux简介:        SELinux:Secure Enahanced Linux 安全增强的Linux SELinux是2.6版本的Linux内核中提供的强制访问控制(MAC)系统。对于目前可用的Linux安全模块来说,SELinux是功能最全面,而且测试最充分的…

    Linux干货 2017-05-21
  • 文件权限

    文件权限 文件属性       rw-r–r–表示文件权限     Root 表示文件所有者     Root 表示文件所属组     0 表示文件的大小 …

    Linux干货 2016-08-11
  • 第四周作业

    1. 复制/etc/skel目录为/home/tuser1,要求/home/tuser1及其内部文件的属组和其它用户均没有任何访问权限 [root@localhost ~]# chmod -R g=,o=  /home/tuser1 [root@localhost home]# ll drwx——  3 root&…

    Linux干货 2016-12-26
  • 文本处理工具sed

    处理文本的工具sed Stream EDitor,  行编辑器     sed 是一种流编辑器,它一次处理一行内容。处理时,把     当前处理的行存储在临时缓冲区中,称为“模式空间”(     pa…

    Linux干货 2016-08-10
  • N25-第四周

    复制/etc/ske1目录为/home/tuser1,要求/home/tuser及其内部文件的属组和其他用户均没有任何访问权限。 [root@localhost ~]# cp -a /etc/skel/ /home/tuser1[root@localhost ~]# ll -d /home/tuser1/drwxr-xr-x. 2 root root 59 …

    Linux干货 2016-12-25