sudo vim /etc/systemd/logind.conf

set

HandleLidSwitch=lock

then

systemctl restart systemd-logind.service

link