CentOS7.4 搭建和使用telnet

1.先檢查是否安裝了telnet

rpm -qa | grep telnet   //檢查你的CentOS是否安裝了telnet和telnet-server
rpm -qa xinetd   //檢查你的CentOS是否安裝了xinetd,telnet的自啓動依賴它

如果執行結果沒有任何輸出,那麼說明你的機器上沒有安裝telnet和xinetd

2.安裝telnet並設置開機自啓動

yum list |grep telnet   //列出當前可用的rpm包
yum install telnet-server  //安裝telnet-server 服務端
yum install telnet    //安裝telnet 客戶端

3.如果沒有安裝xinetd,則安裝它

yum install -y xinetd 
systemctl enable xinetd.service  //設置xinetd開機自啓動

4.開啓xinetd服務並設置爲開機自啓。

[root@CentOs64-7 conf]# systemctl enable xinetd.service 
[root@CentOs64-7 conf]# systemctl status xinetd.service 
● xinetd.service - Xinetd A Powerful Replacement For Inetd
   Loaded: loaded (/usr/lib/systemd/system/xinetd.service; enabled; vendor preset: enabled)
   Active: inactive (dead)
[root@CentOs64-7 conf]# systemctl start xinetd.service
[root@CentOs64-7 conf]# systemctl status xinetd.service 
● xinetd.service - Xinetd A Powerful Replacement For Inetd
   Loaded: loaded (/usr/lib/systemd/system/xinetd.service; enabled; vendor preset: enabled)
   Active: active (running) since Thu 2018-10-18 01:33:11 CST; 17s ago
  Process: 27303 ExecStart=/usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid $EXTRAOPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 27304 (xinetd)
   CGroup: /system.slice/xinetd.service
           └─27304 /usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid

Oct 18 01:33:11 CentOs64-7.4 xinetd[27304]: removing discard
Oct 18 01:33:11 CentOs64-7.4 xinetd[27304]: removing discard
Oct 18 01:33:11 CentOs64-7.4 xinetd[27304]: removing echo
Oct 18 01:33:11 CentOs64-7.4 xinetd[27304]: removing echo
Oct 18 01:33:11 CentOs64-7.4 xinetd[27304]: removing tcpmux
Oct 18 01:33:11 CentOs64-7.4 xinetd[27304]: removing time
Oct 18 01:33:11 CentOs64-7.4 xinetd[27304]: removing time
Oct 18 01:33:11 CentOs64-7.4 xinetd[27304]: xinetd Version 2.3.15 started with libwrap loadavg labeled-networking options compiled in.
Oct 18 01:33:11 CentOs64-7.4 xinetd[27304]: Started working: 0 available services
Oct 18 01:33:11 CentOs64-7.4 systemd[1]: Started Xinetd A Powerful Replacement For Inetd.

  經過上面的啓動命令和查看xinted裝態命令,可以看到xinted已經被成功啓動了。

5.測試telnet是否可用,使用的命令和結果如下

[root@CentOs64-7 conf]# telnet localhost
Trying 127.0.0.1...
telnet: connect to address 127.0.0.1: Connection refused
Trying ::1...
telnet: connect to address ::1: No route to host

  這說明,當前telnet是並不可用的。這是因爲telnet的端口是23,而CentOS默認這個端口是關閉的,我們可以使用命令來查看一下23這個端口的裝態,如下:

[root@CentOs64-7 conf]# netstat -tunlp 
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      1127/sshd           
tcp        0      0 0.0.0.0:2181            0.0.0.0:*               LISTEN      27193/java          
tcp        0      0 0.0.0.0:33031           0.0.0.0:*               LISTEN      27193/java          
tcp6       0      0 :::3306                 :::*                    LISTEN      12118/mysqld        
udp        0      0 172.16.85.47:123        0.0.0.0:*                           14051/ntpd          
udp        0      0 127.0.0.1:123           0.0.0.0:*                           14051/ntpd          
udp        0      0 0.0.0.0:123             0.0.0.0:*                           14051/ntpd          
udp6       0      0 :::123                  :::*                                14051/ntpd          

  可以看到TCP並沒有23 這個端口號,當然你也可以在服務器本地使用nmap ip地址 -p 23 查看telnet狀態是關閉的。

6.設置telnet服務開機啓動。

systemctl enable telnet.socket

7.重啓服務(telnet服務依賴xinetd守護,安裝telnet-server後啓動時需要重啓xinetd服務)。

systemctl start telnet.socket
systemctl start xinetd

8.開啓端口

  CentOS採用了 firewalld 防火牆,查詢是否開啓23端口

# firewall-cmd --query-port=23/tcp

no

顯示23端口沒有開啓使用下面命令開啓23端口

# firewall-cmd --zone=public --add-port=23/tcp --permanent

success

重新加載firewall-cmd

# firewall-cmd --complete-reload

success

重新查詢23端口是否開放

# firewall-cmd --query-port=23/tcp

yes

success表示已開啓!現在可以用telnet命令遠程登陸CentOS7快樂玩耍了!

注:telnet使用的是文明密碼,爲安全建議使用SSH加密登陸!

9.telnet默認是不開啓的,所以你得telnet不能使用也可能不是第8步的原因,例如我的機器就沒有開防火牆,所以執行第9步。

  telnet服務之後,默認是不開啓服務,修改文件/etc/xinetd.d/telnet來開啓服務:

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

  修改後的telnet文件爲:

#default:yes  
# description: The telnet server servestelnet sessions; it uses \  
# unencrypted username/password pairs for authentication.  
service telnet
{        
    flags = REUSE            
    socket_type  = stream           
    wait = no            
    user = root                       
    server =/usr/sbin/in.telnetd                       
    log_on_failure  += USERID                
    disable = no   
}  
  

查看xinetd是否啓動

[root@CentOs64-7 xinetd.d]# ps -ef | grep xinetd 
root 27304 1 0 01:33 ? 00:00:00 /usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid
root 27378 27017 0 02:01 pts/0 00:00:00 grep --color=auto xinetd

說明已經啓動

10.使用telnet localhost檢測telnet是否正常,密碼正確,但是總是提示Login incorrect

telnet是除了SSH之外另一種遠程登錄服務器的辦法,但是它並不安全,因爲是明文的。

[root@CentOs64-7 xinetd.d]# telnet localhost
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

Kernel 3.10.0-693.2.2.el7.x86_64 on an x86_64
CentOs64-7 login: root
Password: 
Login incorrect

CentOs64-7 login: root
Password: 
Login incorrect

CentOs64-7 login: Connection closed by foreign host.

解決辦法:

解決1:註釋/etc/pam.d/remote的第一行,
即:auth       required     pam_securetty.so

 先進入 /etc/pam.d/目錄

[root@CentOs64-7 xinetd.d]# cd /etc/pam.d/
[root@CentOs64-7 pam.d]# pws
-bash: pws: command not found
[root@CentOs64-7 pam.d]# pwd
/etc/pam.d

修改結果如下:

  然後再使用telnet localhost就可以成功登錄你指定的用戶了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章