注:以下所有操作均在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
這兩個服務的自啟動項已經被設置為禁用。
至此,本次工作完成。
