How do I see what pid/process has modified a linux routing table?

Anyone know how to see what pid/process has modified a linux routing table (specifically on Ubuntu )? I have an interesting problem where a route that I have created has been deleted over time, but can’t figure out what. I’ve tried rtmon but seems to only show timestamps of the adds/deletes

ipsirc,
@ipsirc@lemmy.ml avatar
bizdelnick,

I guess it can be NetworkManager if it is used to configure the interface but the route is added manually.

Deckweiss, (edited )

The better solution:

sudo apt-get install auditd

Set up watch: sudo auditctl -w /path/to/your/file -p wa -k file_change_monitor

Check log: sudo ausearch -k file_change_monitor


Alternative solution:

If you know the file that is being edited you can set up watches with inotifywait and log it to a file. This may possibly not work because lsof might not be quick enough.

sudo apt-get install inotify-tools

then put this script in autostart


<span style="color:#323232;">#!/bin/bash
</span><span style="color:#323232;">
</span><span style="color:#323232;">FILE_TO_MONITOR="/path/to/your/file"
</span><span style="color:#323232;">LOG_FILE="/path/to/logfile.txt"
</span><span style="color:#323232;">
</span><span style="color:#323232;">inotifywait -m -e modify,move,create,delete --format '%w %e %T' --timefmt '%Y-%m-%d %H:%M:%S' "$FILE_TO_MONITOR" |
</span><span style="color:#323232;">while read path action time; do
</span><span style="color:#323232;">    # Get the PID of the process that last modified the file
</span><span style="color:#323232;">    PID=$(lsof -t "$FILE_TO_MONITOR" 2>/dev/null)
</span><span style="color:#323232;">
</span><span style="color:#323232;">    # Get the process name using the PID
</span><span style="color:#323232;">    PROCESS_NAME=$(ps -p $PID -o comm= 2>/dev/null)
</span><span style="color:#323232;">
</span><span style="color:#323232;">    # Log details to the file
</span><span style="color:#323232;">    echo "$time: File $path was $action by PID $PID ($PROCESS_NAME)" >> "$LOG_FILE"
</span><span style="color:#323232;">done
</span>

Don’t forget to modify the values at the top of the script and make it executable.

mikey,

They aren’t asking about changes to a file describing the routing config, rather the actual in-use routing config. Unless the routing rules are modified through a couple of files (which I doubt), this doesn’t answer the question.

Cool commands though.

Deckweiss,

My bad, I thought in Linux everything is a file

mikey, (edited )

Well, the routes might manifest somewhere as files, but I don’t expect anyone to be able to viably parse them without commands like ip or ifconfig (or know where the files even are).

Some devices (like disks for example) are very straightforward to use as files, while some other special files (like USB devices) are so weird/ugly to use that everyone uses tools/libraries to access them (like libusb).

This is very off-topic, but there’s a great talk by Benno Rice that talks about this (among many others): youtu.be/9-IWMbJXoLM

Deckweiss,

Thank you for the info and I’ll listen to that talk

Shadow, (edited )
@Shadow@lemmy.ca avatar

I don’t think any historical data would exist, but you could probably watch changes with ftrace

  • All
  • Subscribed
  • Moderated
  • Favorites
  • linux@lemmy.ml
  • localhost
  • All magazines
  • Loading…
    Loading the web debug toolbar…
    Attempt #