CentOS关闭系统不必要的端口

 

注:以下所有操作均在CentOS 7.2 x86_64位系统下完成。

 

1)首先查看当前系统开放的端口号:

# netstat -tlnup
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:9000          0.0.0.0:*               LISTEN      30389/php-fpm: mast
tcp        0      0 0.0.0.0:111             0.0.0.0:*               LISTEN      1/systemd           
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      30425/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2151/sshd           

可以看到目前系统监听的端口有四个,分别是:

  • 9000端口:php-fpm使用中;
  • 111端口:系统使用中;
  • 80端口:Nginx使用中;
  • 22端口:sshd服务使用中;

我们决定关闭111端口。

2)查看111端口正在被哪个服务在使用中:

# cat /etc/services | grep -w 111
sunrpc          111/tcp         portmapper rpcbind      # RPC 4.0 portmapper TCP
sunrpc          111/udp         portmapper rpcbind      # RPC 4.0 portmapper UDP

3)继续查看使用111端口的服务的详细状态信息:

# systemctl list-unit-files --all |grep portmapper
# systemctl list-unit-files --all |grep rpcbind
rpcbind.service                               enabled
rpcbind.socket                                enabled
rpcbind.target                                static

可以看到这两个服务名称分别是rpcbind.service和rpcbind.socket,并且设置了自启动。

4)接下来我们关闭这两个服务:

# systemctl stop rpcbind.service
Warning: Stopping rpcbind.service, but it can still be activated by:
  rpcbind.socket
# systemctl stop rpcbind.socket

这个时候可以再查看下系统开放端口信息:

# netstat -tlnup
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:9000          0.0.0.0:*               LISTEN      30389/php-fpm: mast
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      30425/nginx: master
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      2151/sshd    

可以看到111端口已经没有被使用。

5)由于前面看到这两个服务都被设置了开机自启动,所以还要将其设置为开机不自启动:

# systemctl disable rpcbind.service
Removed symlink /etc/systemd/system/multi-user.target.wants/rpcbind.service.
# systemctl disable rpcbind.socket
Removed symlink /etc/systemd/system/sockets.target.wants/rpcbind.socket.

这个时候再来查看下服务的详细状态信息:

# systemctl list-unit-files --all |grep rpcbind
rpcbind.service                               disabled
rpcbind.socket                                disabled
rpcbind.target                                static

这两个服务的自启动项已经被设置为禁用。

至此,本次工作完成。

posted @ 2019-10-31 11:23  brishenzhou  阅读(6160)  评论(0编辑  收藏  举报