Centos 7 telnet 详解


telnet命令


telnet命令用于登录远程主机,对远程主机进行管理。telnet因为采用明文传送报文,安全性不好,很多Linux服务器都不开放telnet服务,而改用更安全的ssh方式了。但仍然有很多别的系统可能采用了telnet方式来提供远程登录,因此弄清楚telnet客户端的使用方式仍是很有必要的。

语法

telnet(选项)(参数)

选项

-8:允许使用8位字符资料,包括输入与输出;
-a:尝试自动登入远端系统;
-b<主机别名>:使用别名指定远端主机名称;
-c:不读取用户专属目录里的.telnetrc文件;
-d:启动排错模式;
-e<脱离字符>:设置脱离字符;
-E:滤除脱离字符;
-f:此参数的效果和指定"-F"参数相同;
-F:使用Kerberos V5认证时,加上此参数可把本地主机的认证数据上传到远端主机;
-k<域名>:使用Kerberos认证时,加上此参数让远端主机采用指定的领域名,而非该主机的域名;
-K:不自动登入远端主机;
-l<用户名称>:指定要登入远端主机的用户名称;
-L:允许输出8位字符资料;
-n<记录文件>:指定文件记录相关信息;
-r:使用类似rlogin指令的用户界面;
-S<服务类型>:设置telnet连线所需的ip TOS信息;
-x:假设主机有支持数据加密的功能,就使用它;
-X<认证形态>:关闭指定的认证形态。

参数

  • 远程主机:指定要登录进行管理的远程主机;
  • 端口:指定TELNET协议使用的端口号。

实例

