1. 程式人生 > >Centos 7 telnet 詳解

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:使用類似指令的使用者介面;
-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. [[email protected] log]# telnet 192.168.10.56 27017
  2. -bash: telnet: command not found

1:檢視系統版本資訊(centos7):


  1. [[email protected] log]# cat /etc/issue
  2. \S
  3. Kernel \r on an \m

2:檢查是否安裝telnet:


  1. [
    [email protected]
    log]
    # rpm -qa | grep telnet
  2. [[email protected] log]#
3:進行安裝,客戶端和伺服器端:

  1. [[email protected] 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. [[email protected] 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. [[email protected] ~]# rpm -qa | grep xinetd
  2. [[email protected] ~]#

5:安裝xinetd服務:


  1. [[email protected] 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. [[email protected] xinetd.d]# pwd
  2. /etc/xinetd.d
  3. [[email protected] 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. [[email protected] xinetd.d]# vim telnet
  6. [[email protected] 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. [[email protected] xinetd.d]# rpm -qa | grep telnet
  2. telnet-0.17-59.el7.x86_64
  3. telnet-server-0.17-59.el7.x86_64
  4. [[email protected] 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. [[email protected] xinetd.d]# service xinetd restart
  2. Redirecting to /bin/systemctl restart xinetd.service
  3. [[email protected] xinetd.d]# systemctl restart xinetd.service

[[email protected] xinetd.d]# /bin/systemctl restart  xinetd.service

9:檢視啟動:


  1. [[email protected] 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. [[email protected] 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. [[email protected] ~]# exit
  12. logout
  13. Connection closed by foreign host.
11:設定服務開機啟動:

  1. [[email protected] rc3.d]# chkconfig --level 35 xinetd on
  2. Note: Forwarding request to 'systemctl enable xinetd.service'.
  3. [[email protected] rc3.d]# systemctl enable xinetd.service
12:檢視:

  1. [[email protected] 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. [[email protected] pam.d]# pwd
  2. /etc/pam.d
  3. [[email protected] 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. [[email protected] 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: [[email protected] rc3.d]# chkconfig --level 35 xinetd on
Note: Forwarding request to 'systemctl enable xinetd.service'.
[[email protected] 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復位當前操作引數