如果有一天:你不再尋找愛情,只是去愛;你不再渴望成功,只是去做;你不再追求空泛的成長,只是開始修養自己的性情;你的人生一切,才真正開始。——紀伯倫
linux操作系列文章
- Linux網絡配置 (centos,ubuntu)
- Linux常用工具安裝
- Linux-nc命令詳解
- Linux-端口和防火牆相關
端口相關
1. 列出正在使用的端口
# 列出tcp的
netstat -ntlp
# 列出UDP的
netstat -nulp
2. 找出程序運行的端口
netstat -apn | grep ssh
#輸出結果
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1438/sshd
tcp 0 0 10.0.2.15:22 10.0.2.2:52964 ESTABLISHED 3008/sshd
tcp 0 0 10.0.2.15:22 10.0.2.2:52091 ESTABLISHED 2813/sshd
tcp 0 0 :::22 :::* LISTEN 1438/sshd
unix 3 [ ] STREAM CONNECTED 18443 3008/sshd
unix 3 [ ] STREAM CONNECTED 18442 3011/sshd
unix 2 [ ] DGRAM 18439 3008/sshd
unix 3 [ ] STREAM CONNECTED 17777 2813/sshd
unix 3 [ ] STREAM CONNECTED 17776 2816/sshd
firewalld防火牆操作
firewalld是防火牆的另一種程序,與iptables相同,但是使用起來要比iptables簡單的點,可以簡單的理解爲對iptables的封裝。
1. 安裝和啓用
# 安裝firewalld
yum install firewalld
#如果需要圖形界面
yum install firewall-config
systemctl start firewalld # 啓動
systemctl status firewalld # 或者 firewall-cmd --state 查看狀態
systemctl disable firewalld # 停止
systemctl stop firewalld # 禁用
systemctl enable firewalld # 開機啓動
systemctl disable firewalld # 取消開機啓動
2.端口管理
# 查詢端口是否開放
firewall-cmd --query-port=8080/tcp
# 打開443/TCP端口
firewall-cmd --add-port=443/tcp
# 永久打開3690/TCP端口
firewall-cmd --permanent --add-port=3690/tcp
# 永久打開端口好像需要reload一下,臨時打開好像不用,如果用了reload臨時打開的端口就失效了
firewall-cmd --reload
# 查看防火牆,添加的端口也可以看到
firewall-cmd --list-all
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp
參數解釋
- firwall-cmd:是Linux提供的操作firewall的一個工具;
- –permanent:表示設置爲持久;
- –add-port:標識添加的端口;
3. 端口轉發
要打開端口轉發,則需要先
firewall-cmd --zone=external --add-masquerade
轉發 22 端口數據至另一 ip 的 2055 端口上
firewall-cmd --zone=external --add-forward-port=port=22:proto=tcp:toport=2055:toaddr=192.168.1.100
iptables
linux的防火牆由netfilter和iptables組成。用戶空間的iptables制定防火牆規則,內核空間的netfilter實現防火牆功能。
netfilter(內核空間)位於Linux內核中的包過濾防火牆功能體系,稱爲Linux防火牆的“內核態”。
iptables(用戶空間)位於/sbin/iptables,是用來管理防火牆的命令的工具,爲防火牆體系提供過濾規則/策略,決定如何過濾或處理到達防火牆主機的數據包,稱爲Linux防火牆的“用戶態”。
1. 安裝和啓動
# 關閉防火牆
systemctl stop firewalld
# 安裝
yum install iptables-services
#啓動
systemctl enable iptables
# 然後就可以用下面的命令了
service iptables start/restart/stop/status
2. 開啓和關閉端口
開啓端口
iptables -A INPUT -ptcp --dport 3000 -j ACCEPT
移除端口
# 入規則
iptables -A INPUT -p tcp --drop 端口號-j DROP
# 出規則
iptables -A OUTPUT -p tcp --dport 端口號-j DROP
保存規則
#centos7 保存
service iptables save
# 保存
/etc/rc.d/init.d/iptables save
# 重啓
/etc/init.d/iptables restart