firewall.html 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  1. <!DOCTYPE html>
  2. <html lang=en>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>Firewall</title>
  6. <link rel="stylesheet" type="text/css" href="en.css">
  7. <script type="text/javascript" src="jquery.js"></script><script type="text/javascript" src="jquery.syntax.js"></script><script type="text/javascript" src="yelp.js"></script>
  8. </head>
  9. <body id="home">
  10. <script src="https://ssl.google-analytics.com/urchin.js" type="text/javascript"></script><script type="text/javascript">
  11. _uacct = "UA-1018242-8";
  12. urchinTracker();
  13. </script><script>
  14. function englishPageVersion() {
  15. var href = window.location.href;
  16. if (href.slice(-1) == "/") {
  17. window.location = "index.html.en";
  18. } else {
  19. window.location = href.replace(/\.html.*/, ".html.en");
  20. }
  21. return false;
  22. }
  23. function browserPreferredLanguage() {
  24. var href = window.location.href;
  25. if (href.slice(-1) == "/") {
  26. window.location = href;
  27. } else {
  28. window.location = href.replace(/\.html.*/, ".html");
  29. }
  30. return false;
  31. }
  32. </script><div id="container">
  33. <div id="container-inner">
  34. <div id="mothership"><ul>
  35. <li><a href="https://partners.ubuntu.com">Partners</a></li>
  36. <li><a href="https://www.ubuntu.com/support/community-support">Support</a></li>
  37. <li><a href="https://community.ubuntu.com">Community</a></li>
  38. <li><a href="https://www.ubuntu.com">Ubuntu.com</a></li>
  39. </ul></div>
  40. <div id="header">
  41. <h1 id="ubuntu-header"><a href="https://help.ubuntu.com/">Ubuntu Documentation</a></h1>
  42. <ul id="main-menu">
  43. <li><a class="main-menu-item current" href="../../">Official Documentation</a></li>
  44. <li><a href="https://help.ubuntu.com/community/CommunityHelpWiki">Community Help Wiki</a></li>
  45. <li><a href="https://community.ubuntu.com/t/contribute/26">Contribute</a></li>
  46. </ul>
  47. </div>
  48. <div id="menu-search"><div id="search-box">
  49. <noscript><form action="https://www.google.com/cse" id="cse-search-box"><div>
  50. <input type="hidden" name="cx" value="003883529982892832976:e2vwumte3fq"><input type="hidden" name="ie" value="UTF-8"><input type="text" name="q" size="21"><input type="submit" name="sa" value="Search">
  51. </div></form></noscript>
  52. <script>
  53. document.write('<form action="../../search.html" id="cse-search-box">');
  54. document.write(' <div>');
  55. document.write(' <input type="hidden" name="cof" value="FORID:9">');
  56. document.write(' <input type="hidden" name="cx" value="003883529982892832976:e2vwumte3fq">');
  57. document.write(' <input type="hidden" name="ie" value="UTF-8">');
  58. document.write(' <input type="text" name="q" size="21">');
  59. document.write(' <input type="submit" name="sa" value="Search">');
  60. document.write(' </div>');
  61. document.write('</form>');
  62. </script>
  63. </div></div>
  64. <div class="trails"><div class="trail">
  65. <a href="../../18.04" class="trail">Ubuntu 18.04</a> » <a class="trail" href="index.html.en" title="Ubuntu Server Guide">Ubuntu Server Guide</a> » <a class="trail" href="security.html.en" title="Security">Security</a> » </div></div>
  66. <div id="cwt-content" class="clearfix content-area"><div id="page">
  67. <div id="content">
  68. <div class="links nextlinks">
  69. <a class="nextlinks-prev" href="console-security.html.en" title="Console Security">Previous</a><a class="nextlinks-next" href="apparmor.html.en" title="AppArmor">Next</a>
  70. </div>
  71. <div class="hgroup"><h1 class="title">Firewall</h1></div>
  72. <div class="region">
  73. <div class="contents"></div>
  74. <div class="links sectionlinks" role="navigation"><ul>
  75. <li class="links"><a class="xref" href="firewall.html.en#firewall-introduction" title="Introduction">Introduction</a></li>
  76. <li class="links"><a class="xref" href="firewall.html.en#firewall-ufw" title="ufw - Uncomplicated Firewall">ufw - Uncomplicated Firewall</a></li>
  77. <li class="links"><a class="xref" href="firewall.html.en#ip-masquerading" title="IP Masquerading">IP Masquerading</a></li>
  78. <li class="links"><a class="xref" href="firewall.html.en#firewall-logs" title="Logs">Logs</a></li>
  79. <li class="links"><a class="xref" href="firewall.html.en#other-firewall-tools" title="Other Tools">Other Tools</a></li>
  80. <li class="links"><a class="xref" href="firewall.html.en#firewall-references" title="References">References</a></li>
  81. </ul></div>
  82. <div class="sect2 sect" id="firewall-introduction"><div class="inner">
  83. <div class="hgroup"><h2 class="title">Introduction</h2></div>
  84. <div class="region"><div class="contents">
  85. <p class="para">
  86. The Linux kernel includes the <span class="em emphasis">Netfilter</span> subsystem,
  87. which is used to manipulate or decide the fate of network traffic headed into or through
  88. your server. All modern Linux firewall solutions use this system for packet filtering.
  89. </p>
  90. <p class="para">
  91. The kernel's packet filtering system would be of little use to administrators without
  92. a userspace interface to manage it. This is the purpose of iptables: When a packet
  93. reaches your server, it will be handed off to the Netfilter subsystem for acceptance,
  94. manipulation, or rejection based on the rules supplied to it from userspace via
  95. iptables. Thus, iptables is all you need to manage your firewall, if you're familiar
  96. with it, but many frontends are available to simplify the task.
  97. </p>
  98. </div></div>
  99. </div></div>
  100. <div class="sect2 sect" id="firewall-ufw"><div class="inner">
  101. <div class="hgroup"><h2 class="title">ufw - Uncomplicated Firewall</h2></div>
  102. <div class="region">
  103. <div class="contents">
  104. <p class="para">
  105. The default firewall configuration tool for Ubuntu is <span class="app application">ufw</span>. Developed to ease iptables firewall configuration,
  106. <span class="app application">ufw</span> provides a user-friendly way to create an IPv4 or IPv6 host-based firewall.
  107. </p>
  108. <p class="para">
  109. <span class="app application">ufw</span> by default is initially disabled. From the <span class="app application">ufw</span> man page:
  110. </p>
  111. <p class="para">
  112. <span class="quote">“
  113. ufw is not intended to provide complete firewall functionality via its command interface, but instead provides an easy way to add or remove simple rules. It is currently mainly used for host-based firewalls.
  114. ”</span>
  115. </p>
  116. <p class="para">
  117. The following are some examples of how to use <span class="app application">ufw</span>:
  118. </p>
  119. <div class="list itemizedlist"><ul class="list itemizedlist">
  120. <li class="list itemizedlist">
  121. <p class="para">
  122. First, <span class="app application">ufw</span> needs to be enabled. From a terminal prompt enter:
  123. </p>
  124. <div class="screen"><pre class="contents "><span class="cmd command">sudo ufw enable</span>
  125. </pre></div>
  126. </li>
  127. <li class="list itemizedlist">
  128. <p class="para">
  129. To open a port (SSH in this example):
  130. </p>
  131. <div class="screen"><pre class="contents "><span class="cmd command">sudo ufw allow 22</span>
  132. </pre></div>
  133. </li>
  134. <li class="list itemizedlist">
  135. <p class="para">
  136. Rules can also be added using a <span class="em emphasis">numbered</span> format:
  137. </p>
  138. <div class="screen"><pre class="contents "><span class="cmd command">sudo ufw insert 1 allow 80</span>
  139. </pre></div>
  140. </li>
  141. <li class="list itemizedlist">
  142. <p class="para">
  143. Similarly, to close an opened port:
  144. </p>
  145. <div class="screen"><pre class="contents "><span class="cmd command">sudo ufw deny 22</span>
  146. </pre></div>
  147. </li>
  148. <li class="list itemizedlist">
  149. <p class="para">
  150. To remove a rule, use delete followed by the rule:
  151. </p>
  152. <div class="screen"><pre class="contents "><span class="cmd command">sudo ufw delete deny 22</span>
  153. </pre></div>
  154. </li>
  155. <li class="list itemizedlist">
  156. <p class="para">
  157. It is also possible to allow access from specific hosts or networks to a port. The following example allows SSH access
  158. from host 192.168.0.2 to any IP address on this host:
  159. </p>
  160. <div class="screen"><pre class="contents "><span class="cmd command">sudo ufw allow proto tcp from 192.168.0.2 to any port 22</span>
  161. </pre></div>
  162. <p class="para">
  163. Replace 192.168.0.2 with 192.168.0.0/24 to allow SSH access from the entire subnet.
  164. </p>
  165. </li>
  166. <li class="list itemizedlist">
  167. <p class="para">
  168. Adding the <span class="em emphasis">--dry-run</span> option to a <span class="em emphasis">ufw</span> command will output the resulting
  169. rules, but not apply them. For example, the following is what would be applied if opening the HTTP port:
  170. </p>
  171. <div class="screen"><pre class="contents "><span class="cmd command"> sudo ufw --dry-run allow http</span>
  172. </pre></div>
  173. <div class="screen"><pre class="contents "><span class="output computeroutput">*filter
  174. :ufw-user-input - [0:0]
  175. :ufw-user-output - [0:0]
  176. :ufw-user-forward - [0:0]
  177. :ufw-user-limit - [0:0]
  178. :ufw-user-limit-accept - [0:0]
  179. ### RULES ###
  180. ### tuple ### allow tcp 80 0.0.0.0/0 any 0.0.0.0/0
  181. -A ufw-user-input -p tcp --dport 80 -j ACCEPT
  182. ### END RULES ###
  183. -A ufw-user-input -j RETURN
  184. -A ufw-user-output -j RETURN
  185. -A ufw-user-forward -j RETURN
  186. -A ufw-user-limit -m limit --limit 3/minute -j LOG --log-prefix "[UFW LIMIT]: "
  187. -A ufw-user-limit -j REJECT
  188. -A ufw-user-limit-accept -j ACCEPT
  189. COMMIT
  190. Rules updated</span>
  191. </pre></div>
  192. </li>
  193. <li class="list itemizedlist">
  194. <p class="para">
  195. <span class="app application">ufw</span> can be disabled by:
  196. </p>
  197. <div class="screen"><pre class="contents "><span class="cmd command">sudo ufw disable</span>
  198. </pre></div>
  199. </li>
  200. <li class="list itemizedlist">
  201. <p class="para">
  202. To see the firewall status, enter:
  203. </p>
  204. <div class="screen"><pre class="contents "><span class="cmd command">sudo ufw status</span>
  205. </pre></div>
  206. </li>
  207. <li class="list itemizedlist">
  208. <p class="para">
  209. And for more verbose status information use:
  210. </p>
  211. <div class="screen"><pre class="contents "><span class="cmd command">sudo ufw status verbose</span>
  212. </pre></div>
  213. </li>
  214. <li class="list itemizedlist">
  215. <p class="para">
  216. To view the <span class="em emphasis">numbered</span> format:
  217. </p>
  218. <div class="screen"><pre class="contents "><span class="cmd command">sudo ufw status numbered</span>
  219. </pre></div>
  220. </li>
  221. </ul></div>
  222. <div class="note" title="Note"><div class="inner"><div class="region"><div class="contents">
  223. <p class="para">
  224. If the port you want to open or close is defined in <span class="file filename">/etc/services</span>, you can use the port name instead of the number.
  225. In the above examples, replace <span class="em emphasis">22</span> with <span class="em emphasis">ssh</span>.
  226. </p>
  227. </div></div></div></div>
  228. <p class="para">
  229. This is a quick introduction to using <span class="app application">ufw</span>. Please refer to the <span class="app application">ufw</span> man page for
  230. more information.
  231. </p>
  232. </div>
  233. <div class="sect3 sect" id="ufw-application-integration"><div class="inner">
  234. <div class="hgroup"><h3 class="title">ufw Application Integration</h3></div>
  235. <div class="region"><div class="contents">
  236. <p class="para">
  237. Applications that open ports can include an <span class="app application">ufw</span> profile, which details the ports needed for the
  238. application to function properly. The profiles are kept in <span class="file filename">/etc/ufw/applications.d</span>,
  239. and can be edited if the default ports have been changed.
  240. </p>
  241. <div class="list itemizedlist"><ul class="list itemizedlist">
  242. <li class="list itemizedlist">
  243. <p class="para">
  244. To view which applications have installed a profile, enter the following in a terminal:
  245. </p>
  246. <div class="screen"><pre class="contents "><span class="cmd command">sudo ufw app list</span>
  247. </pre></div>
  248. </li>
  249. <li class="list itemizedlist">
  250. <p class="para">
  251. Similar to allowing traffic to a port, using an application profile is accomplished by entering:
  252. </p>
  253. <div class="screen"><pre class="contents "><span class="cmd command">sudo ufw allow Samba</span>
  254. </pre></div>
  255. </li>
  256. <li class="list itemizedlist">
  257. <p class="para">
  258. An extended syntax is available as well:
  259. </p>
  260. <div class="screen"><pre class="contents "><span class="cmd command">ufw allow from 192.168.0.0/24 to any app Samba</span>
  261. </pre></div>
  262. <p class="para">
  263. Replace <span class="em emphasis">Samba</span> and <span class="em emphasis">192.168.0.0/24</span> with the application profile you are
  264. using and the IP range for your network.
  265. </p>
  266. <div class="note" title="Note"><div class="inner"><div class="region"><div class="contents">
  267. <p class="para">
  268. There is no need to specify the <span class="em emphasis">protocol</span> for the application, because that information is detailed in
  269. the profile. Also, note that the <span class="em emphasis">app</span> name replaces the <span class="em emphasis">port</span> number.
  270. </p>
  271. </div></div></div></div>
  272. </li>
  273. <li class="list itemizedlist">
  274. <p class="para">
  275. To view details about which ports, protocols, etc., are defined for an application, enter:
  276. </p>
  277. <div class="screen"><pre class="contents "><span class="cmd command">sudo ufw app info Samba</span>
  278. </pre></div>
  279. </li>
  280. </ul></div>
  281. <p class="para">
  282. Not all applications that require opening a network port come with <span class="app application">ufw</span> profiles, but if
  283. you have profiled an application and want the file to be included with the package, please file a bug against the
  284. package in Launchpad.
  285. </p>
  286. <div class="screen"><pre class="contents "><span class="cmd command">ubuntu-bug nameofpackage</span>
  287. </pre></div>
  288. </div></div>
  289. </div></div>
  290. </div>
  291. </div></div>
  292. <div class="sect2 sect" id="ip-masquerading"><div class="inner">
  293. <div class="hgroup"><h2 class="title">IP Masquerading</h2></div>
  294. <div class="region">
  295. <div class="contents"><p class="para">
  296. The purpose of IP Masquerading is to allow machines with private, non-routable IP
  297. addresses on your network to access the Internet through the machine doing the
  298. masquerading. Traffic from your private network destined for the Internet must be
  299. manipulated for replies to be routable back to the machine that made the request.
  300. To do this, the kernel must modify the <span class="em emphasis">source</span>
  301. IP address of each packet so that replies will be routed back to it, rather than
  302. to the private IP address that made the request, which is impossible over the
  303. Internet. Linux uses <span class="em emphasis">Connection Tracking</span>
  304. (conntrack) to keep track of which connections belong to which machines and reroute
  305. each return packet accordingly. Traffic leaving your private network is thus
  306. "masqueraded" as having originated from your Ubuntu gateway machine.
  307. This process is referred to in Microsoft documentation as Internet
  308. Connection Sharing.
  309. </p></div>
  310. <div class="sect3 sect" id="ip-masquerade-ufw"><div class="inner">
  311. <div class="hgroup"><h3 class="title">ufw Masquerading</h3></div>
  312. <div class="region"><div class="contents">
  313. <p class="para">
  314. IP Masquerading can be achieved using custom <span class="app application">ufw</span> rules. This is possible because the current
  315. back-end for <span class="app application">ufw</span> is <span class="app application">iptables-restore</span> with the rules files located in
  316. <span class="file filename">/etc/ufw/*.rules</span>. These files are a great place to add legacy iptables rules used
  317. without <span class="app application">ufw</span>, and rules that are more network gateway or bridge related.
  318. </p>
  319. <p class="para">
  320. The rules are split into two different files, rules that should be executed before
  321. <span class="app application">ufw</span> command line rules, and rules that are executed after <span class="app application">ufw</span> command line rules.
  322. </p>
  323. <div class="list itemizedlist"><ul class="list itemizedlist">
  324. <li class="list itemizedlist">
  325. <p class="para">
  326. First, packet forwarding needs to be enabled in <span class="app application">ufw</span>. Two configuration files will need to be adjusted, in
  327. <span class="file filename">/etc/default/ufw</span> change the <span class="em emphasis">DEFAULT_FORWARD_POLICY</span> to <span class="quote">“ACCEPT”</span>:
  328. </p>
  329. <div class="code"><pre class="contents ">DEFAULT_FORWARD_POLICY="ACCEPT"
  330. </pre></div>
  331. <p class="para">
  332. Then edit <span class="file filename">/etc/ufw/sysctl.conf</span> and uncomment:
  333. </p>
  334. <div class="code"><pre class="contents ">net/ipv4/ip_forward=1
  335. </pre></div>
  336. <p class="para">
  337. Similarly, for IPv6 forwarding uncomment:
  338. </p>
  339. <div class="code"><pre class="contents ">net/ipv6/conf/default/forwarding=1
  340. </pre></div>
  341. </li>
  342. <li class="list itemizedlist">
  343. <p class="para">
  344. Now add rules to the <span class="file filename">/etc/ufw/before.rules</span> file. The default rules only configure the <span class="em emphasis">filter</span>
  345. table, and to enable masquerading the <span class="em emphasis">nat</span> table will need to be configured. Add the following to the top of the file
  346. just after the header comments:
  347. </p>
  348. <div class="code"><pre class="contents "># nat Table rules
  349. *nat
  350. :POSTROUTING ACCEPT [0:0]
  351. # Forward traffic from eth1 through eth0.
  352. -A POSTROUTING -s 192.168.0.0/24 -o eth0 -j MASQUERADE
  353. # don't delete the 'COMMIT' line or these nat table rules won't be processed
  354. COMMIT
  355. </pre></div>
  356. <p class="para">
  357. The comments are not strictly necessary, but it is considered good practice to document your configuration. Also, when modifying
  358. any of the <span class="em emphasis">rules</span> files in <span class="file filename">/etc/ufw</span>, make sure these lines are the last
  359. line for each table modified:
  360. </p>
  361. <div class="code"><pre class="contents "># don't delete the 'COMMIT' line or these rules won't be processed
  362. COMMIT
  363. </pre></div>
  364. <p class="para">
  365. For each <span class="em emphasis">Table</span> a corresponding <span class="em emphasis">COMMIT</span> statement is required. In these examples
  366. only the <span class="em emphasis">nat</span> and <span class="em emphasis">filter</span> tables are shown, but you can also add rules for the
  367. <span class="em emphasis">raw</span> and <span class="em emphasis">mangle</span> tables.
  368. </p>
  369. <div class="note" title="Note"><div class="inner"><div class="region"><div class="contents">
  370. <p class="para">
  371. In the above example replace <span class="em emphasis">eth0</span>, <span class="em emphasis">eth1</span>, and <span class="em emphasis">192.168.0.0/24</span>
  372. with the appropriate interfaces and IP range for your network.
  373. </p>
  374. </div></div></div></div>
  375. </li>
  376. <li class="list itemizedlist">
  377. <p class="para">
  378. Finally, disable and re-enable <span class="app application">ufw</span> to apply the changes:
  379. </p>
  380. <div class="code"><pre class="contents "><span class="cmd command">sudo ufw disable &amp;&amp; sudo ufw enable</span>
  381. </pre></div>
  382. </li>
  383. </ul></div>
  384. <p class="para">
  385. IP Masquerading should now be enabled. You can also add any additional FORWARD rules
  386. to the <span class="file filename">/etc/ufw/before.rules</span>. It is recommended that these additional
  387. rules be added to the <span class="em emphasis">ufw-before-forward</span> chain.
  388. </p>
  389. </div></div>
  390. </div></div>
  391. <div class="sect3 sect" id="ip-masquerading-iptables"><div class="inner">
  392. <div class="hgroup"><h3 class="title">iptables Masquerading</h3></div>
  393. <div class="region"><div class="contents">
  394. <p class="para">
  395. <span class="app application">iptables</span> can also be used to enable Masquerading.
  396. </p>
  397. <div class="list itemizedlist"><ul class="list itemizedlist">
  398. <li class="list itemizedlist">
  399. <p class="para">
  400. Similar to <span class="app application">ufw</span>, the first step is to enable IPv4 packet forwarding by editing
  401. <span class="file filename">/etc/sysctl.conf</span> and uncomment the following line:
  402. </p>
  403. <div class="code"><pre class="contents ">net.ipv4.ip_forward=1
  404. </pre></div>
  405. <p class="para">
  406. If you wish to enable IPv6 forwarding also uncomment:
  407. </p>
  408. <div class="code"><pre class="contents ">net.ipv6.conf.default.forwarding=1
  409. </pre></div>
  410. </li>
  411. <li class="list itemizedlist">
  412. <p class="para">
  413. Next, execute the <span class="app application">sysctl</span> command to enable the new settings in the configuration file:
  414. </p>
  415. <div class="screen"><pre class="contents "><span class="cmd command">sudo sysctl -p</span>
  416. </pre></div>
  417. </li>
  418. <li class="list itemizedlist">
  419. <p class="para">
  420. IP Masquerading can now be accomplished with a single iptables rule, which may differ slightly based on your network configuration:
  421. </p>
  422. <div class="screen"><pre class="contents ">sudo iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -o ppp0 -j MASQUERADE
  423. </pre></div>
  424. <p class="para">
  425. The above command assumes that your private address space is 192.168.0.0/16 and
  426. that your Internet-facing device is ppp0. The syntax is broken down as follows:
  427. </p>
  428. <div class="list itemizedlist"><ul class="list itemizedlist">
  429. <li class="list itemizedlist"><p class="para"> -t nat -- the rule is to go into the nat table</p></li>
  430. <li class="list itemizedlist"><p class="para"> -A POSTROUTING -- the rule is to be appended (-A) to the POSTROUTING chain</p></li>
  431. <li class="list itemizedlist"><p class="para"> -s 192.168.0.0/16 -- the rule applies to traffic originating from the specified address space</p></li>
  432. <li class="list itemizedlist"><p class="para"> -o ppp0 -- the rule applies to traffic scheduled to be routed through the specified network device</p></li>
  433. <li class="list itemizedlist">
  434. <p class="para">
  435. -j MASQUERADE -- traffic matching this rule is to "jump"
  436. (-j) to the MASQUERADE target to be manipulated as described above
  437. </p>
  438. </li>
  439. </ul></div>
  440. </li>
  441. <li class="list itemizedlist">
  442. <p class="para">
  443. Also, each chain in the filter table (the default table, and where most or all packet
  444. filtering occurs) has a default <span class="em emphasis">policy</span> of
  445. ACCEPT, but if you are creating a firewall in addition to a gateway device, you
  446. may have set the policies to DROP or REJECT, in which case your masqueraded
  447. traffic needs to be allowed through the FORWARD chain for the above rule to work:
  448. </p>
  449. <div class="screen"><pre class="contents ">sudo iptables -A FORWARD -s 192.168.0.0/16 -o ppp0 -j ACCEPT
  450. sudo iptables -A FORWARD -d 192.168.0.0/16 -m state \
  451. --state ESTABLISHED,RELATED -i ppp0 -j ACCEPT
  452. </pre></div>
  453. <p class="para">
  454. The above commands will allow all connections from your local network to the
  455. Internet and all traffic related to those connections to return to the machine
  456. that initiated them.
  457. </p>
  458. </li>
  459. <li class="list itemizedlist">
  460. <p class="para">
  461. If you want masquerading to be enabled on reboot, which you probably do, edit <span class="file filename">/etc/rc.local</span> and add any
  462. commands used above. For example add the first command with no filtering:
  463. </p>
  464. <div class="screen"><pre class="contents ">iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -o ppp0 -j MASQUERADE
  465. </pre></div>
  466. </li>
  467. </ul></div>
  468. </div></div>
  469. </div></div>
  470. </div>
  471. </div></div>
  472. <div class="sect2 sect" id="firewall-logs"><div class="inner">
  473. <div class="hgroup"><h2 class="title">Logs</h2></div>
  474. <div class="region"><div class="contents">
  475. <p class="para">
  476. Firewall logs are essential for recognizing attacks, troubleshooting your
  477. firewall rules, and noticing unusual activity on your network. You must include
  478. logging rules in your firewall for them to be generated, though, and logging
  479. rules must come before any applicable terminating rule (a rule with a target
  480. that decides the fate of the packet, such as ACCEPT, DROP, or REJECT).
  481. </p>
  482. <p class="para">
  483. If you are using <span class="app application">ufw</span>, you can turn on logging by entering the following in a terminal:
  484. </p>
  485. <div class="screen"><pre class="contents "><span class="cmd command">sudo ufw logging on</span>
  486. </pre></div>
  487. <p class="para">
  488. To turn logging off in <span class="app application">ufw</span>, simply replace <span class="em emphasis">on</span> with <span class="em emphasis">off</span> in the above command.
  489. </p>
  490. <p class="para">
  491. If using <span class="app application">iptables</span> instead of <span class="app application">ufw</span>, enter:
  492. </p>
  493. <div class="screen"><pre class="contents ">sudo iptables -A INPUT -m state --state NEW -p tcp --dport 80 \
  494. -j LOG --log-prefix "NEW_HTTP_CONN: "
  495. </pre></div>
  496. <p class="para">
  497. A request on port 80 from the local machine, then, would generate a log in dmesg
  498. that looks like this (single line split into 3 to fit this document):
  499. </p>
  500. <div class="code"><pre class="contents ">[4304885.870000] NEW_HTTP_CONN: IN=lo OUT= MAC=00:00:00:00:00:00:00:00:00:00:00:00:08:00
  501. SRC=127.0.0.1 DST=127.0.0.1 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=58288 DF PROTO=TCP
  502. SPT=53981 DPT=80 WINDOW=32767 RES=0x00 SYN URGP=0
  503. </pre></div>
  504. <p class="para">
  505. The above log will also appear in <span class="file filename">/var/log/messages</span>,
  506. <span class="file filename">/var/log/syslog</span>, and <span class="file filename">/var/log/kern.log</span>.
  507. This behavior can be modified by editing <span class="file filename">/etc/syslog.conf</span>
  508. appropriately or by installing and configuring <span class="app application">ulogd</span>
  509. and using the ULOG target instead of LOG. The <span class="app application">ulogd</span>
  510. daemon is a userspace server that listens for logging instructions from the kernel
  511. specifically for firewalls, and can log to any file you like, or even to a
  512. <span class="app application">PostgreSQL</span> or <span class="app application">MySQL</span>
  513. database. Making sense of your firewall logs can be simplified by using a log
  514. analyzing tool such as <span class="app application">logwatch</span>, <span class="app application">fwanalog</span>,
  515. <span class="app application">fwlogwatch</span>, or <span class="app application">lire</span>.
  516. </p>
  517. </div></div>
  518. </div></div>
  519. <div class="sect2 sect" id="other-firewall-tools"><div class="inner">
  520. <div class="hgroup"><h2 class="title">Other Tools</h2></div>
  521. <div class="region"><div class="contents">
  522. <p class="para">
  523. There are many tools available to help you construct a complete firewall without
  524. intimate knowledge of iptables. A command-line tool with plain-text configuration files:
  525. </p>
  526. <div class="list itemizedlist"><ul class="list itemizedlist"><li class="list itemizedlist">
  527. <p class="para">
  528. <a href="http://www.shorewall.net/" class="ulink" title="http://www.shorewall.net/">Shorewall</a> is a very powerful solution to help you
  529. configure an advanced firewall for any network.
  530. </p>
  531. </li></ul></div>
  532. </div></div>
  533. </div></div>
  534. <div class="sect2 sect" id="firewall-references"><div class="inner">
  535. <div class="hgroup"><h2 class="title">References</h2></div>
  536. <div class="region"><div class="contents"><div class="list itemizedlist"><ul class="list itemizedlist">
  537. <li class="list itemizedlist">
  538. <p class="para">
  539. The <a href="https://wiki.ubuntu.com/UncomplicatedFirewall" class="ulink" title="https://wiki.ubuntu.com/UncomplicatedFirewall">Ubuntu Firewall</a> wiki page contains information on the development
  540. of <span class="app application">ufw</span>.
  541. </p>
  542. </li>
  543. <li class="list itemizedlist">
  544. <p class="para">
  545. Also, the <span class="app application">ufw</span> manual page contains some very useful information: <span class="cmd command">man ufw</span>.
  546. </p>
  547. </li>
  548. <li class="list itemizedlist">
  549. <p class="para">
  550. See the <a href="http://www.netfilter.org/documentation/HOWTO/packet-filtering-HOWTO.html" class="ulink" title="http://www.netfilter.org/documentation/HOWTO/packet-filtering-HOWTO.html">packet-filtering-HOWTO</a>
  551. for more information on using <span class="app application">iptables</span>.
  552. </p>
  553. </li>
  554. <li class="list itemizedlist">
  555. <p class="para">
  556. The <a href="http://www.netfilter.org/documentation/HOWTO/NAT-HOWTO.html" class="ulink" title="http://www.netfilter.org/documentation/HOWTO/NAT-HOWTO.html">nat-HOWTO</a> contains further details on
  557. masquerading.
  558. </p>
  559. </li>
  560. <li class="list itemizedlist">
  561. <p class="para">
  562. The <a href="https://help.ubuntu.com/community/IptablesHowTo" class="ulink" title="https://help.ubuntu.com/community/IptablesHowTo">IPTables HowTo</a> in the Ubuntu wiki is a great resource.
  563. </p>
  564. </li>
  565. </ul></div></div></div>
  566. </div></div>
  567. </div>
  568. <div class="links nextlinks">
  569. <a class="nextlinks-prev" href="console-security.html.en" title="Console Security">Previous</a><a class="nextlinks-next" href="apparmor.html.en" title="AppArmor">Next</a>
  570. </div>
  571. <div class="clear"></div>
  572. </div>
  573. <div id="pagebottom"></div>
  574. </div></div>
  575. </div>
  576. <div id="footer">
  577. <p style="padding-bottom: 0.4em">You can choose the <b>displayed language</b> by adding a language suffix to the web address
  578. so it ends with e.g. <tt>.html.en</tt> or <tt>.html.de</tt>.<br>
  579. If the web address has no language suffix, the preferred language specified in your web browser's settings is used. For your convenience:<br>
  580. [ <a title="English page version" href="#" onClick="englishPageVersion();">Change to English Language</a> |
  581. <a title="Language selected by browser" href="#" onClick="browserPreferredLanguage()">Change to Browser's Preferred Language</a> ]</p>
  582. <p>The material in this document is available under a free license, see <a href="../../legal.html">Legal</a> for details.<br>
  583. For information on contributing see the <a href="https://wiki.ubuntu.com/DocumentationTeam">Ubuntu Documentation Team wiki page</a>.
  584. To report errors in this serverguide documentation, <a href="https://bugs.launchpad.net/serverguide">file a bug report</a>.</p>
  585. </div>
  586. </div>
  587. </body>
  588. </html>