The Unwanted Sleep State
By default, Ubuntu is configured to act like a standard laptop operating system: when you physically close the lid of your laptop, the system automatically enters a low-power Suspend (sleep) state. This is ideal for saving battery when tossing the machine into a backpack. However, if you are using your Ubuntu laptop as a makeshift server, a media center tucked behind a TV, or if you simply prefer to work with an external monitor while the laptop lid is shut, this automatic suspension immediately kills your network connections and halts any running processes.
Modifying the Logind Configuration
To keep your system running and stop Ubuntu from automatically suspending when the laptop lid is closed, you must edit the systemd login manager configuration file.
Open a terminal window (Ctrl+Alt+T). You need administrative privileges to edit this core system file. Type sudo nano /etc/systemd/logind.conf and press Enter (provide your password when asked). The nano text editor will open. Scroll down through the file until you find a line that reads #HandleLidSwitch=suspend. The hash symbol (#) means the line is currently a comment and the system is using the default behavior. You need to uncomment the line by deleting the #, and then change the command from “suspend” to “ignore”. The line should exactly read: HandleLidSwitch=ignore. Press Ctrl+O to save, Enter to confirm, and Ctrl+X to exit. Finally, apply the changes by restarting the service: type sudo systemctl restart systemd-logind (note: this command might immediately log you out of your current desktop session).