How can I sudo su and change directory just after?
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
Related videos on Youtube
Mariano Martinez Peck
Updated on September 18, 2022Comments
-
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 withuserYYY
andpwd
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 wassudo 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 about 9 yearsseems 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 about 9 yearsactually, 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 over 5 yearsthis has helped me edit my
.bashrc
file tosudo
thencd
where as having 2 separate lines just wasn't working.