Add ip to firewall linux

Introduction

For an introduction to firewalls, please see Firewall.

UFW — Uncomplicated Firewall

The default firewall configuration tool for Ubuntu is ufw. Developed to ease iptables firewall configuration, ufw provides a user friendly way to create an IPv4 or IPv6 host-based firewall. By default UFW is disabled.

Gufw is a GUI that is available as a frontend.

Basic Syntax and Examples

Default rules are fine for the average home user

When you turn UFW on, it uses a default set of rules (profile) that should be fine for the average home user. That’s at least the goal of the Ubuntu developers. In short, all ‘incoming’ is being denied, with some exceptions to make things easier for home users.

Enable and Disable

Enable UFW

To turn UFW on with the default set of rules:

To check the status of UFW:

The output should be like this:

youruser@yourcomputer:~$ sudo ufw status verbose [sudo] password for youruser: Status: active Logging: on (low) Default: deny (incoming), allow (outgoing) New profiles: skip youruser@yourcomputer:~$

Note that by default, deny is being applied to incoming. There are exceptions, which can be found in the output of this command:

You can also read the rules files in /etc/ufw (the files whose names end with .rules).

Disable UFW

Allow and Deny (specific rules)

Allow

Deny

Delete Existing Rule

To delete a rule, simply prefix the original rule with delete. For example, if the original rule was:

sudo ufw delete deny 80/tcp

Services

You can also allow or deny by service name since ufw reads from /etc/services To see get a list of services:

Allow by Service Name

Deny by Service Name

Status

IconsPage/important.png

Checking the status of ufw will tell you if ufw is enabled or disabled and also list the current ufw rules that are applied to your iptables.

To check the status of ufw:

sudo ufw status Firewall loaded To Action From -- ------ ---- 22:tcp DENY 192.168.0.1 22:udp DENY 192.168.0.1 22:tcp DENY 192.168.0.7 22:udp DENY 192.168.0.7 22:tcp ALLOW 192.168.0.0/24 22:udp ALLOW 192.168.0.0/24

if ufw was not enabled the output would be:

sudo ufw status Status: inactive

Logging

Advanced Syntax

You can also use a fuller syntax, specifying the source and destination addresses, ports and protocols.

Allow Access

This section shows how to allow specific access.

Allow by Specific IP

sudo ufw allow from 207.46.232.182

Allow by Subnet

sudo ufw allow from 192.168.1.0/24

Allow by specific port and IP address

sudo ufw allow from to port

sudo ufw allow from 192.168.0.4 to any port 22

Allow by specific port, IP address and protocol

sudo ufw allow from to port proto

sudo ufw allow from 192.168.0.4 to any port 22 proto tcp

Enable PING

Note : Security by obscurity may be of very little actual benefit with modern cracker scripts. By default, UFW allows ping requests. You may find you wish to leave (icmp) ping requests enabled to diagnose networking problems.

Читайте также:  Command deleting folders linux

In order to disable ping (icmp) requests, you need to edit /etc/ufw/before.rules and remove the following lines:

# ok icmp codes -A ufw-before-input -p icmp --icmp-type destination-unreachable -j ACCEPT -A ufw-before-input -p icmp --icmp-type source-quench -j ACCEPT -A ufw-before-input -p icmp --icmp-type time-exceeded -j ACCEPT -A ufw-before-input -p icmp --icmp-type parameter-problem -j ACCEPT -A ufw-before-input -p icmp --icmp-type echo-request -j ACCEPT

or change the «ACCEPT» to «DROP»

# ok icmp codes -A ufw-before-input -p icmp --icmp-type destination-unreachable -j DROP -A ufw-before-input -p icmp --icmp-type source-quench -j DROP -A ufw-before-input -p icmp --icmp-type time-exceeded -j DROP -A ufw-before-input -p icmp --icmp-type parameter-problem -j DROP -A ufw-before-input -p icmp --icmp-type echo-request -j DROP

Deny Access

Deny by specific IP

sudo ufw deny from 207.46.232.182

Deny by specific port and IP address

sudo ufw deny from to port

sudo ufw deny from 192.168.0.1 to any port 22

Working with numbered rules

Listing rules with a reference number

You may use status numbered to show the order and id number of rules:

Editing numbered rules

Delete numbered rule

