页面“运维规划2018-2024”与“Ufw on debian”之间的差异

来自linux中国网wiki
(页面间的差异)
跳到导航 跳到搜索
→‎ruby
 
 
第1行: 第1行:
[[category:devops]]  
+
[[category:ops]] [[category:debian]] 
=大龄运维=
 
go ,cmdb, js  devops
 
  
https://www.v2ex.com/t/598440?__cf_chl_jschl_tk__=4fd0e10bc1ee83eb05176806d9433076e1fa08e8-1597039021-0-Af0TkNavo215SSTNLsdKOV9tqmTbckqd9aV_DrcqROga2l0U_TbpmLo5RYrY1VFEtsDNtYp82uGAiF9rOj-5LQw_8R9ic8SMnnk-lvBz1VxXZnlc4af3Qr6KC5Rqe9v1nX3W3hmXhO6NtxE8dgXbs13Wo5-xE5wgt5At15GuUC8UDd6v0W2w6eGdw9VBxks5jXlk-nZ2sc9xycmrjEwl6l4HZYVEvLnNdBRJpV9Aa6LG8T7qnoAvGeT-eQYqbUN0b7T6fRXFkjxqNbTOgOLm_xuL7sou1BKNe9zd1kNNfFtgcoKekkuwYNFcMP9ve9PTKRddmq1E5ELO8PzFSOZWMB8
+
=on my kali2020 laptop=
 +
<pre>
  
 +
#on my laptop
 +
sudo apt install ufw
  
