how to close an established network connection in linux and osx

5,135

Solution 1

You could attach to your process using gdb and close() the file descriptor corresponding to the connection. You can find out the file descriptor number using lsof -a -i4 -p $PID.

Solution 2

  • tcpkill -i eth0 { expression }
    (basic command/options)

  • tcpkill -i eth0 port 21
    (Kill all outgoing ftp (port 21) connection)

  • tcpkill host 192.168.1.2
    or
    tcpkill host hostname.domainname.com
    (Kill all all packets arriving at or departing from host 192.168.1.2)

  • tcpkill ip host 192.168.1.2 and not 192.168.1.111
    (To kill all IP packets between 192.168.1.2 and any host except 192.168.1.111)

Share:
5,135

Related videos on Youtube

Rodrigo Gonzalez
Author by

Rodrigo Gonzalez

Updated on September 18, 2022

Comments

  • Rodrigo Gonzalez
    Rodrigo Gonzalez over 1 year

    I used to do this in the Windows world with tool called TCPView: just select a established connection (ESTABLISHED), then select "Close Connection". I can't find a way to this on Mac OSX or Linux (I need both). The only way I know of is to kill the process that has the port open.

    My main use case is to simulate interruptions in connectivity to test failover, like unplugging a cable or losing a router or middleware situations.

    • terdon
      terdon about 11 years
      Killing the process is not good enough?
  • Rodrigo Gonzalez
    Rodrigo Gonzalez about 11 years
    works! too bad gdb pauses the program, but I'll look more into that