You may then delete rules using the number. This will delete the first rule and rules will shift up to fill in the list.

Insert numbered rule

sudo ufw insert 1 allow from

Advanced Example

Scenario: You want to block access to port 22 from 192.168.0.1 and 192.168.0.7 but allow all other 192.168.0.x IPs to have access to port 22 using tcp

sudo ufw deny from 192.168.0.1 to any port 22 sudo ufw deny from 192.168.0.7 to any port 22 sudo ufw allow from 192.168.0.0/24 to any port 22 proto tcp

IconsPage/important.png

This puts the specific rules first and the generic second. Once a rule is matched the others will not be evaluated (see manual below) so you must put the specific rules first. As rules change you may need to delete old rules to ensure that new rules are put in the proper order.

To check your rules orders you can check the status; for the scenario the output below is the desired output for the rules to work properly

sudo ufw status Firewall loaded To Action From -- ------ ---- 22:tcp DENY 192.168.0.1 22:udp DENY 192.168.0.1 22:tcp DENY 192.168.0.7 22:udp DENY 192.168.0.7 22:tcp ALLOW 192.168.0.0/24

Scenario change: You want to block access to port 22 to 192.168.0.3 as well as 192.168.0.1 and 192.168.0.7.

sudo ufw delete allow from 192.168.0.0/24 to any port 22 sudo ufw status Firewall loaded To Action From -- ------ ---- 22:tcp DENY 192.168.0.1 22:udp DENY 192.168.0.1 22:tcp DENY 192.168.0.7 22:udp DENY 192.168.0.7 sudo ufw deny 192.168.0.3 to any port 22 sudo ufw allow 192.168.0.0/24 to any port 22 proto tcp sudo ufw status Firewall loaded To Action From -- ------ ---- 22:tcp DENY 192.168.0.1 22:udp DENY 192.168.0.1 22:tcp DENY 192.168.0.7 22:udp DENY 192.168.0.7 22:tcp DENY 192.168.0.3 22:udp DENY 192.168.0.3 22:tcp ALLOW 192.168.0.0/24

IconsPage/important.png

If you simply add the deny rule the allow would have been above it and been applied instead of the deny

Читайте также:  Version linux mac os

Interpreting Log Entries

Based on the response to the post UFW log guide/tutorial ?.

The SPT and DPT values, along with SRC and DST values, will typically be the values you’ll focus on when analysing the firewall logs.

Pseudo Log Entry

Feb 4 23:33:37 hostname kernel: [ 3529.289825] [UFW BLOCK] IN=eth0 OUT= MAC=00:11:22:33:44:55:66:77:88:99:aa:bb:cc:dd SRC=444.333.222.111 DST=111.222.333.444 LEN=103 TOS=0x00 PREC=0x00 TTL=52 DF PROTO=UDP SPT=53 DPT=36427 LEN=83

Date

It’s good practice to watch the dates and times. If things are out of order or blocks of time are missing then an attacker probably messed with your logs.

Hostname

Uptime

The time in seconds since boot.

Logged Event

Short description of the logged event; e.g. [UFW BLOCK]

IN

If set, then the event was an incoming event.

OUT

If set, then the event was an outgoing event.

MAC

This provides a 14-byte combination of the Destination MAC, Source MAC, and EtherType fields, following the order found in the Ethernet II header. See Ethernet frame and EtherType for more information.

SRC

This indicates the source IP, who sent the packet initially. Some IPs are routable over the internet, some will only communicate over a LAN, and some will only route back to the source computer. See IP address for more information.

DST

This indicates the destination IP, who is meant to receive the packet. You can use whois.net or the cli whois to determine the owner of the IP address.

LEN

This indicates the length of the packet.

TOS

I believe this refers to the TOS field of the IPv4 header. See TCP Processing of the IPv4 Precedence Field for more information.

PREC

I believe this refers to the Precedence field of the IPv4 header.

TTL

This indicates the “Time to live” for the packet. Basically each packet will only bounce through the given number of routers before it dies and disappears. If it hasn’t found its destination before the TTL expires, then the packet will evaporate. This field keeps lost packets from clogging the internet forever. See Time to live for more information.

ID

Not sure what this one is, but it’s not really important for reading logs. It might be ufw’s internal ID system, it might be the operating system’s ID.

PROTO

