iptables Demystified - Port Redirection and Forwarding HTTP Traffic to another machine (part 1)

Sdílet
Vložit
  • čas přidán 6. 07. 2024
  • The Linux kernel’s network packet processing subsystem is called Netfilter, and iptables is the command used to configure it. In this video I’ll demonstrate iptables and show you practical examples on how to do port forwarding, IP forwarding, and even load balancing hopefully in upcoming videos. This is part 1.
    Slides
    payhip.com/b/a7lP1
    (Members get all slides for free. Become a Member to support the channel)
    / @hnasr
    0:00 iptables components
    4:24 Port Redirect in the same machine
    10:54 Example
    13:37 IP Forwarding to another machine (DNAT, SNAT)
    22:00 Example
    32:00 IP Forward Enable
    commands used in this video
    github.com/hnasr/javascript_p...
    🎙️Listen to the Backend Engineering Podcast
    husseinnasser.com/podcast
    🏭 Backend Engineering Videos
    backend.husseinnasser.com
    💾 Database Engineering Videos
    • Database Engineering
    🏰 Load Balancing and Proxies Videos
    • Proxies
    🏛️ Software Archtiecture Videos
    • Software Architecture
    📩 Messaging Systems
    • Message Queues & PubSu...
    Become a Member
    / @hnasr
    Support me on PayPal
    bit.ly/33ENps4
    Join our Thriving Backend Community on Discord
    / discord
    Stay Awesome,
    Hussein
  • Věda a technologie

