Possible to ignore the users shell in "su" command?

16,904

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.

Share:
16,904

Related videos on Youtube

Sandra
Author by

Sandra

Updated on September 18, 2022

Comments

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

    Question

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

    • MDMarra
      MDMarra over 11 years
      Why is a user's shell set to something that doesn't exist?
    • Sandra
      Sandra over 11 years
      /etc/passwd comes from NIS.
  • cpl593x
    cpl593x over 11 years
    That will still attempt to use the shell that's in /etc/passwd, which seems to be the bigger problem.
  • cpl593x
    cpl593x over 11 years
    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)