This indicates the protocol of the packet — TCP or UDP. See TCP and UDP Ports Explained for more information.

SPT

This indicates the source. I believe this is the port, which the SRC IP sent the IP packet over. See List of TCP and UDP port numbers for more information.

DPT

This indicates the destination port. I believe this is the port, which the SRC IP sent its IP packet to, expecting a service to be running on this port.

WINDOW

This indicates the size of packet the sender is willing to receive.

RES

This bit is reserved for future use & is always set to 0. Basically it’s irrelevant for log reading purposes.

SYN URGP

SYN indicates that this connection requires a three-way handshake, which is typical of TCP connections. URGP indicates whether the urgent pointer field is relevant. 0 means it’s not. Doesn’t really matter for firewall log reading.

Other Resources

  • For instructions on using ufw first see the official server guide.
  • The most recent syntax and manual can be retrieved by getting the man page. Otherwise open a terminal window and type:
Читайте также:  Astra linux переустановка libreoffice

UFW (последним исправлял пользователь paulw2u 2017-03-31 14:52:46)

The material on this wiki is available under a free license, see Copyright / License for details
You can contribute to this wiki, see Wiki Guide for details

Источник

How to configure a firewall on Linux with firewalld

Learn how to install, configure, and use firewalld to restrict or allow a computer’s access to services, ports, networks, subnets, and IP addresses.

Stop worrying and love systemd

Firewalld is an open source, host-based firewall that seeks to prevent unauthorized access to your computer. A firewall is usually a minimum requirement by any information security team at any modern organization, but it’s also a good idea for general computer use.

Training & certification

Firewalld can restrict access to services, ports, and networks. You can block specific subnets and IP addresses.

As with any firewall, firewalld inspects all traffic traversing the various interfaces on your system. The traffic is allowed or rejected if the source address network matches a rule.

Firewalld uses the concept of zones to segment traffic that interacts with your system. A network interface is assigned to one or more zones, and each zone contains a list of allowed ports and services. A default zone is also available to manage traffic that does not match any zones.

Firewalld is the daemon’s name that maintains the firewall policies. Use the firewall-cmd command to interact with the firewalld configuration.

Check the firewalld configuration

Before getting started, confirm that firewalld is running:

The output is either running or not running. To start your firewall if it’s not running, use systemctl :

$ sudo systemctl --enable --now firewalld

View zones

To view all zones on a system, use the —get-zones option:

$ sudo firewall-cmd --get-zones

To display the default zone, use —get-default-zone :

$ sudo firewall-cmd --get-default-zone

By default, if firewalld is enabled and running and in the public zone, all incoming traffic is rejected except SSH and DHCP.

Allow a port

To allow traffic from any IP through a specific port, use the —add-port option along with the port number and protocol:

$ sudo firewall-cmd --add-port=80/tcp

This rule takes effect immediately but only lasts until the next reboot. Add the —permanent flag to make it persistent:

$ sudo firewall-cmd --add-port=80/tcp --permanent

Reload firewalld

I prefer to reload my firewall after making changes. To reload firewalld and all permanent rules:

Add a service

There are predefined services you can allow through your firewall. To see all predefined services available on your system:

$ sudo firewall-cmd --get-services

For example, to add the HTTP service to your firewall permanently, enter:

$ sudo firewall-cmd --add-service=http --permanent $ sudo firewall-cmd --reload

IT Automation ebook

Specify traffic by subnet

You can assign traffic coming from a particular subnet to a specific zone (which allows specific ports and services, possibly unique to just that zone).

For example, to assign the network 172.16.1.0/24 to the internal zone and to allow the Jenkins service:

$ sudo firewall-cmd --zone=internal \ --add-source=172.16.1.0/24 --permanent $ sudo firewall-cmd --add-service=jenkins --permanent $ sudo firewall-cmd --reload

List ports and services

You can list all ports and services allowed in the default zone using the —list-all option:

To view all settings for all zones, use —list-all-zones :

$ sudo firewall-cmd --list-all-zones

Know your firewall

A good firewall is an essential feature on modern computer systems, and firewalld is one of the most convenient available. Its commands are intuitive and clear, and its ability to report useful descriptions of its policies makes it easy to understand. Review your firewall settings, and try out some firewall-cmd commands today.

Источник

Оцените статью
Adblock
detector