Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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 and ksh is not installed.

Question

How do I carry commands out as the user in such a case?

share|improve this question
    
Why is a user's shell set to something that doesn't exist? –  MDMarra Nov 2 '12 at 1:25
1  
/etc/passwd comes from NIS. –  Sandra Nov 2 '12 at 1:34
1  
Well that explains that. –  MDMarra Nov 2 '12 at 1:41

3 Answers 3

up vote 14 down vote accepted

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.

share|improve this answer
    
That will still attempt to use the shell that's in /etc/passwd, which seems to be the bigger problem. –  freiheit Nov 2 '12 at 16:50
    
The 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) –  freiheit Nov 2 '12 at 17:12

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.