telnet 192.168.2.10
Trying 192.168.2.10...
Connected to 192.168.2.10 (192.168.2.10).
Escape character is '^]'.

    localhost (Linux release 2.6.18-274.18.1.el5 #1 SMP Thu Feb 9 12:45:44 EST 2012) (1)

login: root
Password: 
Login incorrect



默认在centOS最小安装下没有安装telnet服务的,需要自己安装(root权限):

错误:


  1. [root@localhost log]# telnet 192.168.10.56 27017
  2. -bash: telnet: command not found

1:查看系统版本信息(centos7):


  1. [root@localhost log]# cat /etc/issue
  2. S
  3. Kernel on an m

2:检查是否安装telnet:


  1. [root@localhost log]# rpm -qa | grep telnet
  2. [root@localhost log]#
3:进行安装,客户端和服务器端:

  1. [root@localhost xinetd.d]# yum -y install telnet
  2. Loaded plugins: fastestmirror
  3. Loading mirror speeds from cached hostfile
  4. ……………………
  5. Verifying : 1:telnet-0.17-59.el7.x86_64 1/1
  6. Installed:
  7. telnet.x86_64 1:0.17-59.el7
  8. Complete!
  9. [root@localhost xinetd.d]# yum -y install telnet-server
  10. Loaded plugins: fastestmirror
  11. Loading mirror speeds from cached hostfile
  12. ……………………
  13. Verifying : 1:telnet-server-0.17-59.el7.x86_64 1/1
  14. Installed:
  15. telnet-server.x86_64 1:0.17-59.el7
  16. Complete!

注:如果安装telnet-server服务启动依赖xinetd服务.xinetd超级服务为管理保护各个服务,未安装,需要首先按照。

xinetd:eXtended InterNET services daemon,超级Internet服务器,常用来管理多种轻量级Internet服务。

4:查看是否安装xinetd (若安装则不安装):


  1. [root@localhost ~]# rpm -qa | grep xinetd
  2. [root@localhost ~]#

5:安装xinetd服务:


  1. [root@localhost init.d]# yum -y install xinetd
  2. Loaded plugins: fastestmirror
  3. base | 3.6 kB 00:00:00
  4. ……………………
  5. Verifying : 2:xinetd-2.3.15-12.el7.x86_64 1/1
  6. Installed:
  7. xinetd.x86_64 2:2.3.15-12.el7
  8. Complete!

xinetd安装完成!
6:telnet服务之后,默认是不开启服务,修改文件/etc/xinetd.d/telnet来开启服务:

注:如有则修改,第一次修改,此文件若不存在,可自己vim创建修改:
       修改 disable = yes 为 disable = no


  1. [root@localhost xinetd.d]# pwd
  2. /etc/xinetd.d
  3. [root@localhost xinetd.d]# ls
  4. chargen-dgram chargen-stream daytime-dgram daytime-stream discard-dgram discard-stream echo-dgram echo-stream tcpmux-server time-dgram time-stream
  5. [root@localhost xinetd.d]# vim telnet
  6. [root@localhost xinetd.d]# cat telnet

修改后的telnet文件为:


  1. # default: yes
  2. # description: The telnet server servestelnet sessions; it uses
  3. # unencrypted username/password pairs for authentication.
  4. service telnet
  5. {
  6. flags = REUSE
  7. socket_type = stream
  8. wait = no
  9. user = root
  10. server =/usr/sbin/in.telnetd
  11. log_on_failure += USERID
  12. disable = no
  13. }

7:安装后检查:


  1. [root@CentOS-Slave1 xinetd.d]# rpm -qa | grep telnet
  2. telnet-0.17-59.el7.x86_64
  3. telnet-server-0.17-59.el7.x86_64
  4. [root@CentOS-Slave1 xinetd.d]# rpm -qa | grep xinetd
  5. xinetd-2.3.15-12.el7.x86_64

8:启动telnet和依赖的xinetd服务:

在centos7之前:


  1. $ service xinetd restart
  2. 或$ /etc/rc.d/init.d/xinetd restart

在centos7中(无xinetd的service启动项):


  1. [root@CentOS-Slave1 xinetd.d]# service xinetd restart
  2. Redirecting to /bin/systemctl restart xinetd.service
  3. [root@CentOS-Slave1 xinetd.d]# systemctl restart xinetd.service

[root@CentOS-Slave1 xinetd.d]# /bin/systemctl restart  xinetd.service

9:查看启动:


  1. [root@CentOS-Slave1 xinetd.d]# ps -ef | grep xinetd
  2. root 6641 1 0 23:22 ? 00:00:00 /usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid
  3. root 6644 5817 0 23:24 pts/3 00:00:00 grep --color=auto xinetd

10:测试telent,输入ip+用户名+密码登陆,登陆问题见备注附件:


  1. [root@CentOS-Slave1 pam.d]# telnet 192.168.10.56
  2. Trying 192.168.10.56...
  3. Connected to 192.168.10.56.
  4. Escape character is '^]'.
  5. Kernel 3.10.0-229.el7.x86_64 on an x86_64
  6. CentOS-Slave1 login: root
  7. Password:
  8. Last failed login: Sat Oct 17 23:25:50 CST 2015 from CentOS-Slave1 on pts/0
  9. There were 3 failed login attempts since the last successful login.
  10. Last login: Sat Oct 17 22:22:27 from CentOS-Slave1
  11. [root@CentOS-Slave1 ~]# exit
  12. logout
  13. Connection closed by foreign host.
11:设置服务开机启动:

  1. [root@CentOS-Slave1 rc3.d]# chkconfig --level 35 xinetd on
  2. Note: Forwarding request to 'systemctl enable xinetd.service'.
  3. [root@CentOS-Slave1 rc3.d]# systemctl enable xinetd.service
12:查看:

  1. [root@CentOS-Slave1 rc3.d]# chkconfig --list
  2. Note: This output shows SysV services only and does not include native
  3. systemd services. SysV configuration data might be overridden by native
  4. systemd configuration.
  5. If you want to list systemd services use 'systemctl list-unit-files'.
  6. To see services enabled on particular target use
  7. 'systemctl list-dependencies [target]'.
  8. mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  9. netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
  10. network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
  11. xinetd based services:
  12. chargen-dgram: off
  13. chargen-stream: off
  14. daytime-dgram: off
  15. daytime-stream: off
  16. discard-dgram: off
  17. discard-stream: off
  18. echo-dgram: off
  19. echo-stream: off
  20. tcpmux-server: off
  21. telnet: on
  22. time-dgram: off
  23. time-stream: off

备注附件:

问题1:

telnet下root登录,密码正确,总提示:Login incorrect
解决1:注释/etc/pam.d/remote的第一行,
即:auth       required     pam_securetty.so


  1. [root@CentOS-Slave1 pam.d]# pwd
  2. /etc/pam.d
  3. [root@CentOS-Slave1 pam.d]# cat remote
  4. #%PAM-1.0
  5. #telent 远程root登陆允许
  6. #auth required pam_securetty.so
  7. auth substack password-auth
  8. auth include postlogin
  9. ………………

问题2:其他机器远程telnet的时候,登陆不成功,可能是防火墙的问题,修改防火墙的设置:
注:netstat –tunlp查看是否23端口被防火墙封掉:

  1. [root@CentOS-Slave1 pam.d]# netstat -tunlp
  2. Active Internet connections (only servers)
  3. Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
  4. tcp 0 0 0.0.0.0:27017 0.0.0.0:* LISTEN 5891/./mongod
  5. tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 848/sshd
  6. tcp6 0 0 :::3306 :::* LISTEN 1997/mysqld
  7. tcp6 0 0 :::22 :::* LISTEN 848/sshd
  8. tcp6 0 0 :::23 :::* LISTEN 1/systemd
再使用iptables修改设置,使用service iptables save保存设置,然后service iptables restart重启防火墙:

  1. iptables -I INPUT -p tcp --dport 23 -jACCEPT
  2. iptables -I INPUT -p udp --dport 23 -jACCEPT
  3. service iptables save //保存
  4. service iptables restart //重启防火墙
问题new:
[root@CentOS-Slave1 rc3.d]# chkconfig --level 35 xinetd on
Note: Forwarding request to 'systemctl enable xinetd.service'.
[root@CentOS-Slave1 xinetd.d]# service xinetd restart
Redirecting to /bin/systemctl restart  xinetd.service
解决new:
指令可以用,但是新版本系统,指令被(重新定向Redirecting/转发Forwarding)到:
service xinetd restart  ---> systemctl restart  xinetd.service

chkconfig --level 35 xinetd on ---> systemctl enable sshd.service      #对应为disable
$.记住:以后控制服务就用这个指令。
疑问3:telnet登陆主机后会提示Escape character is '^]':
点击提示的意思是按Ctrl + ] 会呼出telnet的命令行,就可以执行telnet命令:
telnet命令:

  1. #close关闭当前连接
  2. #logout强制退出远程用户并关闭连接
  3. #display显示当前操作的参数
  4. #mode试图进入命令行方式或字符方式
  5. #open连接到某一站点
  6. #quit退出
  7. #telnetsend发送特殊字符
  8. #set设置当前操作的参数
  9. #unset复位当前操作参数
  10. #status打印状态信息
  11. #toggle对操作参数进行开关转换
  12. #slc改变特殊字符的状态
  13. #auth打开/关闭确认功能z挂起
  14. #telnetenviron更改环境变量,显示帮助信息
