如果有一天:你不再寻找爱情,只是去爱;你不再渴望成功,只是去做;你不再追求空泛的成长,只是开始修养自己的性情;你的人生一切,才真正开始。——纪伯伦
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