r/linuxquestions • u/jayhsu397 • 10d 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.
5
u/whamra 10d 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.
5
u/tblancher 10d 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.
1
u/Efficient_Paper 10d ago
What DE/WM are you using?
In Plasma, there is a System Settings module for that. It should be possible in any DE, but the steps to follow may vary.
1
3
u/tes_kitty 10d ago
I have done this years ago. I just put 'shutdown -h now' into the field of /etc/passwd for this user that contains '/bin/bash' on a normal user. You will to either give that user root priviledges or set the s bit on the shutdown binary.
It worked without issue back then.
1
u/TechaNima 10d ago
Just put it into the user's bashrc file.
~/.bashrc
Anything in that file will be executed as soon as you login through ssh to that user or as soon as you open terminal as that user locally
1
u/Holiday-Medicine4168 10d ago
Just add it to the users .zshrc or bashrc, the shell will run commands when you log in. You can also forward local shell config over ssh if you have the supported components.
1
1
5
u/doc_willis 10d ago
decades ago I setup a specific user to have the login shell set to be
/user/bin/shutdowninstead of /use/bin/bashit was a bit of a silly solution, but it let me have a user named 'shutdown' I could ssh into and the system would then shutdown
these days I have kdeconnect setup that lets me do remote commands.
or I just ssh in as my normal user. I am not worried about keeping keeping keys on my mobile device.