iptables参数:

  1. iptables [-AI 链名] [-io 网络接口] [-p 协议]
  2. > [-s 来源IP/网域] [-d 目标IP/网域] -j [ACCEPT|DROP|REJECT|LOG]
  3. 选项与参数:
  4. -AI 链名:针对某的链进行规则的 "插入" 或 "累加"
  5. -A :新增加一条规则,该规则增加在原本规则的最后面。例如原本已经有四条规则,
  6. 使用 -A 就可以加上第五条规则!
  7. -I :插入一条规则。如果没有指定此规则的顺序,默认是插入变成第一条规则。
  8. 例如原本有四条规则,使用 -I 则该规则变成第一条,而原本四条变成 2~5 号
  9. 链 :有 INPUT, OUTPUT, FORWARD 等,此链名称又与 -io 有关,请看底下。
  10. -io 网络接口:设定封包进出的接口规范
  11. -i :封包所进入的那个网络接口,例如 eth0, lo 等接口。需与 INPUT 链配合;
  12. -o :封包所传出的那个网络接口,需与 OUTPUT 链配合;
  13. -p 协定:设定此规则适用于哪种封包格式,主要的封包格式有: tcp, udp, icmp 及 all 。
  14. -s 来源 IP/网域:设定此规则之封包的来源项目,可指定单纯的 IP 或包括网域,例如:IP :192.168.0.100
  15. 网域:192.168.0.0/24, 192.168.0.0/255.255.255.0 均可。
  16. 若规范为『不许』时,则加上 ! 即可,例如:-s ! 192.168.100.0/24 表示不许 192.168.100.0/24 之封包来源;
  17. -d 目标 IP/网域:同 -s ,只不过这里指的是目标的 IP 或网域。
  18. -j :后面接动作,主要的动作有接受(ACCEPT)、丢弃(DROP)、拒绝(REJECT)及记录(LOG)
