1

I want to login to an interactive shell on a server using SSH and, after logging in, execute two commands.

This works: ssh user1@server -t "cd /home/user2; bash --login"

None of the following work:

ssh user1@server -t "cd /home/user2; bash --login -c 'source /home/user2/.bashrc'"

ssh user1@server -t "cd /home/user2; bash --login -c source /home/user2/.bashrc"

ssh user1@server -t "cd /home/user2; bash --login source /home/user2/.bashrc"

ssh user1@server -t "cd /home/user2; bash --login; source /home/user2/.bashrc"

When I call ssh with the -v flag, I see the following before the ssh session exits:

debug1: Sending command: cd /home/user2; bash --login -c "source /home/user2/.bashrc"
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0

I know this might look odd; I need to login as user1 so that I can use software (legally) that can only be run by user1, but I want to run my personal .bashrc and work in my personal working directory. I can not modify /home/user1/.bashrc. Is there any better way to do what I'm trying to do?

1 Answer 1

3

I might be missing something but in order to achieve the stated goal:

  1. Log in as user1
  2. Set working directory to the home of user2
  3. Source .bashrc from user2

it is enough to say

ssh user1@server -t "cd /home/user2; bash --rcfile /home/user2/.bashrc -i"

Note that in most cases this would work without the -i and without giving the full path to .bashrc:

ssh user1@server -t "cd /home/user2; bash --rcfile .bashrc "
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that's exactly what I needed. I thought there might be a way to specify an rc file, but I've never needed to do such a thing before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.