百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术教程 > 正文

# CentOS7开启telnet服务端,配合进行ssh升级

csdh11 2025-01-15 12:08 18 浏览

CentOS7开启telnet服务端,配合进行ssh升级

默认情况下,linux'操作系统我们都是通过ssh进行远程操作,开启telnet一般是在升级ssh的时候需要,开启telnet是为了防止升级ssh之后,无法通过ssh登录系统,可用telnet登录。

1.查找telnet安装包

前置条件:服务器可访问互联网

安装之前首先查找telnet的安装包

[root@localhost ~]# yum list | grep telnet-server
telnet-server.x86_64                        1:0.17-66.el7              updates
[root@localhost ~]# yum list | grep xinetd
xinetd.x86_64                               2:2.3.15-14.el7            base

2.执行安装(yum)

使用yum -y install telnet-server.x86_64命令安装telnet服务器

[root@localhost ~]# yum -y install telnet-server.x86_64
Loaded plugins: fastestmirror
········(过程省略)
Installed:
  telnet-server.x86_64 1:0.17-66.el7

Complete!

使用yum -y install xinetd.x86_64命令安装xinetd守护进程

[root@localhost ~]# yum -y install xinetd.x86_64
Loaded plugins: fastestmirror
········(过程省略)
Installed:
  xinetd.x86_64 2:2.3.15-14.el7

Complete!

3.配置并启动

3.1 配置开机启动

使用systemctl enable xinetd.servicesystemctl enable telnet.socket命令将telnet服务设置为开机自动启动。

[root@localhost ~]# systemctl enable xinetd.service
[root@localhost ~]# systemctl enable telnet.socket
Created symlink from /etc/systemd/system/sockets.target.wants/telnet.socket to /usr/lib/systemd/system/telnet.socket.

3.2 启动服务

使用systemctl start telnet.socketsystemctl start xinetd命令启动telnet服务

[root@localhost ~]# systemctl start telnet.socket
[root@localhost ~]# systemctl start xinetd

使用netstat -ntlp命令查看运行中的端口(如果提示command not found,可运行yum -y install net-tools命令安装网络工具包。)

[root@localhost ~]# yum -y install net-tools  #安装网络工具包
Loaded plugins: fastestmirror
········(过程省略)
Installed:
  net-tools.x86_64 0:2.0-0.25.20131004git.el7

Complete!
[root@localhost ~]# netstat -ntlp   #查看端口
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      987/sshd
tcp        0      0 127.0.0.1:25            0.0.0.0:*               LISTEN      1146/master
tcp6       0      0 :::22                   :::*                    LISTEN      987/sshd
tcp6       0      0 :::23                   :::*                    LISTEN      1/systemd
tcp6       0      0 ::1:25                  :::*                    LISTEN      1146/master

可以看到,telnet服务已经开启,端口号为23。

注意:完成上述步骤后,还不能通过telnet进行登录,因为centos初始安装后默认开启了firewalld防火墙服务,且默认只打开了22端口。还需要进行防火墙配置。

3.3 配置防火墙规则

通过firewall-cmd --list-all命令查看防火墙配置

