translated

This commit is contained in:
XYenChi 2018-02-01 16:49:21 +08:00
parent 51f8159ea6
commit df58793886
2 changed files with 160 additions and 161 deletions

View File

@ -1,161 +0,0 @@
XYenChi is translating
Why You Should Still Love Telnet
======
Telnet, the protocol and the command line tool, were how system administrators used to log into remote servers. However, due to the fact that there is no encryption all communication, including passwords, are sent in plaintext meant that Telnet was abandoned in favour of SSH almost as soon as SSH was created.
For the purposes of logging into a remote server, you should never, and probably have never considered it. This does not mean that the `telnet` command is not a very useful tool when used for debugging remote connection problems.
In this guide, we will explore using `telnet` to answer the all too common question, "Why can't I ###### connect‽".
This frustrated question is usually encountered after installing a application server like a web server, an email server, an ssh server, a Samba server etc, and for some reason, the client won't connect to the server.
`telnet` isn't going to solve your problem but it will, very quickly, narrow down where you need to start looking to fix your problem.
`telnet` is a very simple command to use for debugging network related issues and has the syntax:
```
telnet <hostname or IP> <port>
```
Because `telnet` will initially simply establish a connection to the port without sending any data it can be used with almost any protocol including encrypted protocols.
There are four main errors that you will encounter when trying to connect to a problem server. We will look at all four, explore what they mean and look at how you should fix them.
For this guide we will assume that we have just installed a [Samba][1] server at `samba.example.com` and we can't get a local client to connect to the server.
### Error 1 - The connection that hangs forever
First, we need to attempt to connect to the Samba server with `telnet`. This is done with the following command (Samba listens on port 445):
```
telnet samba.example.com 445
```
Sometimes, the connection will get to this point stop indefinitely:
```
telnet samba.example.com 445
Trying 172.31.25.31...
```
This means that `telnet` has not received any response to its request to establish a connection. This can happen for two reasons:
1. There is a router down between you and the server.
2. There is a firewall dropping your request.
In order to rule out **1.** run a quick [`mtr samba.example.com`][2] to the server. If the server is accessible then it's a firewall (note: it's almost always a firewall).
Firstly, check if there are any firewall rules on the server itself with the following command `iptables -L -v -n`, if there are none then you will get the following output:
```
iptables -L -v -n
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
```
If you see anything else then this is likely the problem. In order to check, stop `iptables` for a moment and run `telnet samba.example.com 445` again and see if you can connect. If you still can't connect see if your provider and/or office has a firewall in place that is blocking you.
### Error 2 - DNS problems
A DNS issue will occur if the hostname you are using does not resolve to an IP address. The error that you will see is as follows:
```
telnet samba.example.com 445
Server lookup failure: samba.example.com:445, Name or service not known
```
The first step here is to substitute the IP address of the server for the hostname. If you can connect to the IP but not the hostname then the problem is the hostname.
This can happen for many reasons (I have seen all of the following):
1. Is the domain registered? Use `whois` to find out if it is.
2. Is the domain expired? Use `whois` to find out if it is.
3. Are you using the correct hostname? Use `dig` or `host` to ensure that the hostname you are using resolves to the correct IP.
4. Is your **A** record correct? Check that you didn 't accidentally create an **A** record for something like `smaba.example.com`.
Always double check the spelling and the correct hostname (is it `samba.example.com` or `samba1.example.com`) as this will often trip you up especially with long, complicated or foreign hostnames.
### Error 3 - The server isn't listening on that port
This error occurs when `telnet` is able to reach to the server but there is nothing listening on the port you specified. The error looks like this:
```
telnet samba.example.com 445
Trying 172.31.25.31...
telnet: Unable to connect to remote host: Connection refused
```
This can happen for a couple of reasons:
1. Are you **sure** you 're connecting to the right server?
2. Your application server is not listening on the port you think it is. Check exactly what it's doing by running `netstat -plunt` on the server and see what port it is, in fact, listening on.
3. The application server isn't running. This can happen when the application server exits immediately and silently after you start it. Start the server and run `ps auxf` or `systemctl status application.service` to check it's running.
### Error 4 - The connection was closed by the server
This error happens when the connection was successful but the application server has a build in security measure that killed the connection as soon as it was made. This error looks like:
```
telnet samba.example.com 445
Trying 172.31.25.31...
Connected to samba.example.com.
Escape character is '^]'.
<EFBFBD><EFBFBD>Connection closed by foreign host.
```
The last line `Connection closed by foreign host.` indicates that the connection was actively terminated by the server. In order to fix this, you need to look at the security configuration of the application server to ensure your IP or user is allowed to connect to it.
### A successful connection
This is what a successful `telnet` connection attempt looks like:
```
telnet samba.example.com 445
Trying 172.31.25.31...
Connected to samba.example.com.
Escape character is '^]'.
```
The connection will stay open for a while depending on the timeout of the application server you are connected to.
A telnet connection is closed by typing `CTRL+]` and then when you see the `telnet>` prompt, type "quit" and hit ENTER i.e.:
```
telnet samba.example.com 445
Trying 172.31.25.31...
Connected to samba.example.com.
Escape character is '^]'.
^]
telnet> quit
Connection closed.
```
### Conclusion
There are a lot of reasons that a client application can't connect to a server. The exact reason can be difficult to establish especially when the client is a GUI that offers little or no error information. Using `telnet` and observing the output will allow you to very rapidly narrow down where the problem lies and save you a whole lot of time.
--------------------------------------------------------------------------------
via: https://bash-prompt.net/guides/telnet/
作者:[Elliot Cooper][a]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://bash-prompt.net
[1]:https://www.samba.org/
[2]:https://www.systutorials.com/docs/linux/man/8-mtr/

