1
ssh -vvv -F /home/me/.ssh/config serva -t "source ~/.bashrc"

This is the output I get when I use -vvv flag:

debug1: Authentication succeeded (publickey).                    
debug1: channel 0: new [client-session]                          
debug3: ssh_session2_open: channel_new: 0                        
debug2: channel 0: send open                                     
debug1: Requesting [email protected]                  
debug1: Entering interactive session.                            
debug3: Wrote 128 bytes for a total of 2413                      
debug2: callback start                                           
debug2: client_session2_setup: id 0                              
debug2: channel 0: request pty-req confirm 1                     
debug1: Sending command: source ~/.bashrc                        
debug2: channel 0: request exec confirm 1                        
debug2: fd 3 setting TCP_NODELAY                                 
debug2: callback done                                            
debug2: channel 0: open confirm rwindow 0 rmax 32768             
debug3: Wrote 400 bytes for a total of 2813                      
debug2: channel_input_status_confirm: type 99 id 0               
debug2: PTY allocation request accepted on channel 0             
debug2: channel 0: rcvd adjust 2097152                           
debug2: channel_input_status_confirm: type 99 id 0               
debug2: exec request accepted on channel 0                       
debug1: client_input_channel_req: channel 0 rtype exit-status rep
ly 0                                                             
debug1: client_input_channel_req: channel 0 rtype [email protected]
 reply 0                                                         
debug2: channel 0: rcvd eow                                      
debug2: channel 0: close_read                                    
debug2: channel 0: input open -> closed                          
debug2: channel 0: rcvd eof                                      
debug2: channel 0: output open -> drain                          
debug2: channel 0: obuf empty                                    
debug2: channel 0: close_write                                   
debug2: channel 0: output drain -> closed                        
debug2: channel 0: rcvd close                                    
debug3: channel 0: will not send data after close                
debug2: channel 0: almost dead                                   
debug2: channel 0: gc: notify user                               
debug2: channel 0: output drain -> closed                     
debug2: channel 0: rcvd close                                 
debug3: channel 0: will not send data after close             
debug2: channel 0: almost dead                                
debug2: channel 0: gc: notify user                            
debug2: channel 0: gc: user detached                          
debug2: channel 0: send close                                 
debug2: channel 0: is dead                                    
debug2: channel 0: garbage collecting                         
debug1: channel 0: free: client-session, nchannels 1          
debug3: channel 0: status: The following connections are open:
  #0 client-session (t4 r0 i3/0 o3/0 fd -1/-1 cfd -1)         
                                                              
debug3: channel 0: close_fds r -1 w -1 e 6 c -1               
debug3: Wrote 32 bytes for a total of 2845                    
debug3: Wrote 64 bytes for a total of 2909                         

The server side log has the following message: sshd[18763]: Received disconnect from ...

I am using CentOS 6.4

Edit

My original question was flawed. Sorry about that. What I wanted to execute bash shell with the rc file I wanted (~/.bashrc_temp) and then execute something else. I think PROMPT_COMMAND is the recommended option it seems like, or having the command executed within ~/.bashrc_temp itself, which is less than ideal, but I can probably put some conditional statements.

2
  • 2
    What are you expecting to happen? That output suggests it's succeeding and then terminating at the end of the command. I'm not sure what the goal of using source is here. Commented Aug 27, 2014 at 21:30
  • Also note that bash already interprets ~/.bashrc when invoked over ssh (with and without -t and even when non-interactive like when passed a command like source ~/.bashrc (which causes sshd to run bash -c 'source ~/.bashrc')). Commented Aug 27, 2014 at 21:31

1 Answer 1

1

When you do:

ssh serva -t "source ~/.bashrc"

ssh tells sshd to invoke the login shell of the remote user as:

the-shell -c 'source ~/.bashrc'

That tells the-shell to run that command and exit.

Probably, what you want is to run an interactive shell and have that interactive shell to run that source ~/.bashrc command and then to issue a prompt and read more commands to execute from you.

First note that in the case of source ~/.bashrc, that's not needed since bash already sources your ~/.bashrc when interactive (actually over ssh, it even does it when non-interactive). So:

ssh serva

Is enough.

Now, if you want to run a command and then an interactive shell, then you'd do:

ssh -t serva 'cmd; bash'

(there, the -t is needed as ssh doesn't start a pseudo-terminal by default when passed a command to execute)

cmd would not be executed by that bash though (it would be executed by the shell (your login shell there) started by sshd to interpret that cmd; bash command line).

If you wanted that interactive bash to run that command. A trick is to use bash's PROMPT_COMMAND variable. bash interprets the content of that variable as shell code to be executed before each prompt. So you can do:

ssh -t serva 'PROMPT_COMMAND="cmd; unset PROMPT_COMMAND" bash'
4
  • how can I run a command after executing bash? And I am hoping to keep the shell in session. Commented Aug 27, 2014 at 21:43
  • @Forethinker, you mean after bash has exited? ssh -t serva 'bash; cmd' Commented Aug 27, 2014 at 21:46
  • but then cmd does not get to run within the bash session that started. Commented Aug 27, 2014 at 21:48
  • If you want cmd to be executed by that bash then use the PROMPT_COMMAND method. Or do you mean that you want that cmd to be executed just before bash exits? Commented Aug 27, 2014 at 21:50

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.