Komentáře • 132

  • @zedzpan
    @zedzpan Před 3 lety +21

    Iptables have always been a mystery and rather complicated. Thanks for demystifying it. Thank you for all your great content delivered with such enthusiasm!

    • @hnasr
      @hnasr  Před 3 lety +5

      Glad it helped ❤️

  • @russohc
    @russohc Před 2 měsíci

    Hussein! I've been trying to understand iptables for more than three months and you explained everything in less than 35 minutes in a super happy way!!! We want more hahaha, thanks for the video!

  • @DerrickHayden
    @DerrickHayden Před rokem +1

    I've search all over the net for exactly this. Hours of searching and you're the only one covering iptables in this manner. Thank you.

  • @matches0423
    @matches0423 Před 13 dny

    Thanks for the video!! Clears a lot of things up for me. The bottleneck of using socat disappeared after I switched to iptables.

  • @Demodude123
    @Demodude123 Před 3 lety +3

    Thanks Hussein! This is a great tutorial! I work with docker/kubernetes and I never understood their complex iptable chains. I'll definitely keep watching this series to understand how container networks work

  • @90hijacked
    @90hijacked Před 2 lety

    you're a good presenter, down to earth and seem genuinely enthusiastic. cheers :)
    It's always a pleasure seeing people learning and/or hacking about with the same topics as you

  • @m.m.4589
    @m.m.4589 Před 3 lety +4

    Wow man, I thought ip tables was complicated, but u nailed it. Great examples. Thanks for your hard work

  • @Z3rgatul
    @Z3rgatul Před měsícem

    This is the best format I could imagine to explain iptables. Many thanks

  • @LuisReyes-mw3jo
    @LuisReyes-mw3jo Před 2 lety +2

    Dude, AMAZING explination. Thank you so much. I've spent hours looking for the information like this. And you made it so easy to comprehend! THANK YOU SO MUCH. Keep the awesome work!!

  • @mishasawangwan6652
    @mishasawangwan6652 Před 3 lety +8

    beauuuutiful root permissions. beautiful. BEAUTIFUL.

  • @pratikdaigavane1111
    @pratikdaigavane1111 Před 3 lety +13

    sudo iptables -A OUTPUT -m statistic --mode random -p udp --probability 0.77 -j DROP
    Useful in an online class when professor asks you a question and you don't know the answer 😜

    • @hnasr
      @hnasr  Před 3 lety +7

      Evil 🦹‍♀️
      Will explain this in next episode! This is the filter table and Pratik here is dropping 77% of outgoing udp packets. So it forces video call to lag.

    • @pratikdaigavane1111
      @pratikdaigavane1111 Před 3 lety

      @@hnasr Can wait to watch next episode!!

  • @rightangleoverseas2391
    @rightangleoverseas2391 Před 3 lety +1

    This is pure value ! Thank you so much for this !

  • @totti343
    @totti343 Před rokem +3

    Love the storytelling. Great video!

  • @TaniaTebaldi
    @TaniaTebaldi Před 2 lety

    Wow after hours searching iptables information, I am here, you're amazing teacher, thanks a lot! I'm working with mitmproxy in transparent mode for testing iot devices, tomorrow I will try to make my iptables. Greetings from Italy!

  • @stolenidentity1152
    @stolenidentity1152 Před rokem

    Perfect tone of voice. Funny and accurate. Kudos to you!

  • @yetanother7754
    @yetanother7754 Před 3 lety +2

    My Friend ... Amazing explanation .. Loved it! gonna recommend it to my friends

  • @fujinafiul6044
    @fujinafiul6044 Před rokem

    Oh man, you are the life saver.. very few dare to cover such boring topics with such excitement.. love you man ❤ you just saved me hours of work...❤

  • @OfferoC
    @OfferoC Před 3 lety +1

    Very nice. Thank you. Keep these videos coming!

  • @EddyCaffrey
    @EddyCaffrey Před 3 lety +2

    Iptables is great 👍🏾. Good explanation. Thanks 🙏🏾

  • @saad.83
    @saad.83 Před 3 lety +2

    Very interesting to watch!

  • @steamrangercomputing
    @steamrangercomputing Před 10 měsíci

    Genuinely informative video. Great job!

  • @martinfurstenberg2281
    @martinfurstenberg2281 Před 5 měsíci

    Thanks for the explanation. Awesome Job.👍

  • @dacoup5955
    @dacoup5955 Před 8 měsíci

    Thank you so much, i'm going through a Kali tutorial for pen testing studies to eventually pass my OSCP, i love your delivery and an enthusiasm, much of this was glossed over because its a vast aspect of linux's opensource firewall and you have a clear and concise way of boiling it all down so i can wrap my head around its many functions and uses, currently working on port forwarding 80 HTTP traffic to sslstripper on port 8080 and i was curious what that process actually looks like behind the curtains ... Appreciate the feedback and i just subscribed.

  • @leo-phiponacci
    @leo-phiponacci Před rokem +1

    It is really demystified like he said, thank you very much Hussein

  • @Kifter1983
    @Kifter1983 Před rokem

    Fantastic tutorial. I loved that you mentioned you wasted hours because of ip forwarding as exactly the same happened to me being a newb to ip tables ha!

  • @yashas8238
    @yashas8238 Před 6 měsíci

    I liked how you stressed the important things, thks you saved me a lot of time!

  • @lajoskicsi6910
    @lajoskicsi6910 Před 2 lety

    Aweesome! Love this IP table course. I want to see more!

  • @phoenixray3512
    @phoenixray3512 Před 3 lety

    Very informative thank you for sharing, I will be waiting for more.

  • @ChristianAltamiranoAyala
    @ChristianAltamiranoAyala Před 3 lety +2

    Such a insightful video, congrats Hussain

  • @pging8328
    @pging8328 Před 8 měsíci

    hey mate, i appreciate your videos very much which you put on the internet.

  • @mohamedhabas7391
    @mohamedhabas7391 Před 3 lety +6

    Yaaay finally Linux admin stuff 😍😍😍😍

  • @jlxip
    @jlxip Před 2 lety

    This video is absolutely outstanding. Thank you

  • @yassirhassan2063
    @yassirhassan2063 Před 3 lety +2

    Great content, great person. proud of you man.

  • @ivanramadhan5185
    @ivanramadhan5185 Před 3 lety

    Thanks for the explanation, it's very helpful.

  • @islem1263
    @islem1263 Před 3 lety +5

    If you press Ctrl+F5 you can force the browser to serve you a new non cached version

  • @mathisart
    @mathisart Před 2 lety

    Finally someone who understands this and explains it in a way that's understandable! Jesus bless you

  • @robertphillips124714
    @robertphillips124714 Před 2 lety

    Great explanation. Thanks very much!!

  • @totopops98
    @totopops98 Před rokem

    nice . very clear way to explain !

  • @shashidharnagraj896
    @shashidharnagraj896 Před rokem

    Thank you very much, this video has helped me learn a lot.

  • @rahulcsaple
    @rahulcsaple Před 2 lety

    Amazing explanation.

  • @AshutoshKumar-ue3dr
    @AshutoshKumar-ue3dr Před 3 lety +2

    Bro, you are awesome.

  • @mdshehab8947
    @mdshehab8947 Před 6 měsíci

    Absolutely amazing

  • @root1101
    @root1101 Před 7 měsíci

    Thanks, that was rather useful. The lack of use cases in reference materials is aggravating.

  • @pging8328
    @pging8328 Před 8 měsíci

    hey man you saved me on the tip to to enable ip forwarding. I was literally about to smash my head against the computer

  • @adamstrejcovsky8257
    @adamstrejcovsky8257 Před 9 měsíci

    could not have been easier. thanks a lot

  • @osamaa.h.altameemi5592
    @osamaa.h.altameemi5592 Před 3 lety +4

    that was really smooth, thx a ton. Hussein are you aware of any implementations that do the processing thing on the NIC itself (talking about TOE NICs)?

  • @bun4bun
    @bun4bun Před 2 lety

    Just sent a donation on paypal for your efforts. Thanks a lot for the video.
    You saved me a lot of trouble :)

  • @anisht1838
    @anisht1838 Před rokem

    Great work, great value, thank you so much.

    • @hnasr
      @hnasr  Před rokem

      Appreciate it!

  • @usamatahseenulhaque9125
    @usamatahseenulhaque9125 Před 3 lety +2

    Great video

  • @kaustubhmane8287
    @kaustubhmane8287 Před 3 lety +2

    way too cool video !!

  • @romans4471
    @romans4471 Před 2 lety

    Thank You very much! And it was a great idea to lowercase everything ( 07:00 ) o it became more meaningful.

  • @bharadwaz7
    @bharadwaz7 Před 6 měsíci

    one of the best video thanks a ton

  • @conscium
    @conscium Před rokem

    great video, helped me a lot and it was also entertaining
    peace

  • @synchronizingsynchronize3066

    creative explanation!

  • @alkklajslkdajlk
    @alkklajslkdajlk Před rokem

    Thank you for your video :)

  • @azul2011ish
    @azul2011ish Před 3 lety

    Awesome, I love linux!

  • @bobsmithy3103
    @bobsmithy3103 Před 5 měsíci

    informative and funny, good stuff

  • @francisobwogo6366
    @francisobwogo6366 Před 2 lety

    Thanks. This works.

  • @mohammedaris2338
    @mohammedaris2338 Před 3 lety +2

    Thank you @Hussein , i appreciate your video

  • @mnj1
    @mnj1 Před 3 lety

    Hi. In the last example, I wonder, would it also work if you defined both rules in the PREROUTING chain (or both in the POSTROUTING chain)? Why (not?)?

  • @johnmark2014
    @johnmark2014 Před 3 lety

    THANK YOUUUUU SOOO MUCHH!!!!

  • @misterg4548
    @misterg4548 Před 3 lety +2

    Informative video. But there is also another approach to start an app and let is use the restricted ports without the root permission. The tool is called authbind.

  • @okekeebube1426
    @okekeebube1426 Před 2 lety

    Thanks for helping your viewers avoid hours of reading in just one video

  • @kaczuszka-dt
    @kaczuszka-dt Před 2 lety

    damn son, I love your content.

  • @ehsanshadi810
    @ehsanshadi810 Před rokem

    Thanks bro

  • @jakke1975
    @jakke1975 Před rokem

    So if you have a rule for both tcp and udp (e.g. for a dns server as target), can you do that command in 1 line or do you have to create a separate rule for it?

  • @mohamedhabas7391
    @mohamedhabas7391 Před 3 lety +2

    for ip forwarding
    #this injects the value to the kernel regardless of the destro
    echo 1 > /proc/sys/net/ipv4/ip_forward

  • @thierryalbareda3860
    @thierryalbareda3860 Před 2 lety

    thanks, usefull.

  • @uwontlikeit
    @uwontlikeit Před 5 měsíci

    The best visualization of the NAT flow I saw so far, the only thing left a bit unclear is the way back, when server responds - how does it do backwards translation?

  • @hsjsjssnnsjsjs1666
    @hsjsjssnnsjsjs1666 Před 2 lety

    Holy fuck I enjoyed watching this😂😂❤️

  • @syedtahaaziz2668
    @syedtahaaziz2668 Před rokem +1

    Please also explain ip masquerade as well.

  • @PauloGervilla
    @PauloGervilla Před 2 lety

    Just saved my life, thanks! Do I need to do something else to make this configuration persistent?

  • @kallingal1662
    @kallingal1662 Před dnem

    Super

  • @bayliner4387
    @bayliner4387 Před 3 lety

    this is a great help however I'm still quite confused. I'm trying to get my RPi to RPi WireGuard tunnel to allow Access Point traffic on the Client RPi to pass to the internet on the RPi server. Is this possible? I'm assuming I only need to modify the RPi Client ipTables? The Rpi AP Clients are on 10.10.10.X and the WireGuard tunnel is 10.6.0.2 (Client) to10.6.0.1 (server).

  • @akashagarwal6390
    @akashagarwal6390 Před 6 měsíci

    1. is 8080 an ephemeral port from server's side/PoV?
    2. Also, does it make the diff between an app server running on 8080 while the web server exposed on 80?
    3. is this port redirect the same as port forwarding?

  • @akashagarwal6390
    @akashagarwal6390 Před 6 měsíci

    why do we need this exactly? what are its actual use cases in real-world as compared to delegating a request explicitly by some server to another?

  • @autohmae
    @autohmae Před 3 lety +4

    30:53 if you hate it, use the --numeric ( -n ) when listing my dear friend :-)
    PS You forgot to mention what to do after changing the config file.

    • @hnasr
      @hnasr  Před 3 lety

      😍 nice tip that should be the default though :p

    • @autohmae
      @autohmae Před 3 lety +1

      @@hnasr I don't know why, but this is very common for lots of commands on Linux, Unix (thus Mac) and also Winows. -n is not the default for any of them. ping, traceroute, netstat, tcpdump, etc.

    • @hnasr
      @hnasr  Před 3 lety +2

      What is tcpdump? ;) :p

    • @autohmae
      @autohmae Před 3 lety +1

      @@hnasr I feel some day you might do videos on tcpdump as well

  • @ebu7
    @ebu7 Před rokem

    Assalamualaikum
    Great channel for programming. Thanks a lot of.
    I want to you how you setup your raspberry pi. Plzzz tell me???

  • @ShueFig
    @ShueFig Před 3 lety +2

    In the example for DNAT & SNAT, in the response from the computer at 192.168.1.3 back to the server, how does the server determine that this response is meant for the computer at 192.168.1.2? I'm assuming the response packet will be something like (192.168.1.3:80 | 10.0.0.2:1234) (sIP | dIP)?

    • @brod515
      @brod515 Před 3 lety

      It must be using the created ip tables. when the response (192.168.1.3.:80 | 10.0.0.2:1234) is received the server should remember (lookup) that it mapped dIP from 10.0.0.2 => 192.168.1.3 and changed the mapping from sIP 192.168.1.2 to come back to 10.0.0.2. so it should know that if I get a response from 192.168.1.3 , by checking the dIP that must have been a request intended for me that I redirected to 192.168.1.3. and if I check the sIP I can see that the stuff that was supposed to come back to 10.0.0.2 was initially intended to go back to 192.168.1.2

    • @hnasr
      @hnasr  Před 3 lety

      The computer at 192.168.1.3 only knows that the packet needs to go to 10.0.0.2 , its the responsibility of 10.0.0.2 to send it back to 192.168.1.2 thats the job of DNAT .. watch NAT video for more details
      czcams.com/video/RG97rvw1eUo/video.html

    • @ShueFig
      @ShueFig Před 3 lety

      @@hnasr Yup, I actually was asking about how 10.0.0.2 knows to forward this packet back to 192.168.1.2, which I believe @MrBrN197 answered above.
      Thank you so much for making these videos btw, learnt a ton of new stuff :)

    • @ShueFig
      @ShueFig Před 3 lety

      @@brod515 okay, I kind of figured it was some kind of reverse lookup, thank you! Do you know if there's a name or something I can google to know more about this process?

  • @fabwrld5773
    @fabwrld5773 Před 2 lety

    When their is a reply from 192.168.1.3: 8080 to 10.0.0.2:1234, do we need to have another DNAT rule to send this packet to 192.168.1.2?

  • @pajeetsingh
    @pajeetsingh Před 2 měsíci

    iptables vs ip route? Do they serve same purpose? Why does changes made using ip route does not show up in iptables rules?

  • @ConversationWay
    @ConversationWay Před 3 měsíci

    is it possible to have 'any' protocol ?

  • @ahangk
    @ahangk Před 3 lety

    Can you show us how to redirect port 443 to another port, Thanks.

  • @edgarlip2
    @edgarlip2 Před měsícem

    @Husseein -
    ♦it looks like after the redirect - the packet is going straight to the process - but i know that is is passing the INPUT table before ... and u have omitted this hole important part !
    ♦REDIRECT example config - PREROUTING is used for incoming traffic from the out side of the machine ( which u used ) , and "-t nat -I OUTPUT" is used to handle traffic that is originated
    from the local machine - why did u decided to use the "PREROUTING " ?

  • @alexbennion9087
    @alexbennion9087 Před 3 lety

    Thanks, "babes"!

  • @thoongchinglee4905
    @thoongchinglee4905 Před 3 lety

    So my question is iptables able to replace port forwarding, reverse proxy, and as a firewall rules if we do it right?

    • @DamjanDimitrioski
      @DamjanDimitrioski Před 3 lety

      Same question, can we match HTTP path like /api1/ on inbound port 80/443 and redirect to some docker container on port N.

    • @ShamilSattarov
      @ShamilSattarov Před 3 lety

      @@DamjanDimitrioski nope, guys, it's completely different levels of OSI. Iptables works with 2nd to 4th and http is 7th.

    • @thoongchinglee4905
      @thoongchinglee4905 Před 3 lety

      Sorry, I mix up the word reverse proxy. It shouldn't be there

    • @autohmae
      @autohmae Před 3 lety

      @@DamjanDimitrioski their are some possible ways to do but it gets complicated and error prone to to let iptables look inside the packet and hopefully find the URL and it only works with HTTP not HTTPS. But the Linux kernel has some crazy capabilities if you know how to use it because you can also upload code into the Linux kernel with "eBPF"

    • @hnasr
      @hnasr  Před 3 lety

      As some said there are ways but very complicated and not a replacement for layer 7 proxying. But layer 4 proxying? perhaps

  • @codewithmubin8866
    @codewithmubin8866 Před 2 lety

    What is the equivalent tool of iptables in Windows?

  • @SuperMan-rw6iz
    @SuperMan-rw6iz Před 11 měsíci

    why nginx if we can use something like this to achieve network control?

  • @shubhamjain9433
    @shubhamjain9433 Před rokem

    I am not getting ack packets when I redirect the port. Can anyone help?

  • @emmanuell89
    @emmanuell89 Před rokem

    you're funny, thanks for the explanation

  • @CharlieArehart1
    @CharlieArehart1 Před 3 lety

    More great stuff as always, Hussein, and thanks. But I had a couple of observations/questions for you and that may help other viewers considering all this.
    First, you mention the need to enable ip forwarding, as it's off by default in Linux (at 21:25) . I'm a little surprised you didn't elaborate at least a bit (or offer a brief caution) on the possible implications of doing that. I'm new to the topic of iptables myself, so again thanks for the great intro, but I've been burned by making such a change too innocently. :-)
    A quick poke around the web found a few cautions. I fear that some viewers may not be likely to consider that without your suggesting it. Given how your goal always is to educate and share words of advice, it just seemed a missed opportunity. Or do you think any concern is perhaps overstated?
    Second, while you understandably picked an http example to keep things simple, it seems that such a use case could be solved at a higher level by a reverse proxy (nginx, haproxy, varnish, etc.). But I don't think you mentioned that.
    Given the concern above, it seems worth at least a brief mention. Of course you've covered that topic well in the past, but you produce so much great content that most people can't track it all. :-) This would have seemed another one worth offering a pop up link to such a past video.
    All that said, as one who also likes to share knowledge myself, I realize it's a balancing act. Just trying to help.
    And I appreciate that this was just a part 1, introducing the wonders and power of iptables with your inimitable style. :-) Again I share these observations here for your viewers, and perhaps also for your consideration in future videos, assuming you'd not already planned to cover them. :-)

  • @ethangender
    @ethangender Před 9 měsíci

    im trying to work on that and so far not reached the solution. i have this 2 networks 10.0.0.6 (public IP) 192.168.30.254 (private) I want to enable there iptables to port map the apache server that is located at 192.168.30.2 , everything I tried not working, connection refused :
    here is my chain:
    sudo iptables -t nat -A PREROUTING -d 10.0.0.6 -p tcp --dport 80 -j DNAT --to-destination 192.168.30.2:80
    sudo iptables -t nat -A POSTROUTING -j MASQUERADE
    sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
    sudo iptables -A FORWARD -p tcp --dport 80 -j ACCEPT

  • @gentleling9404
    @gentleling9404 Před rokem

    how to route from to IPv6 to ipv4

  • @alizia2186
    @alizia2186 Před 3 lety

    Brother can you recommend any books regarding Database Or Application Scale Out. I would be really grateful.😘

    • @aymanpatel5862
      @aymanpatel5862 Před 3 lety +2

      1. Designing Data Intensive Applications by Martin Klepmann
      2. Database Internals

    • @alizia2186
      @alizia2186 Před 3 lety

      @@aymanpatel5862 Thank you🙏

  • @thuongnguyennhu4312
    @thuongnguyennhu4312 Před 2 lety

    Can you share your presentation file on this video? thanks u so much

    • @hnasr
      @hnasr  Před 2 lety

      Sorry just saw the message! Here it is IPTables (Members slides)
      Slides payhip.com/b/VTsPG

  • @theinthanhlan3582
    @theinthanhlan3582 Před 2 lety

    2021 ? I am learning in 2022😁

  • @kikandisafari4920
    @kikandisafari4920 Před 3 lety

    actually I think for the POSTROUTING it should have been --out-interface

  • @AakashVarma95
    @AakashVarma95 Před 3 lety

    26:00

  • @rexsovelllejes9383
    @rexsovelllejes9383 Před 2 lety

    Buuuttt it doesn't work on my linux vps :( I just only thought that this can solve my almost half a year problem

  • @AvineshSinghSaab
    @AvineshSinghSaab Před 2 lety

    12:10 Wouldn't that be dangerous - if you do not specific matching port and protocol - everything goes to port 8080 including TCP on Port 22 - essentially locking you from accessing your RPi