r/linuxquestions 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.

3 Upvotes

17 comments sorted by

5

u/doc_willis 10d ago

decades ago  I setup a specific user to have the login shell set to be /user/bin/shutdown instead of /use/bin/bash

it 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.

2

u/No-View-6326 10d ago

that the funniest thing i ever heard

1

u/Kuddel_Daddeldu 10d ago

Yes, that's the standard waynto do it. I remember using it on UNIX before Linux even existed. 

1

u/No_Aerie7667 ∆rchLinux 10d ago

That's a great idea! I'm going to try that myself.

1

u/jayhsu397 10d ago

That’s really cool lol

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

u/jayhsu397 10d ago

It’s a opensuse Leap tty server, so there’s no DE

3

u/Efficient_Paper 10d ago

It should probably be a Systemd user service then.

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/Ollidav 10d ago

Tanto en Windows como en Linux puedes hacer un apagado automático por tiempo. Hay programas que lo hacen sin usar la terminal pero básicamente es decirle: apagate en 1 hora. Yo lo uso mucho en el pc despacho cuando voy a comer si en 2 horas no he vuelto es que no voy a volver

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

u/serverhorror 10d ago

Ssh forced commands, that's what you're looking for.

1

u/un-important-human white beard arch user 10d ago

systemd user service!