6

I connect to server through ssh under vpn. To make project changes I always do the same procedure after accessing server - log as project_user, because with my identity I get sh shell and I don't have write access to project files.

In my ~/.ssh/config file I tried to use LocalCommand parameter to automate the procedure:

LocalCommand sudo -iu project_user bash

and then

LocalCommand /bin/sh -c sudo -iu project_user bash

But none of this makes any change - after connecting I'm still in sh shell and I need to run above commands again and again.

Any ideas?


Update: sorry, but this LocalCommand parameter is meant for local and not remote command. Is there any ssh_config parameter that would let me execute remote command after connecting to server?
Or at least change the shell to bash so that I can execute my command through bash initialization scripts?

  • why not simply add the command to the end of the ssh cmd? eg ssh .... sudo ... bash -i – meuh Jul 5 '15 at 16:59
  • That would be simple task for terminal, but I want to use editor plugin. Do you perhaps know which script is executed on ssh connection with sh shell? – vlad Jul 5 '15 at 17:04
  • You probably just need to say you need a pty. Use ssh -t .... – meuh Jul 5 '15 at 17:23
  • Yes but I want the command executed automatically on remote server, just by ssh-ing to it without ssh command line parameters. Doesnot initialization of remote sh shell parse some scripts before starting? – vlad Jul 5 '15 at 18:19
  • I dont think there any important differences between an ssh with no command and ssh -t ... bash --login. – meuh Jul 5 '15 at 19:00
2

As the name indicates, LocalCommand runs a command on the local (client) machine, not on the remote (server) machine. The command that runs on the server is the argument(s) that you pass to ssh after the options and the host name.

By default, a terminal is not created when you pass a command, so pass the -t option to create one. Instead of ssh yourserver, run

ssh -t yourserver exec sudo -iu project_user
5

The OpenSSH server permits you to associate a command with an ssh key. When a particular ssh key is used to connect to a server, the server will ignore the command requested by the client and run the command associated with the key instead. If you are using ssh keys for these sessions, you could use this feature to launch your sudo command.

You set this up through your account's "authorized_keys" file on the server. The sshd(8) man page describes the authorized_keys file format. Briefly, authorized_keys is a series of lines that each start with something like this:

ssh-rsa AAAAB3NzaC1yc2...

You can add a command directive to the front of a line:

command="sudo -iu project_user bash" ssh-rsa AAAAB3NzaC1yc2...

When the corresponding key is used to authenticate, the SSH server will ignore the command requested by the SSH client, and run the specified command instead. The ssh session will close when the sudo process (and the shell running within it) exits.

I recommend that you set up two keys for ssh'ing into this server: one key that performs this sudo operation, and one that doesn't. You will probably need to be able to get into the server without performing the sudo operation from time to time. The key that runs sudo also won't work for running scp or sftp, at least not without setting up something more sophisticated on the server.

0

If you can log in to the remote system you can change your default shell there:

chsh -s /bin/bash
  • Well if I'm there in terminal I can just run bash. – vlad Jul 5 '15 at 18:18
  • @vlad yes, of course. But you asked at the bottom of your question how to change the shell to bash. This answer shows you how to do that. – roaima Jul 5 '15 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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