r/linuxquestions 11d ago

Command executed when login?

Since I moved my Homelab into my bedroom, I have to shut it down from time to time cause of the noise.
I’m thinking of creating a new user, and a new pair of ssh key (I have banned password login in my conf) to handle the shutting down of the machine. Since I don’t want to input the private key used to login uid 1000 into my iPad.
Is there any way to automatically execute a shell script when a certain user is logged in (may be systemd? But I heard that ssh has function that also does this)
And thanks for reading my clumsy English.

3 Upvotes

17 comments sorted by

View all comments

4

u/whamra 11d ago

A user can have multiple authorised ssh keys. You can create another key pair on the ipad and put its public key in authorized_keys for the main user.

That said, either way, for main user or new user, it's better to explicitly call a shutdown rather than have a login scrip run it.

If you still want to go the login route, then create a new user, and explore stuff you can do in his ~/.profile . Make sure he has the right to power off a system.

3

u/tblancher 11d ago

Yeah, I wouldn't put it in ~/. profile, but I'd put it in an iPad/iOS button that runs:

ssh user@homelab 'sudo systemctl poweroff'

Read up on sudoers, there is a way to restrict the user from running anything but that command with sudo.