View File

@ -0,0 +1,160 @@
Telnet爱一直在
======
Telnet, 是系统管理员登录远程服务器的协议和工具。然而由于所有的通信都没有加密包括密码都是明文发送的。Telnet 在 SSH 被开发出来之后就基本弃用了。
登录远程服务器,你可能不会也从未考虑过它。但这并不意味着 `telnet` 命令在调试远程连接问题时不是一个实用的工具。
本教程中,我们将探索使用 `telnet` 解决所有常见问题,“我怎么又连不上啦?”
这种讨厌的问题通常会在安装了像web服务器、邮件服务器、ssh服务器、Samba服务器等诸如此类的事之后遇到用户无法连接服务器。
`telnet` 不会解决问题但可以很快缩小问题的范围。
`telnet` 用来调试网络问题的简单命令和语法:
```
telnet <hostname or IP> <port>
```
因为 `telnet` 最初通过端口建立连接不会发送任何数据,适用于任何协议包括加密协议。
连接问题服务器有四个可能会遇到的主要问题。我们会研究这四个问题,研究他们意味着什么以及如何解决。
本教程默认已经在 `samba.example.com` 安装了 [Samba][1] 服务器而且本地客户无法连上服务器。
### Error 1 - 连接挂起
首先,我们需要试着用 `telnet` 连接 Samba 服务器。使用下列命令 (Samba 监听端口445)
```
telnet samba.example.com 445
```
有时连接会莫名停止:
```
telnet samba.example.com 445
Trying 172.31.25.31...
```
这意味着 `telnet` 没有收到任何回应来建立连接。有两个可能的原因:
1. 你和服务器之间有个路由器宕掉了。
2. 防火墙拦截了你的请求。
为了排除 **1.** 在服务器上运行一个快速 [`mtr samba.example.com`][2] 。如果服务器是可达的那么便是防火墙(注意:防火墙总是存在的)。
首先用 `iptables -L -v -n` 命令检查服务器本身有没有防火墙, 没有的话你能看到以下内容:
```
iptables -L -v -n
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
```
如果你看到其他东西那可能就是问题所在了。为了检验,停止 `iptables` 一下并再次运行 `telnet samba.example.com 445` 看看你是否能连接。如果你还是不能连接看看你的提供商或企业有没有防火墙拦截你。
### Error 2 - DNS 问题
DNS问题通常发生在你正使用的主机名没有解析到 IP 地址。错误如下:
```
telnet samba.example.com 445
Server lookup failure: samba.example.com:445, Name or service not known
```
第一步是把主机名替换成服务器的IP地址。如果你可以连上那么就是主机名的问题。
有很多发生的原因(以下是我见过的):
1. 域注册了吗?用 `whois` 来检验。
2. 域过期了吗?用 `whois` 来检验。
3. 是否使用正确的主机名?用 `dig``host` 来确保你使用的主机名解析到正确的 IP。
4. 你的 **A** 记录正确吗?确保你没有偶然创建类似 `smaba.example.com`**A** 记录。
一定要多检查几次拼写和主机名是否正确(是 `samba.example.com` 还是 `samba1.example.com`)这些经常会困扰你特别是长、难或外来主机名。
### Error 3 - 服务器没有侦听端口
这种错误发生在 `telnet` 可达服务器但是指定端口没有监听。就像这样:
```
telnet samba.example.com 445
Trying 172.31.25.31...
telnet: Unable to connect to remote host: Connection refused
```
有这些原因:
1. 你 **确定** 连接的是正确的服务器?
2. 你的应用服务器没有侦听预期的端口。在服务器上运行 `netstat -plunt` 来查看它究竟在干什么并看哪个端口才是对的,实际正在监听中的。
3. 应用服务器没有运行。这可能突然而又悄悄地发生在你启动应用服务器之后。启动服务器运行 `ps auxf``systemctl status application.service` 查看运行。
### Error 4 - 连接被服务器关闭
这种错误发生在连接成功建立但是应用服务器建立的安全措施一连上就将其结束。错误如下:
```
telnet samba.example.com 445
Trying 172.31.25.31...
Connected to samba.example.com.
Escape character is '^]'.
<EFBFBD><EFBFBD>Connection closed by foreign host.
```
最后一行 `Connection closed by foreign host.` 意味着连接被服务器主动终止。为了修复这个问题,需要看看应用服务器的安全设置确保你的 IP 或用户允许连接。
### 成功连接
成功的 `telnet` 连接如下:
```
telnet samba.example.com 445
Trying 172.31.25.31...
Connected to samba.example.com.
Escape character is '^]'.
```
连接会保持一段时间只要你连接的应用服务器时限没到。
输入 `CTRL+]` 中止连接然后当你看到 `telnet>` 提示,输入 "quit" 并点击 ENTER 例:
```
telnet samba.example.com 445
Trying 172.31.25.31...
Connected to samba.example.com.
Escape character is '^]'.
^]
telnet> quit
Connection closed.
```
### 总结
客户程序连不上服务器的原因有很多。确切原理很难确定特别是当客户是图形用户界面提供很少或没有错误信息。用 `telnet` 并观察输出可以让你很快确定问题所在节约很多时间。
--------------------------------------------------------------------------------
via: https://bash-prompt.net/guides/telnet/
作者:[Elliot Cooper][a]
译者:[XYenChi](https://github.com/XYenChi)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]:https://bash-prompt.net
[1]:https://www.samba.org/
[2]:https://www.systutorials.com/docs/linux/man/8-mtr/