LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   execute with privileges a particular script at login time (https://www.linuxquestions.org/questions/linux-newbie-8/execute-with-privileges-a-particular-script-at-login-time-4175605419/)

fabioca 05-07-2017 04:14 AM

execute with privileges a particular script at login time
 
I would like that when a certain unprivileged user logs in either the console or ssh, instead of /bin/bash, a special command was executed with privileges:

Code:

ip netns exec mynamespace /bin/bash
The purpose is for this user to see a segregated network stack.

I tried to put this is the correspondent entry in /etc/passwd, but that did not work.

I could try and put it in bashrc, but at that point the shell has already been launched and this would launch a new shell, plus I would need to solve the problem of how to give this unprivileged user the right to run this command, which I do not know how to do either.

Is there a way to do that?

Thanks a lot

Turbocapitalist 05-07-2017 05:12 AM

There are two ways depending on how locked down it must be.

If it ok that the people logging in have access to the script, you can put it in ~/.ssh/rc on the server in their account(s) and make sure to enable PermitUserRC in sshd_config.

If the people logging in should not have write access to the script, the easy way is to call it from /etc/ssh/sshrc

See the manual page in the section "SSHRC" for details.

Code:

man sshd
As far as I know, you'll have the following environment variables available to the script. For group, you'd have to calculate that from $LOGNAME or $USER

LOGNAME
HOME
SSH_TTY
MAIL
SSH_CLIENT
PATH
SHELL
TERM
SSH_CONNECTION
USER

So you could make a case statement matching accounts that need customized scripts.

Turbocapitalist 05-07-2017 05:14 AM

Or you could use ForceCommand either on the server's configuration or in the SSH key used to log into the server in ~/.ssh/authorized_keys. Again, see

Code:

man sshd
but in the section "AUTHORIZED_KEYS FILE FORMAT"

fabioca 05-07-2017 05:31 AM

Thanks for your suggestions. I will try.

Does this work automatically also when logging directly from the console?

The console is actually more important than ssh for me now.

Thanks

Turbocapitalist 05-07-2017 05:36 AM

The above only works with logins via SSH.

Habitual 05-07-2017 07:55 AM

"sudo - You're Doing it wrong"


All times are GMT -5. The time now is 02:14 AM.