-2

To facilitate my work. I add

cd $working_dir
sudo su

in ~/.bashrc file.

then I could login as root and in my working dir.

But when I need run a command from remote through ssh, It is just blocked (seems like su ran and waited for an input, but I didn't get a shell).

So, the problem is:

How can I login with dir changing to working-dir and with root privilege automatically and without ssh cmd blocked?

I use pstree -a, blocked process tree shown as this:

|   `-sshd
|       `-sshd
|           `-kbash -c pstree\040-a
|               `-sudo su
|                   `-su
|                       `-kbash
jixiang
  • 121
  • Commands like this belong in ~/.bash_profile, you don't want change directory for every shell. Having said this, standard techniques like case $- in (*i*) ;; (*) return ;; esac to exit ~/.bashrc before running the su might help. The big question is when you ssh into this box do you want to be root? If so why bother logging in as a normal user and just go directly in as root? – icarus Jan 20 '17 at 07:51
  • @icarus Thanks for your magic codes, works fine for me. For your big question : my company op team manage our users(privileges). we normal user have sudo privilege without passwords, but we can only logging in as our own user, but all services under maintenance belongs to root. That's why I changed my users .bashrc for automatically switching root. – jixiang Jan 20 '17 at 09:59

1 Answers1

0

You can do it with:

ssh -t root@your_server "cd /path_to_your_working_dir ; bash"

Note that in this case your shell must be bash.