Switch user without password inside shell

I want to switch to another user without password inside shell.

I used the below command and it is not working.

sudo su - user1

user1 is not in the sudoers file. This incident will be reported.

I'm getting the above message.

If I want to add user1 into the sudoers file using below command, sorry I don't have root permission to edit such files :frowning:

USER_NAME   ALL=(ALL) ALL

Using: ksh, HP-UX. Someone please let know how to switch user without password.

Does your server allow paswordless ssh sessions?

I'm thinking you could setup a public/private key pair for the user1 account and use ssh to execute the commands.

Thanks Chubler_XL,

I just learned about ssh and tried in command prompt and it working as expected.

ssh -keygen 
  • To set up public/private key.
ssh host1 
  • To login to another host machine (host1)
ssh user1@host1 
  • To change user.
ssh user1@host1 -P password 
  • Provide password with ssh command (which will avoid prompting for password)

But here the problem is when I used this in shell, for ex:

#!/bin/ksh 
 
ssh user1@host1 -P password   #--> Trying to login as different user 
 
sleep 1 
 
ping host3 
 
- Recycle components step to come here
 

After login to user1, the ping and recycle components are not happening in user1.

Steps to be executed by ssh that come from the script (rather than from the script's standard input) need to be redirected:

#!/bin/ksh 
ssh user1@host1 -P password <<-EOF
        sleep 1 
        ping host3
        # other commands to be executed through ssh
EOF
# other commands to be executed after ssh session terminates.

Note that using -P password this way makes user1's password on host1 visible to anyone who runs ps while ssh is running. Consider putting the password in the here-document; then that user's password is only public to anyone with permission to read your script. Depending on your security requirements, you may want to explore other ways to enter the password.

Hi Don,

I tried the above,

When executing the ssh command with or without EOF concept, it will ask for my personal user id "roozo" other than user1(machine user)

When executing the above inside shell, the below is happening where I have to enter my personal id say "roozo" and exiting from user1

Enter you User Id: stty : Not a typewriter 
logout user1 HP-UX

What, exactly, did you try? There's a whole lot of "add your stuff here" in that code where we can't see what you actually added.

Show us exactly what you did, word for word, letter for letter, keystroke for keystroke.