[https://www.zhihu.com/question/27214752 什么是运维?运维工程师这个职位有多重要?]
+
sudo ufw default deny incoming
  
=2020=
+
sudo ufw default allow outgoing
==english==
+
 
 +
sudo ufw allow ssh
 +
 
 +
#is a web
 +
  sudo ufw allow 80
 +
  sudo ufw allow 443
 +
 +
#Firewall is active and enabled on system startup
 +
sudo ufw enable
  
==redis==
+
sudo ufw status
wiki上相关的可以看看先
+
</pre>
  
[[Redis运维]]
+
=线上一般的结合操作=
 +
<pre>
 +
apt  install ufw
 +
ufw enable
  
==lua==
+
#Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
[[Lua基础]]
+
#Firewall is active and enabled on system startup
  
==golang==
 
  
== Zookeeper==
+
sudo ufw default allow outgoing
https://www.jianshu.com/u/3829416c7065
+
sudo ufw default deny incoming
==see also==
 
  
[https://zhuanlan.zhihu.com/p/66483660 运维超级好用工具大PK]
+
ufw  allow ssh
 +
ufw  allow  www
  
=k8s=
+
etc
搞个国内的吧  debian  主要就是这下面三个站内资源
 
  
[https://developer.aliyun.com/mirror/kubernetes?spm=a2c6h.13651102.0.0.3e221b11Au1vN6 Kubernetes 镜像 aliyun]
 
  
 +
正常返回如下
  
 +
ufw  status
 +
Status: active
  
[[Install and Configure Kubernetes (k8s) on debian10]]
+
To                        Action      From
 +
--                        ------      ----
 +
SSH                        ALLOW      Anywhere                 
 +
80/tcp                    ALLOW      Anywhere                 
 +
SSH (v6)                   ALLOW      Anywhere (v6)           
 +
80/tcp (v6)                ALLOW      Anywhere (v6) 
  
 +
</pre>
  
[[使用kubeadm离线部署kubernetesv1.9.0 on centos7]]
+
=*  install=
  
 +
apt  install ufw
  
 +
=* Configuration=
 +
<pre>
 +
ufw enable
 +
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
 +
Firewall is active and enabled on system startup
  
[[2018k8s]]
 
  
 +
UFW’s defaults are to deny all incoming connections and allow all outgoing connections.
  
[https://zhuanlan.zhihu.com/p/46341911 kubernetes安装(国内环境)]
+
要确定ufw  status 后有没有规则可见
  
 +
可见 这两个不要手工执行了 默认就有的 现执行就可以连ssh都上不去了  昨天也是这样 2020年 10月 27日
 +
ufw default deny incoming
 +
ufw default allow outgoing
  
[https://www.cnblogs.com/chenzhenqi/p/10695959.html 国内环境安装k8s]
+
ufw status verbose
 
+
</pre>
  
 
+
=* Firewall Rules=
国内k8s集群部署的几种方式
 
部署方式
 
 
 
目前我所了解有以下几种方式:
 
 
<pre>
 
<pre>
    使用kubeadmin通过离线镜像的方式,网上教程和镜像包挺多的
+
ufw app list
 +
  
    通过厂商集成的方式如:rancher
+
ufw  allow 'SSH'
    这里推荐一篇blog:CentOS7环境安装Kubernetes四部曲https://blog.csdn.net/boling_cavalry/article/details/79613037
+
ufw  allow  22/tcp
 +
ufw  allow WWW #其实就是80
  
    直接使用类似阿里云这样的公有云平台k8s 。只要有钱,一键搞定 : )
+
 +
 +
ufw allow 'Nginx HTTP'
  
    通过yum官方仓库安装,需要启用extras仓库。
 
    不过里面版本都比较老,目前最新的也只是1.5.2版本,对于想学新的东西,比如k8s角色管理、helm等来说不太方便,因此也不建议使用。
 
  
    通过二进制包的形式进行安装,这里推荐一个repo:kubeasz https://github.com/gjmzj/kubeasz
+
ufw allow 53/tcp </pre>
    里面文档也很丰富清晰,对于初学者来说很有帮助。 </pre>
 
  
原文链接:https://blog.csdn.net/lusyoe/article/details/80217291
 
  
=服务器费用=
 
自动续费 ?
 
  
=cmdb=
+
==** Port Ranges ==
搞在xk intra 然后 frp到外面
+
<pre>
 +
Port ranges may also be specified, a simple example for tcp would be:
  
[https://www.linuxprobe.com/cmdb-things.html 前聚美优品运维负责人谈CMDB]
+
  ufw allow 1000:2000/tcp
  
[https://www.jianshu.com/p/f3a70cddccc5 CMDB和运维自动化]
+
and for udp:
  
[http://www.yunweipai.com/archives/6856.html (深度好文)重构CMDB,避免运维之耻]
+
  ufw allow 1000:2000/udp</pre>
  
[https://segmentfault.com/q/1010000003838760 开源CMDB]
+
==** IP address==
 +
<pre>An IP address may also be used:
  
=运维自动化=
+
ufw allow from 111.222.333.444</pre>
  
[http://os.51cto.com/art/201806/576802.htm 小团队如何从零搭建一个自动化运维体系]
+
=* Deleting Rules=
 +
<pre>
 +
Rules may be deleted with the following command:
  
[http://os.51cto.com/art/201806/576888.htm 如何做好自动化运维?自动化运维必备技能有哪些]
+
ufw delete allow ssh
  
 +
ufw reset
  
[[category:ops]] [[category:生活与情感]]
 
==python 自动化 ==
 
  
[https://blog.csdn.net/aeolus_pu/article/details/52605109 利用python实现批量自动化运维脚本案例]
+
</pre>
  
早下班时回家记得看书和动手例子
+
=开启/禁用相应端口或服务举例=
 
+
<pre>
=监控=
+
sudo ufw allow 80 #允许外部访问80端口
==zabbix==
+
sudo ufw delete allow 80 #禁止外部访问80 端口
 +
sudo ufw allow from 192.168.1.1 #允许此IP访问所有的本机端口
 +
sudo ufw deny smtp #禁止外部访问smtp服务,#以服务名代表端口,可以使用less /etc/services列出所有服务信息, 其中包括该服务使用了哪个端口和哪种协议
 +
sudo ufw delete allow smtp #删除上面建立的某条规则,或者sudo ufw delete allow 80/tcp,如果出现无法删除,可以用序号:sudo ufw status numbered,然后通过序号删除sudo ufw delete 1
 +
sudo ufw deny proto tcp from 10.0.0.0/8 to 192.168.0.1 port 22 #要拒绝所有的TCP流量从10.0.0.0/8 到192.168.0.1地址的22端口
 +
#可以允许所有RFC1918网络(局域网/无线局域网的)访问这个主机(/8,/16,/12是一种网络分级):
 +
sudo ufw allow from 10.0.0.0/8
 +
sudo ufw allow from 172.16.0.0/12
 +
sudo ufw allow from 192.168.0.0/16
 +
</pre>
 +
==eg==
 +
<pre>
 +
允许8090到9090之间的端口都允许tcp/udp访问,我们可以通过以下命令解决。
  
[[Zabbix报警]]
+
sudo ufw allow 8090:9090/tcp
 +
sudo ufw allow 8090:9090/udp
  
[https://blog.csdn.net/wyl9527/article/details/73928853 action_email 还有报警前执行脚本]
+
允许特定的IP地址
  
email 报警
+
要允许从特定IP地址访问所有端口,可以使用ufw allow from命令,后跟IP地址。命令如下:
短信报警
 
wechat 报警
 
  
zabbix new server docker 化
+
sudo ufw allow from 115.127.62.61
[https://blog.csdn.net/heyongluoyao8/article/details/51413668 服务器监控(包括性能指标与web应用程序)]
 
  
==pinpoint==
+
当然也可以允许特定IP访问前提条件下,允许其访问特定端口,命令如下:
  
== Nightingale ==
+
sudo ufw allow from 115.127.62.61 to any port 22
[https://www.oschina.net/p/nightingale-didi nightingale-didi]
 
  
Open-Falcon 是小米运维团队开源的企业级监控解决方案,在国内广泛使用。
 
  
Nightingale 采用 Apache-2.0 开源协议,Copyright © 滴滴 2020
+
允许子网
  
=python=
+
允许从IP地址的子网进行连接的命令与使用单个IP地址时的命令相同。 唯一的区别是您需要指定网络掩码。 例如,如果要允许访问从192.168.1.1到192.168.1.254的IP地址到端口3360(MySQL),则可以使用以下命令:
[https://blog.csdn.net/chengxuyuanyonghu/article/details/59486631 python常用运维脚本实例]
 
  
 +
sudo ufw allow from 192.168.1.0/24 to any port 3306
  
[https://blog.csdn.net/hardywang/article/details/51536510  python用于系统运维管理和灰度发布部署]
+
允许连接到特定的网络接口
  
[http://www.voidcn.com/article/p-poyppygk-ng.html 基于python的一个运维自动化的项目(进度更新)【已开源】]
+
要允许访问特定端口,假设仅将3360端口访问特定网络接口eth2,可以使用allow in on和网络接口名称:
  
=django=
+
sudo ufw allow in on eth2 to any port 3306
  
Gunicorn
+
拒绝连接
  
=ruby=
+
所有传入连接的默认策略都设置为“拒绝”,这意味着除非你专门打开该连接,否则UFW将阻止所有传入连接。
[[Learn ruby]]
 
  
=tomcat=
+
假设您打开了端口80和443,并且服务器受到23.24.25.0/24网络的攻击。 要拒绝来自23.24.25.0/24的所有连接,可以使用以下命令:
[http://blog.51cto.com/jowin/1739400 Linux运维 第三阶段 (十九) tomcat]
 
  
[https://blog.csdn.net/lifetragedy/article/details/7708724 通向架构师的道路(第四天)之Tomcat性能调优]
+
sudo ufw deny from 23.24.25.0/24
  
[https://www.jianshu.com/p/d45e12241af4 Tomcat性能调优-JVM监控与调优]
+
如果只想拒绝从23.24.25.0/24访问端口80和443,请使用:
  
[https://blog.csdn.net/xyang81/article/details/51530979 Tomcat配置与优化(内存、并发、管理)]
+
sudo ufw deny from 23.24.25.0/24 to any port 80
 +
sudo ufw deny from 23.24.25.0/24 to any port 443
  
 +
编写拒绝规则与编写允许规则相同。 您只需要将allow替换为deny即可。
 +
删除UFW规则
  
[https://www.centos.bz/2017/08/tomcat-jvm-performance-tuning/ Tomcat和JVM的性能调优总结]
+
删除UFW规则有两种方法, 通过规则编号和通过指定实际规则。
  
[https://www.oschina.net/translate/tomcat-performance-tuning Tomcat 生产服务器性能优化 【已翻译100%】]
+
通过规则编号删除UFW规则比较容易,特别是如果您不熟悉UFW。
  
[http://yunzhu.iteye.com/blog/953387 监控Tomcat解决方案(监控应用服务器系列文章)]
+
要首先按规则编号删除规则,您需要找到要删除的规则编号。 为此,请运行以下命令:
  
==tomcat优化==
+
sudo ufw status numbered
  
[https://blog.csdn.net/coolwzjcool/article/details/2544448 TOMCAT内存大小调整]
+
输出类似如下:
  
[http://elf8848.iteye.com/blog/378805 Tomcat内存设置详解]
+
Status: active
  
[https://blog.csdn.net/xyang81/article/details/51530979 Tomcat配置与优化(内存、并发、管理)]
+
    To                        Action      From
 +
    --                        ------      ----
 +
[ 1] 22/tcp                    ALLOW IN    Anywhere
 +
[ 2] 80/tcp                    ALLOW IN    Anywhere
 +
[ 3] 8080/tcp                  ALLOW IN    Anywhere
  
[https://blog.csdn.net/lifetragedy/article/details/7708724 通向架构师的道路(第四天)之Tomcat性能调优-让小猫飞奔]
+
要删除规则号3(允许连接到端口8080的规则),可以使用以下命令:
  
==tomcat 启动脚本==
+
sudo ufw delete 3
<pre>
 
#!/bin/bash
 
#cat /etc/init.d/pp-col  在centos7上试过 可运行
 
# chkconfig: 345 99 28
 
# description: Starts/Stops Apache Tomcat
 
  
#by evan
+
第二种方法是通过指定实际规则来删除规则。 例如,如果您添加了打开端口8069的规则,则可以使用以下命令将其删除:
#修改几个目录
 
##export JAVA_HOME=/usr/java/jdk1.8.0_121/
 
#sed -i "s/JAVA_HOME=\/usr\/java\/default\//JAVA_HOME=\/usr\/java\/jdk1.8.0_121\//g" pp-collector.init
 
#sed -i "s/CATALINA_HOME=\/data\/service\/pinpoint-collector\//CATALINA_HOME=\/data\/pp-col\//g" pp-collector.init
 
#sed -i "s/CATALINA_BASE=\/data\/service\/pinpoint-collector\//CATALINA_BASE=\/data\/pp-col\//g" pp-collector.init
 
#by evan
 
  
# Tomcat  start/stop/status script
+
sudo ufw delete allow 8069
# Forked from: https://gist.github.com/valotas/1000094
 
# @author: Miglen Evlogiev <[email protected]>
 
#
 
# Release updates:
 
# Updated method for gathering pid of the current proccess
 
# Added usage of CATALINA_BASE
 
# Added coloring and additional status
 
# Added check for existence of the tomcat user
 
#
 
 
#Location of JAVA_HOME (bin files)
 
export JAVA_HOME=/usr/java/jdk1.8.0_121/
 
 
#Add Java binary files to PATH
 
export PATH=$JAVA_HOME/bin:$PATH
 
 
#CATALINA_HOME is the location of the bin files of Tomcat 
 
export CATALINA_HOME=/data/pp-col/
 
 
#CATALINA_BASE is the location of the configuration files of this instance of Tomcat
 
export CATALINA_BASE=/data/pp-col/
 
 
#TOMCAT_USER is the default user of tomcat
 
export TOMCAT_USER=root
 
 
#TOMCAT_USAGE is the message if this script is called without any options
 
TOMCAT_USAGE="Usage: $0 {\e[00;32mstart\e[00m|\e[00;31mstop\e[00m|\e[00;32mstatus\e[00m|\e[00;31mrestart\e[00m}"
 
 
#SHUTDOWN_WAIT is wait time in seconds for java proccess to stop
 
SHUTDOWN_WAIT=3
 
 
tomcat_pid() {
 
        echo `ps -fe | grep $CATALINA_BASE | grep -v grep | tr -s " "|cut -d" " -f2`
 
}
 
 
start() {
 
  pid=$(tomcat_pid)
 
  if [ -n "$pid" ]
 
  then
 
    echo -e "\e[00;31mTomcat is already running (pid: $pid)\e[00m"
 
  else
 
    # Start tomcat
 
    echo -e "\e[00;32mStarting tomcat\e[00m"
 
    #ulimit -n 100000
 
    #umask 007
 
    #/bin/su -p -s /bin/sh tomcat
 
        if [ `user_exists $TOMCAT_USER` = "1" ]
 
        then
 
                su $TOMCAT_USER -c $CATALINA_HOME/bin/startup.sh
 
        else
 
                sh $CATALINA_HOME/bin/startup.sh
 
        fi
 
        status
 
  fi
 
  return 0
 
}
 
 
status(){
 
          pid=$(tomcat_pid)
 
          if [ -n "$pid" ]; then echo -e "\e[00;32mTomcat is running with pid: $pid\e[00m"
 
          else echo -e "\e[00;31mTomcat is not running\e[00m"
 
          fi
 
}
 
 
stop() {
 
  pid=$(tomcat_pid)
 
  if [ -n "$pid" ]
 
  then
 
    echo -e "\e[00;31mStoping Tomcat\e[00m"
 
    #/bin/su -p -s /bin/sh tomcat
 
        sh $CATALINA_HOME/bin/shutdown.sh
 
 
    let kwait=$SHUTDOWN_WAIT
 
    count=0;
 
    until [ `ps -p $pid | grep -c $pid` = '0' ] || [ $count -gt $kwait ]
 
    do
 
      echo -n -e "\n\e[00;31mwaiting for processes to exit\e[00m";
 
      sleep 1
 
      let count=$count+1;
 
    done
 
 
    if [ $count -gt $kwait ]; then
 
      echo -n -e "\n\e[00;31mkilling processes which didn't stop after $SHUTDOWN_WAIT seconds\e[00m"
 
      kill -9 $pid
 
    fi
 
  else
 
    echo -e "\e[00;31mTomcat is not running\e[00m"
 
  fi
 
 
  return 0
 
}
 
 
user_exists(){
 
        if id -u $1 >/dev/null 2>&1; then
 
        echo "1"
 
        else
 
                echo "0"
 
        fi
 
}
 
 
case $1 in
 
 
        start)
 
          start
 
        ;;
 
     
 
        stop) 
 
          stop
 
        ;;
 
     
 
        restart)
 
          stop
 
          start
 
        ;;
 
     
 
        status)
 
                status
 
             
 
        ;;
 
     
 
        *)
 
                echo -e $TOMCAT_USAGE
 
        ;;
 
esac   
 
exit 0
 
  
 
</pre>
 
</pre>
  
<pre>[root@node2 tomcat]# vim /etc/init.d/tomcat(tomcat启动脚本)
+
=troubleshooting=
 +
<pre>
  
------------script start--------------
+
openssh都连接上去
  
#!/bin/sh
+
这个导致上不了的
 +
ufw default deny incoming
  
# Tomcat init script for Linux.
 
  
#
+
确定了 不是这个问题  是
 +
ufw  status verbose
 +
Status: active
 +
Logging: on (low)
 +
Default: deny (incoming), allow (outgoing), disabled (routed)
 +
New profiles: skip
  
# chkconfig: 2345 96 14
+
To                        Action      From
 +
--                        ------      ----
 +
22/tcp (SSH)              ALLOW IN    Anywhere                 
 +
80/tcp (WWW)              ALLOW IN    Anywhere                 
 +
22/tcp (SSH (v6))          ALLOW IN    Anywhere (v6)           
 +
80/tcp (WWW (v6))          ALLOW IN    Anywhere (v6) 
 +
没有东西 
  
# description: The Apache Tomcatservlet/JSP container.
+
linode 文档得了
  
export JAVA_HOME=/usr/java/latest
+
如何确定在 ssh这后再deny incoming 呢  参考iptalbes ?
  
export CATALINA_HOME=/usr/local/tomcat
 
  
exec $CATALINA_HOME/bin/catalina.sh $*
 
  
--------------script end---------------
+
</pre>
 
 
[root@node2 tomcat]# chmod +x !$
 
 
 
chmod +x /etc/init.d/tomcat
 
 
 
[root@node2 tomcat]# chkconfig --add tomcat
 
 
 
[root@node2 tomcat]# chkconfig tomcat on
 
 
 
[root@node2 tomcat]# chkconfig --list tomcat
 
 
 
tomcat            0:关闭      1:关闭      2:启用      3:启用      4:启用      5:启用      6:关闭
 
 
 
[root@node2 tomcat]# service tomcat stop
 
 
 
……
 
 
 
[root@node2 tomcat]# service tomcat start</pre>
 
 
 
=safe=
 
[https://blog.linuxeye.cn/454.html fail2ban阻止SSH暴力破解]
 
 
 
== safe dns==
 
http://www.freenom.world/zh/index.html?lang=zh
 
 
 
Freenom是世界上第一个也是唯一的免费域名提供商。 我们的使命是把互联网带给世界人民,帮助各国发展自己的数字经济。
 
 
 
通过打破壁垒、整合免费域名与最新网站建设和托管技术,Freenom让企业或个人建立网站和管理内容很容易,没有任何成本。
 
 
 
=docker k8s=
 
 
 
=intra=
 
==内网穿透打洞==
 
[[内网穿透打洞]]
 
 
 
==内网环境==
 
oracle 17
 
 
 
nginx  other 11
 
 
 
frp
 
 
 
但是没有开机自启动呢
 
 
 
=apps=
 
==7.1redis==
 
公司的redis要 HA
 
 
 
[https://my.oschina.net/u/2600078/blog/1923696 redis架构演变与redis-cluster群集读写方案 ]
 
  
==7.2 nginx==
+
=* see also=
 +
https://wiki.debian.org/Uncomplicated%20Firewall%20%28ufw%29
  
redis  lua+ cc
+
https://www.linode.com/docs/security/firewalls/configure-firewall-with-ufw/
[[Nginx Lua Redis防止CC攻击]]
 
  
==7.3 mysql==
+
[https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-with-ufw-on-debian-9 How To Set Up a Firewall with UFW on Debian 9]
mysql gpt
 
  
==7.4 dns==
+
https://help.ubuntu.com/community/UFW
[http://blog.51cto.com/sweetpotato/1596973 Linux运维实战之DNS基础]
 
  
==7.5 CI jenkins==
+
https://www.digitalocean.com/community/tutorials/how-to-setup-a-firewall-with-ufw-on-an-ubuntu-and-debian-cloud-server
  
==备份==
+
[https://zhuanlan.zhihu.com/p/36646621 ubuntu ufw 防火墙]
==关键文件备份==
 
passwd  etc
 
  
== 更新==
+
[https://linux265.com/news/5984.html 如何在Debian 10中使用ufw设置防火墙]
 
mysql 文件 这个尽量做成自动化
 
接下何焱的活
 
==db备份==
 
  
=on =
+
[https://www.configserverfirewall.com/debian-linux/install-ufw-firewall-debian/ How to Install UFW Ubuntu Firewall on Debian Linux]
# 内网开发环境
 
# jenkins 自动化更新 (dev test)
 
# zabbix 报警
 
# pinpoint
 
#
 
#
 
#
 
# 有序列表项
 
#
 
# 有序列表项
 
#
 
# 有序列表项
 
#
 

2020年12月2日 (三) 14:15的版本


on my kali2020 laptop


#on my laptop
sudo apt install ufw

sudo ufw default deny incoming

 sudo ufw default allow outgoing
  
 sudo ufw allow ssh
  
 #is a web 
  sudo ufw allow 80
   sudo ufw allow 443 
 
 #Firewall is active and enabled on system startup
 sudo ufw enable

sudo ufw status

线上一般的结合操作

apt  install ufw 
ufw enable 

#Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
#Firewall is active and enabled on system startup


sudo ufw default allow outgoing
sudo ufw default deny incoming

 ufw  allow ssh 
 ufw  allow  www

etc 


正常返回如下 

 ufw  status 
Status: active

To                         Action      From
--                         ------      ----
SSH                        ALLOW       Anywhere                  
80/tcp                     ALLOW       Anywhere                  
SSH (v6)                   ALLOW       Anywhere (v6)             
80/tcp (v6)                ALLOW       Anywhere (v6)  

* install

apt  install ufw

* Configuration

 ufw enable 
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup


UFW’s defaults are to deny all incoming connections and allow all outgoing connections.

要确定ufw  status 后有没有规则可见 

可见 这两个不要手工执行了 默认就有的 现执行就可以连ssh都上不去了  昨天也是这样 2020年 10月 27日
ufw default deny incoming
ufw default allow outgoing

ufw status verbose

* Firewall Rules

 ufw app list
 

 ufw  allow 'SSH'
ufw  allow  22/tcp
 ufw  allow WWW #其实就是80

 
 
 ufw allow 'Nginx HTTP'


ufw allow 53/tcp 


** Port Ranges

Port ranges may also be specified, a simple example for tcp would be:

  ufw allow 1000:2000/tcp

and for udp:

  ufw allow 1000:2000/udp

** IP address

An IP address may also be used:

 ufw allow from 111.222.333.444

* Deleting Rules

Rules may be deleted with the following command:

 ufw delete allow ssh

ufw reset


开启/禁用相应端口或服务举例

sudo ufw allow 80 #允许外部访问80端口
sudo ufw delete allow 80 #禁止外部访问80 端口
sudo ufw allow from 192.168.1.1 #允许此IP访问所有的本机端口
sudo ufw deny smtp #禁止外部访问smtp服务,#以服务名代表端口,可以使用less /etc/services列出所有服务信息, 其中包括该服务使用了哪个端口和哪种协议
sudo ufw delete allow smtp #删除上面建立的某条规则,或者sudo ufw delete allow 80/tcp,如果出现无法删除,可以用序号:sudo ufw status numbered,然后通过序号删除sudo ufw delete 1
sudo ufw deny proto tcp from 10.0.0.0/8 to 192.168.0.1 port 22 #要拒绝所有的TCP流量从10.0.0.0/8 到192.168.0.1地址的22端口
#可以允许所有RFC1918网络(局域网/无线局域网的)访问这个主机(/8,/16,/12是一种网络分级):
sudo ufw allow from 10.0.0.0/8
sudo ufw allow from 172.16.0.0/12
sudo ufw allow from 192.168.0.0/16

eg

允许8090到9090之间的端口都允许tcp/udp访问,我们可以通过以下命令解决。

sudo ufw allow 8090:9090/tcp
sudo ufw allow 8090:9090/udp

允许特定的IP地址

要允许从特定IP地址访问所有端口,可以使用ufw allow from命令,后跟IP地址。命令如下:

sudo ufw allow from 115.127.62.61

当然也可以允许特定IP访问前提条件下,允许其访问特定端口,命令如下:

sudo ufw allow from 115.127.62.61 to any port 22


允许子网

允许从IP地址的子网进行连接的命令与使用单个IP地址时的命令相同。 唯一的区别是您需要指定网络掩码。 例如,如果要允许访问从192.168.1.1到192.168.1.254的IP地址到端口3360(MySQL),则可以使用以下命令:

sudo ufw allow from 192.168.1.0/24 to any port 3306

允许连接到特定的网络接口

要允许访问特定端口,假设仅将3360端口访问特定网络接口eth2,可以使用allow in on和网络接口名称:

sudo ufw allow in on eth2 to any port 3306

拒绝连接

所有传入连接的默认策略都设置为“拒绝”,这意味着除非你专门打开该连接,否则UFW将阻止所有传入连接。

假设您打开了端口80和443,并且服务器受到23.24.25.0/24网络的攻击。 要拒绝来自23.24.25.0/24的所有连接,可以使用以下命令:

sudo ufw deny from 23.24.25.0/24

如果只想拒绝从23.24.25.0/24访问端口80和443,请使用:

sudo ufw deny from 23.24.25.0/24 to any port 80
sudo ufw deny from 23.24.25.0/24 to any port 443

编写拒绝规则与编写允许规则相同。 您只需要将allow替换为deny即可。
删除UFW规则

删除UFW规则有两种方法, 通过规则编号和通过指定实际规则。

通过规则编号删除UFW规则比较容易,特别是如果您不熟悉UFW。

要首先按规则编号删除规则,您需要找到要删除的规则编号。 为此,请运行以下命令:

sudo ufw status numbered

输出类似如下:

Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 22/tcp                     ALLOW IN    Anywhere
[ 2] 80/tcp                     ALLOW IN    Anywhere
[ 3] 8080/tcp                   ALLOW IN    Anywhere

要删除规则号3(允许连接到端口8080的规则),可以使用以下命令:

sudo ufw delete 3

第二种方法是通过指定实际规则来删除规则。 例如,如果您添加了打开端口8069的规则,则可以使用以下命令将其删除:

sudo ufw delete allow 8069

troubleshooting


openssh都连接上去 

这个导致上不了的 
 ufw default deny incoming


确定了 不是这个问题  是 
ufw  status verbose 
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp (SSH)               ALLOW IN    Anywhere                  
80/tcp (WWW)               ALLOW IN    Anywhere                  
22/tcp (SSH (v6))          ALLOW IN    Anywhere (v6)             
80/tcp (WWW (v6))          ALLOW IN    Anywhere (v6)   
没有东西  

linode 文档得了

如何确定在 ssh这后再deny incoming 呢  参考iptalbes ?



* see also

https://wiki.debian.org/Uncomplicated%20Firewall%20%28ufw%29

https://www.linode.com/docs/security/firewalls/configure-firewall-with-ufw/

How To Set Up a Firewall with UFW on Debian 9

https://help.ubuntu.com/community/UFW

https://www.digitalocean.com/community/tutorials/how-to-setup-a-firewall-with-ufw-on-an-ubuntu-and-debian-cloud-server

ubuntu ufw 防火墙

如何在Debian 10中使用ufw设置防火墙

How to Install UFW Ubuntu Firewall on Debian Linux