CentOS 防火牆、端口、端口轉發、selinux、Windows 端口

1.防火牆基礎操作
1) 重啓後生效 
開啓: chkconfig iptables on 
關閉: chkconfig iptables off 
2) 即時生效,重啓後失效 
開啓: service iptables start 
關閉: service iptables stop 
參考:https://zhidao.baidu.com/question/303901938543294164.html
3)開放特定端口
方法1:查看狀態:iptables -L -n
下面添加對特定端口開放的方法:
使用iptables開放如下端口:/sbin/iptables -I INPUT -p tcp --dport 6379 -j ACCEPT
保存:/etc/rc.d/init.d/iptables save
重啓服務:service iptables restart
查看需要打開的端口是否生效:/etc/init.d/iptables status
方法2:直接編輯/etc/sysconfig/iptables
添加:-A INPUT -p tcp -m tcp --dport 6379 -j ACCEPT

 

重啓:service iptables restart

 

4)如果CentOS7,service iptables stop 時顯示not loaded

可能是用的firewalld

停止firewalld,並禁用這個服務

sudo systemctl stop firewalld.service

sudo systemctl disable firewalld.service

啓動firewalld:sudo systemctl start firewalld.service

參考:https://blog.csdn.net/yelllowcong/article/details/75945339

其他firewalld開放指定端口及相關操作:

firewall-cmd --zone=public --add-port=2377/tcp --permanent

 

firewall-cmd --reload

參考:https://blog.csdn.net/u012498149/article/details/78772058

 

2.端口管理(部署集羣時可以將其寫在sh腳本里執行)
禁用指定端口:iptables -I INPUT -p tcp --dport 6379 -j DROP
對指定ip開啓指定端口:iptables -I INPUT -s 192.168.1.10 -p tcp --dport 6379 -j ACCEPT
命令開啓80端口:iptables -I INPUT -p tcp --dport 80 -j ACCEPT
配置文件開啓80端口:修改/etc/sysconfig/iptables文件,添加-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
參考:http://blog.csdn.net/zhouyufengqingyang/article/details/51737254


3.關閉 selinux
修改/etc/selinux/config 文件
將SELINUX=enforcing改爲SELINUX=disabled
重啓
參考:http://bguncle.blog.51cto.com/3184079/957315/
 

 

4.通過防火牆配置實現 端口映射、端口轉發

# 打開 ipv4 端口轉發

sysctl net.ipv4.ip_forward=1

#  配置端口映射(例:該機器公網ip:8080轉到192.168.0.122:80,其中192.168.0.62爲該機器的內網ip)
iptables -A INPUT -p tcp --dport 8080 -j ACCEPT
iptables -t nat -A PREROUTING -p tcp --dport 8080 -j DNAT --to-destination 192.168.0.122:80

iptables -t nat -A POSTROUTING -d 192.168.0.122 -p tcp --dport 80 -j SNAT --to 192.168.0.62

參考:http://man.linuxde.net/iptables

 

5.linux查看端口占用

1) lsof -i:端口號

2) netstat -tunlp | grep 端口號

參考:https://jingyan.baidu.com/article/546ae1853947b71149f28cb7.html

 

6.Windows關閉端口方法:
netstat -ano | findstr :4200
TaskKill.exe /F /PID 12784
參考地址:https://stackoverflow.com/questions/38586364/ember-s-port-4200-is-already-in-use

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