How can I sudo su and change directory just after?

19,207

Solution 1

alias userYYY='sudo su userYYY -c "cd /a/path/that/only/userYYY/has/access; /bin/bash"'

Solution 2

One option would be to edit ~/.bashrc of the target user, and add cd there:

cd /a/path/that/only/userYYY/has/access
Share:
19,207

Related videos on Youtube

Mariano Martinez Peck
Author by

Mariano Martinez Peck

Updated on September 18, 2022

Comments

  • Mariano Martinez Peck
    Mariano Martinez Peck over 1 year

    I would like to create an alias that does something like this:

    alias userYYY='sudo su userYYY; cd /a/path/that/only/userYYY/has/access'
    

    So then from my command line, I am logged in with a sudo user, and I would like to type the alias userYYY so that my shell is now logged with userYYY and pwd is /a/path/that/only/userYYY/has/access.

    How can I do that? This userYYY is for running some processes, and there must be anything in its home. Hence, I tried changing its $HOME using:

    sudo usermod -m -d /a/path/that/only/userYYY/has/access userYYY 
    

    And then from my shell with my sudoer file I did sudo su userYYY. But that didn't work. The only that worked was sudo su -l userYYYY but that opened a new bash inside my original shell (-bash-4.1$ ....).

    In summary, what I want is to simply avoid having to write 2 lines in my shell:

    sudo su userYYY
    cd /a/path/that/only/userYYY/has/access
    

    Any ideas?

  • sonjz
    sonjz about 9 years
    seems to work fine, but not sure how to remove this error, or if its an issue. using ubuntu 14.04LTS, noticed there was an issue with on debian... bash: cannot set terminal process group (13964): Inappropriate ioctl for device\nbash: no job control in this shell
  • sonjz
    sonjz about 9 years
    actually, after using it for a while, i noticed that the terminal gets killed pretty frequently using this method (disconnect around 15-30 min, regularly, my terminal lasts around 6hrs before disconnect)
  • RozzA
    RozzA over 5 years
    this has helped me edit my .bashrc file to sudo then cd where as having 2 separate lines just wasn't working.