2

How would I go about automatically stopping all network activity inbound and outbound when I lock my screen? Including http, ftp, ssh, etc.

I can test this for example by putting on a Youtube video and then locking my screen. The video would still be playing even though the computer is locked. Indicated continued network activity.

Once locked, I want my Ubuntu desktop to stop all data traffic to and from the computer, and resume it from where it left off when I log back in. Is there a way to do this? Can you please help out?

AlaraLK
  • 21

1 Answers1

1

This answer was inspired by this answer to How to run a command or script at screen lock/unlock?

For Ubuntu 14.04:

Assuming your using the default network manager, create a script (I put mine in ~/bin and called it netlock.sh)

Here's the content:

#!/bin/bash

dbus-monitor --session "type='signal',interface='com.ubuntu.Upstart0_6'" | \
(
  while true; do
    read X
    if echo $X | grep "desktop-lock" &> /dev/null; then
      nmcli nm enable false;
    elif echo $X | grep "desktop-unlock" &> /dev/null; then
      nmcli nm enable true;
    fi
  done
)

This script will disable networking on lock and re-enable on unlock.

If you want it to run every time at startup see How to run scripts on start up?

If you want it to run everytime you login, see How do I start applications automatically on login?

For Ubuntu 16.04: simply edit the script and change the nmcli nm enable false to nmcli networking off and the nmcli nm enable true to nmcli networking on as required due to changes in nmcli

Of course anything already downloaded and buffered will still play until the buffers run dry so if you want more exact control you'll have to use the pause button on the youtube player.

I can't gaurantee that everything will resume where you left off, as a server can drop inactive connections, but hopefully this is close to what you want.

Elder Geek
  • 36,023
  • 25
  • 98
  • 183