Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have some question in closing port, I think I got some strange things.

When I use execute

nmap --top-ports 10 192.168.1.1

it shows that 23/TCP port is open.

But when I execute

nmap --top-ports 10 localhost

it show that 23/tcp port is closed.

Which of them is true? I want to close this port on my whole system, how can I do it?

share|improve this question
8  
they are both true. TCP ports aren't associated with hosts. they are associated with network interfaces. the difference is subtle but important. interfaces are often the same as hosts, but not always. in this case (as stated in answers) localhost is accessing the lo (loopback) interface. the IP Address is accesing your real interface, probably eth0 or wlan0 or somesuch. –  strugee Jun 30 at 20:41
2  
The apparent naivety of this question led to some terrific answers. Thank you for the question! –  dotancohen Jul 2 at 6:35

5 Answers 5

up vote 28 down vote accepted

Nmap is a great port scanner, but sometimes you want something more authoritative. You can ask the kernel what processes have which ports open by using the netstat utility:

me@myhost:~$ sudo netstat -tlnp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address   Foreign Address   State    PID/Program name
tcp        0      0 127.0.0.1:53    0.0.0.0:*         LISTEN   1004/dnsmasq    
tcp        0      0 0.0.0.0:22      0.0.0.0:*         LISTEN   380/sshd        
tcp        0      0 127.0.0.1:631   0.0.0.0:*         LISTEN   822/cupsd       
tcp6       0      0 :::22           :::*              LISTEN   380/sshd        
tcp6       0      0 ::1:631         :::*              LISTEN   822/cupsd       

The options I have given are:

  • -t TCP only
  • -l Listening ports only
  • -n Don't look up service and host names, just display numbers
  • -p Show process information (requires root privilege)

In this case, we can see that sshd is listening on any interface (0.0.0.0) port 22, and cupsd is listening on loopback (127.0.0.1) port 631. Your output may show that telnetd has a local address of 192.168.1.1:23, meaning it will not answer to connections on the loopback adapter (e.g. you can't telnet 127.0.0.1).

There are other tools that will show similar information (e.g. lsof or /proc), but netstat is the most widely available. It even works on Windows (netstat -anb). BSD netstat is a little different: you'll have to use sockstat(1) to get the process information instead.

Once you have the process ID and program name, you can go about finding the process and killing it if you wish to close the port. For finer-grained control, you can use a firewall (iptables on Linux) to limit access to only certain addresses. You may need to disable a service startup. If the PID is "-" on Linux, it's probably a kernel process (this is common with NFS for instance), so good luck finding out what it is.

Note: I said "authoritative" because you're not being hindered by network conditions and firewalls. If you trust your computer, that's great. However, if you suspect that you've been hacked, you may not be able to trust the tools on your computer. Replacing standard utilities (and sometimes even system calls) with ones that hide certain processes or ports (a.k.a. rootkits) is a standard practice among attackers. Your best bet at this point is to make a forensic copy of your disk and restore from backup; then use the copy to determine the way they got in and close it off.

share|improve this answer

A Linux system has a so called loopback interface, which is for internal communication. Its hostname is localhost and its IP address is 127.0.0.1.

When you run nmap on localhost, you actually run the portscan on the virtual loopback interface. 192.168.1.1 is the IP address of your physical (most likely eth0) interface.

So you've run nmap on two different network interfaces, this is why there's a difference in the open ports. They are both true.

If you have TCP port 23 open, it is likely that you have a telnet server running (which is not a good thing due to its lack of encryption) or you have some kind of trojan horse on your machine.

share|improve this answer
1  
so how can i close it? –  user74080 Jun 30 at 20:18
3  
@user74080 You can add an iptables rule as a nearby answer suggests, but it will keep the service going unused, which consumes resources. So if you have telnetd running, just shut it down. –  psimon Jul 1 at 6:23

To "close" the port you can use iptables

sudo iptables -A INPUT -p tcp --dport 23 -m state --state NEW,ESTABLISHED -j DROP
share|improve this answer
    
In this answer, "closing the port" means "ignoring any traffic to it". The port is still open, but it cannot be reached anymore. Also note that DROP does as it says, it sees the packet and then ignores it. Normally (without iptables enabled), the kernel would send an ICMP port unreachable packet back (which can be simulated with the REJECT target instead of DROP). –  Lekensteyn Jul 1 at 15:36
1  
@Lekensteyn ICMP port unreachable is for UDP. The proper packet to reply with is a TCP RST packet, which can also be generated using the REJECT target by writing -j REJECT --reject-with tcp-reset. –  kasperd Jul 1 at 16:28

If you do nmap localhost, it tells you about a different situation: some programs on linux work as server although they are used only locally. This is because other programs use them like a server they connect to. So both answers are true, since you ask something different.

Port 23 is used for telnet. Normally not used anymore. Try to do nmap -sV 192.168.1.1 to find out which program opens the port.

(192... is a local network IP, so the result of nmap <your outside world IP> will also give a different result, because of possible firewall settings etc)

share|improve this answer

If you have a service running and listening on port 23, it is arguably cleaner to stop the process that listens to port 23 (probably telnet) than to keep it running and close or block port 23 using iptables.

When there's no process listening on a port, even in the absence of a firewall block, any attempt to connect to it should result in an immediate "connection refused" (ECONNREFUSED to connect(2))

One way to find the process (and its pid) that listens on port 23, if there's such process, is:

sudo lsof -i -P | grep ':23 '

In the above -i lists open internet ports (both UDP and TCP), and -P inhibits translation of ports to service names (via /etc/services)

After you found the running process listening on port 23, you can figure out how it got started by looking at the process tree (with say, pstree). If its parent is init (very likely), you may recursively search for the name of the process under /etc. e.g.:

sudo grep -r telnet /etc

This should lead you to the best way to disable it from running in the 1st place.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.