[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: dhcpv6-client ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

添加23端口,并重启防火墙。配置防火墙有两种方式可选:

  • 直接对外开放23端口
  • 只对特定地址的终端开启23端口(推荐)

第一种方式比较简单,安全性低,操作如下:

[root@localhost ~]# firewall-cmd --add-port=23/tcp --permanent   #永久开启23端口,--permanent 为永久开启,不加此参数重启防火墙后规则不保存
success
[root@localhost ~]# firewall-cmd --reload   #重启防火墙
success
[root@localhost ~]# firewall-cmd --list-all    #查看规则
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: dhcpv6-client ssh
  ports: 23/tcp

第二种方式安全性高,甚至如果只对某一台终端开启23端口,后续可以不卸载(当然建议还是卸载了吧),操作如下:

[root@localhost ~]# firewall-cmd  --permanent --add-rich-rule='rule family=ipv4 source address=192.168.12.1 port protocol=tcp port=23 accept'    #--permanent 为永久开启,不加此参数重启防火墙后规则不保存
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-all   #查看规则
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
 ······(省略)
  rich rules:
        rule family="ipv4" source address="192.168.12.1" port port="23" protocol="tcp" accept

其中source address就是需要访问服务器的终端的ip地址。第二种防火墙配置非常实用,在服务器安全中,强烈推荐多实用第二种防火墙配置方式,比如数据库服务器配置只允许特定的应用服务器访问,能大幅提高服务器环境安全。

3.4 安全配置

此时还无法使用root登录,会提示登录错误。因为默认root无法远程访问。

Kernel 3.10.0-1127.el7.x86_64 on an x86_64                                                                              localhost login: root                                                                                                   Password:                                                                                                               Login incorrect               

还需要修改/etc/securetty文件

[root@localhost ~]# vim /etc/securetty

在末尾添加以下内容:

pts/0
pts/1

最终内容如下:

console
·······(省略)
xvc0
pts/0
pts/1
~

4 测试验证

在windows上测试通过telnet登录linux

C:\Users\11>telnet 192.168.12.101                                                                                                               
                                                                                                                        Kernel 3.10.0-1127.el7.x86_64 on an x86_64                                                                              localhost login:                                                                                                                           (输入账户密码)
                                                                                                                        Last failed login: Wed Mar 30 22:45:11 CST 2022 from ::ffff:192.168.12.1 on pts/0                                       There was 1 failed login attempt since the last successful login.                                                       Last login: Wed Mar 30 19:04:51 from 192.168.12.1                     

5 关闭telnet

关闭telnet很简单,有很多种方式可以关闭。

  • 去除防火墙配置
  • 停止telnet服务并取消开机启动
  • 彻底移除telnet安装包

5.1 去除防火墙配置

去除防火墙配置根据不容的配置方式,使用不同的命令进行移除。核心实际上就是将添加防火墙配置的命令中的add替换为remove即可。

[root@localhost ~]# firewall-cmd --remove-port=23/tcp --permanent   #针对直接对外开放23端口的配置的移除
success
[root@localhost ~]#  firewall-cmd  --permanent --remove-rich-rule='rule family=ipv4 source address=192.168.12.1 port protocol=tcp port=23 accept'   #针对第二种方式的防火墙配置的移除
success
[root@localhost ~]# firewall-cmd --reload   #重启防火墙
success
[root@localhost ~]# firewall-cmd --list-all   #查看配置
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources:
  services: dhcpv6-client ssh
  ports:
  protocols:
  masquerade: no
  forward-ports:
  source-ports:
  icmp-blocks:
  rich rules:

5.2 停止telnet服务并取消开机启动

停止服务与开启服务命令类似,将开启服务的start换为stop即可。

[root@localhost ~]# systemctl stop telnet.socket
[root@localhost ~]# systemctl stop xinetd

取消开机启动原理一样,将enable替换为disable即可。

[root@localhost ~]# systemctl disable xinetd.service
Removed symlink /etc/systemd/system/multi-user.target.wants/xinetd.service.
[root@localhost ~]#  systemctl disable telnet.socket
Removed symlink /etc/systemd/system/sockets.target.wants/telnet.socket.

5.3 彻底移除telnet安装包

移除安装包与安装安装包类似,将install替换为remove即可。(是不是发现了什么新大陆)

[root@localhost ~]# yum -y remove telnet-server.x86_64
Loaded plugins: fastestmirror
········(过程省略)
Installed:
  telnet-server.x86_64 1:0.17-66.el7

Complete!

[root@localhost ~]# yum -y remove xinetd.x86_64
Loaded plugins: fastestmirror
········(过程省略)
Installed:
  xinetd.x86_64 2:2.3.15-14.el7

Complete!

至此,关于在CentOS7上安装telnet、卸载telnet的方法就完全介绍完了,希望对大家有用。

相关推荐

当iPhone X遇上 Mate 10 Pro 怎么挑?

产品:Mate10Pro(全网通)华为手机1iPhoneX一出就遇上了对手Hello!大家好我是石头这里是zol。首先来看看我手里的这个,不是装x的意思,今天好多人欢天喜地的拿到了属于自己或者属...

谁是办公高手? 六款商务平板全推荐

1超低功耗联想Thinkpad10领衔ThinkPad的名字在笔记本上依然是高曝光率,从IBM到联想,它一直以来是商务、办公领域的不二人选,然而在平板电脑大行其道的今天,ThinkPad当然也不单...

如何仅用几行代码将微信4.0公测版转换成玲珑格式?

微信4.0(玲珑版)已上架如意玲珑应用商店!!!...

音乐研发必备:理解 MIDI 协议与标准 MIDI 文件格式

1.MIDI简介...

HEIF格式可以节约50%的空间!质量会有影响吗?

关于手机1亿像素到底有没有必要,争议肯定是存在的,但无法回避的一点是随着像素的成倍增加,一张照片十几MB成了常事,这必将给本就不太宽裕的存储容量造成压力,这种压力又会转嫁到用户身上,让他们不得不花更多...

「干货分享」30个前端知识技能提升的资源网站

今天给小伙伴们分享30个前端相关学习资源网站及一些在线小工具,希望能帮助到大家。CSS相关1、css精灵牛|在线图片CSS生成工具。...

用Facebook引流Shopify独立站,如何优化Facebook商户页面?

什么是FacebookBusinessPage?FacebookBusinessPage是官方的称号,其实你可以简单理解为facebookbusinesspage就是微信公众账户,而我们...

玩转手机摄影 nubia Z9 mini新品首测

1Z9系列新品nubiaZ9mini发布会开始之前的邀请函总能透露出很多内容,或者说我们总会特意解读出很多内容。nubia此前发出的3月26日智能手机新品发布会的邀请函很显然再一次着重强调了其产品...

影院看片怕座位不好?那买个盒子在家看

着消费者对高清播放器的需求的不断增加,高清播放器的功能也在不断得到提升。以前看电影的时候相信很多人更喜欢去电影院里观看,但是有时候去电影院的人太多买不到好位子也会让不少人感到扫兴。现在随着技术的革新,...

今天你买了么?不入后悔的8款平板推荐

随着高考大战的落幕,对于广大学生朋友们来说正真的暑假终于来了!这个假期想要外出旅行或是居家畅玩,没有一款便携式全能娱乐利器的平板电脑怎么能叫放假呢?正直京东618购物季即将到来的日子,相信你也不会错过...

Cursor使用指南:释放AI编程的无限潜能

Cursor简介...

谁说轻薄本缺乏生产力?没体验过别乱说

笔记本电脑轻薄化一直是各大厂商们不断努力的方向,不过早前的轻薄笔记本电脑会受制于技术方面的限制,无法将高能耗的CPU以及显卡塞入轻薄的机身内。为了能给这些芯片散热、供电,制造商们不得不对这些芯片的功耗...

通告ImageMagick再爆核心漏洞 站长小心

目前所有版本的GraphicsMagick和ImageMagick都支持打开文件,当文件名的第一个字符为“|”,则文件名会被传递给shell程序执行,导致(可能远程)代码执行。文件打开操作位于源代码文...

《玻璃之花与坏掉的世界》再曝新宣传片

雨文冰璃发表于昨天22:56【新闻】《玻璃之花与坏掉的世界》再曝新宣传片http://comic.qq.com/a/20151122/016784.htmPONYCANYON和A-1Pictu...

7 招教你轻松搭建以图搜图系统

作者|小龙责编|胡巍巍当您听到“以图搜图”时,是否首先想到了百度、Google等搜索引擎的以图搜图功能呢?事实上,您完全可以搭建一个属于自己的以图搜图系统:自己建立图片库;自己选择一张图片到...