Config firewalld » Historie » Version 3
Jeremias Keihsler, 09.09.2021 18:26
1 | 1 | Jeremias Keihsler | h1. Config firewalld |
---|---|---|---|
2 | |||
3 | have a look at |
||
4 | * https://www.digitalocean.com/community/tutorials/how-to-set-up-a-firewall-using-firewalld-on-centos-8 |
||
5 | * https://www.certdepot.net/rhel7-get-started-firewalld/ |
||
6 | 2 | Jeremias Keihsler | * https://www.liquidweb.com/kb/an-introduction-to-firewalld/ |
7 | 1 | Jeremias Keihsler | |
8 | 2 | Jeremias Keihsler | h2. Add a Port for TCP or UDP |
9 | |||
10 | You do have to specify TCP or UDP and to open a port for both. You will need to add rules for each protocol. |
||
11 | |||
12 | <pre><code class="shell"> |
||
13 | firewall-cmd --permanent --add-port=22/TCP |
||
14 | firewall-cmd --permanent --add-port=53/UDP |
||
15 | </code></pre> |
||
16 | |||
17 | h2. Remove a Port for TCP or UDP |
||
18 | |||
19 | Using a slight variation on the above structure, you can remove a currently open port, effectively closing off that port. |
||
20 | |||
21 | <pre><code class="shell"> |
||
22 | firewall-cmd --permanent --remove-port=444/tcp |
||
23 | </code></pre> |
||
24 | |||
25 | h2. Add a Service |
||
26 | |||
27 | These services assume the default ports configured within the /etc/services configuration file; if you wish to use a service on a non-standard port, you will have to open the specific port, as in the example above. |
||
28 | |||
29 | <pre><code class="shell"> |
||
30 | firewall-cmd --permanent --add-service=ssh |
||
31 | firewall-cmd --permanent --add-service=http |
||
32 | </code></pre> |
||
33 | |||
34 | h2. Remove a Service |
||
35 | |||
36 | As above, you specify the remove-service option, and you can close off the port that is defined for that service. |
||
37 | |||
38 | <pre><code class="shell"> |
||
39 | firewall-cmd --permanent --remove-service=mysql |
||
40 | </code></pre> |
||
41 | |||
42 | h2. Whitelist an IP Address |
||
43 | |||
44 | To whitelist or allow access from an IP or range of IPs, you can tell the firewall to add a trusted source. |
||
45 | |||
46 | <pre><code class="shell"> |
||
47 | firewall-cmd --permanent --add-source=192.168.1.100 |
||
48 | </code></pre> |
||
49 | |||
50 | You can also allow a range of IPs using what is called CIDR notation. CIDR is outside the scope of this article but is a shorthand that can be used for noting ranges of IP addresses. |
||
51 | |||
52 | <pre><code class="shell"> |
||
53 | firewall-cmd --permanent --add-source=192.168.1.0/24 |
||
54 | </code></pre> |
||
55 | |||
56 | h2. Remove a Whitelisted IP Address |
||
57 | |||
58 | To remove a whitelisted IP or IP range, you can use the --remove-source option. |
||
59 | |||
60 | <pre><code class="shell"> |
||
61 | firewall-cmd --permanent --remove-source=192.168.1.100 |
||
62 | </code></pre> |
||
63 | |||
64 | h2. Block an IP Address |
||
65 | |||
66 | As the firewall-cmd tool is mostly used for opening or allowing access, rich rules are needed to block an IP. Rich rules are similar in form to the way iptables rules are written. |
||
67 | |||
68 | <pre><code class="shell"> |
||
69 | firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='192.168.1.100' reject" |
||
70 | </code></pre> |
||
71 | |||
72 | You can again use CIDR notation also block a range of IP addresses. |
||
73 | |||
74 | <pre><code class="shell"> |
||
75 | firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='192.168.1.0/24' reject" |
||
76 | </code></pre> |
||
77 | |||
78 | h2. to forward a specific port (not yet working!) |
||
79 | 3 | Jeremias Keihsler | |
80 | 2 | Jeremias Keihsler | <pre><code class="shell"> |
81 | 1 | Jeremias Keihsler | firewall-cmd --zone=public --permanent --add-port=2222/tcp |
82 | firewall-cmd --zone=public --add-masquerade --permanent |
||
83 | firewall-cmd --zone=public --add-forward-port=port=2222:proto=tcp:toport=22:toaddr=192.168.122.80 --permanent |
||
84 | firewall-cmd --reload |
||
85 | firewall-cmd --zone=public --list-all |
||
86 | </code></pre> |
||
87 | |||
88 | h1. Testing |
||
89 | |||
90 | you may consider http://portquiz.net/ |
||
91 | |||
92 | 2 | Jeremias Keihsler | <pre><code class="shell"> |
93 | 1 | Jeremias Keihsler | http://portquiz.net:1234/ |
94 | </code></pre> |
||
95 | |||
96 | This example tests whether you are able to visit outbound port 1234. You simply change the port number to whatever you like. Also, the site gives some examples that could be used in a command line script: |
||
97 | |||
98 | 2 | Jeremias Keihsler | <pre><code class="shell"> |
99 | 1 | Jeremias Keihsler | wget -qO- portquiz.net:1234 |
100 | </code></pre> |
||
101 | |||
102 | 2 | Jeremias Keihsler | <pre><code class="shell"> |
103 | 1 | Jeremias Keihsler | Port 1234 test successful! |
104 | Your IP: 198.252.206.16 |
||
105 | </code></pre> |