linux运行级别

  1. chkconfig命令提供了一种简单的方式来设置一个服务的运行级别,on和off分别指服务被启动和停止。
  2. 运行级别就是操作系统当前正在运行的功能级别。级别是从06
  3. 级别35上设定服务为“on”
  4. chkconfig --level 35 mysql on
  5. 其他级别上设为off
  6. chkconfig --level 01246 mysql off
  7. 列出服务将会运行的运行级别
  8. #chkconfig --list mysql
  9. mysql 0:off 1:off 2:off 3:on 4:off 5:on 6:off
  10. Linux下的7个运行级别:
  11. 0:系统停机状态,系统默认运行级别不能设置为0,否则不能正常启动,机器关闭。
  12. 1:单用户工作状态,root权限,用于系统维护,禁止远程登陆,就像Windows下的安全模式 登录。
  13. 2:多用户状态,没有NFS支持。
  14. 3:完整的多用户模式,有NFS,登陆后进入控制台命令行模式。
  15. 4:系统未使用,保留一般不用,在一些特殊情况下可以用它来做一些事情。eg:在笔记本电脑的电池用尽时,可以切换到这个模式来做一些设置。
  16. 5:X11控制台,登陆后进入图形GUI模式,XWindow系统。
  17. 6:系统正常关闭并重启,默认运行级别不能设为6,否则不能正常启动。运行init6机器就会重启。
  18. 标准的Linux运行级别为35运行级别原理:
  19. 1.在目录/etc/rc.d/init.d下有许多服务器脚本程序,一般称为服务(service)
  20. 2.在/etc/rc.d下有7个名为rcN.d的目录(N:0,1,2……),对应系统的7个运行级别
  21. 3.rcN.d目录下都是一些符号链接文件,这些链接文件都指向init.d目录下的service脚 本文件,命名规则为K+nn+服务名或S+nn+服务名,其中nn为两位数字。
  22. 4.系统会根据指定的运行级别进入对应的rcN.d目录,并按照文件名顺序检索目录下的链接文件:对于以K(Kill)开头的文件,系统将终止对应的服;对于以S(Start开头的文件,系统将启动对应的服务
  23. 5.查看运行级别用:runlevel
  24. [root@CentOS-Slave1 pam.d]# runlevel
  25. N 3
  26. [root@CentOS-Slave1 etc]# cd rc.d/
  27. [root@CentOS-Slave1 rc.d]# ls
  28. init.d rc0.d rc1.d rc2.d rc3.d rc4.d rc5.d rc6.d rc.local
  29. [root@CentOS-Slave1 rc.d]# cd rc3.d/
  30. [root@CentOS-Slave1 rc3.d]# ls
  31. K50netconsole S10network S64mysql
  32. [root@CentOS-Slave1 rc3.d]# pwd
  33. /etc/rc.d/rc3.d
--------------------- 本文来自 TS_A1 的CSDN 博客 ,全文地址请点击:https://blog.csdn.net/typa01_kk/article/details/46604967?utm_source=copy
原文地址:https://www.cnblogs.com/ios9/p/9747736.html