Possible to ignore the users shell in "su" command?
Solution 1
If you don't put the -
after su
then it won't load that user's environment, but the commands will still be run as that user.
It appears that I misunderstood your actual problem. As other have said -s
is the option you probably actually want.
Solution 2
You can use:
su -s /bin/sh -c 'mkdir bin' user1
The -s
option to su specifies the shell to use, overriding whatever is in /etc/passwd.
I dropped the -
from your command because, in addition to loading the profile, it will probably change the working directory to the home directory of the user for the command, so you'd be creating 'bin' in user1's home directory, not your current directory.
Solution 3
I believe the -s/--shell option to su lets you pick the shell to use while keeping the other parts of users environment, /bin/sh is specified by posix so should be available everywhere.
Related videos on Youtube
Sandra
Updated on September 18, 2022Comments
-
Sandra over 1 year
If I do
su - -c 'mkdir bin' user1
then I get
su: /usr/bin/ksh: No such file or directory
because the users shell is set to
ksh
in/etc/passwd
andksh
is not installed.Question
How do I carry commands out as the user in such a case?
-
MDMarra over 11 yearsWhy is a user's shell set to something that doesn't exist?
-
Sandra over 11 years
/etc/passwd
comes from NIS.
-
-
cpl593x over 11 yearsThat will still attempt to use the shell that's in /etc/passwd, which seems to be the bigger problem.
-
cpl593x over 11 yearsThe shell puts it's own identity into the $SHELL environment variable, but /etc/passwd is where su is looking for which shell to run. ` - ` is passed on to the shell invocation telling it to act as a "login" shell (bash runs a profile file instead of a bashrc file, changes working directory, etc)