Wednesday, May 1, 2024
 Popular · Latest · Hot · Upcoming
3
rated 0 times [  3] [ 0]  / answers: 1 / hits: 1867  / 1 Year ago, wed, may 10, 2023, 11:17:05

So I think many people have gone through this issue where you SSH into a machine, cd to whatever path you need to be at, and then realize you actually want to be root and not your own user. However, when you sudo su -, you sign in as root and get taken to root's home directory.



Is there a way to copy the PWD before I run sudo su - and then cd to it after without typing it again? Something like cd - that is available across users.


More From » ssh

 Answers
6

A few things.



First you should almost never sudo su. There's no reason for it, but lets pretend your in one of the very few circumstances where you want to sudo su.



Second, sudo su (no -) will keep you in your current directory and bring over your current shell setup (i.e. vars and such). So your real answer is to use sudo su without a -. Now lets pretend you want to use it together with - for some reason (note: the usages for this are damn rare). The result is that you will log in as root and not just run things as root.



Third, assuming you want to get back to your directory after sudo su -ing. Your best bet is to just copy and paste, or navigate back to the directory. However, if you insist:




sudo su - -c "cd `pwd`; bash"




I would like to stress however that you are almost certainly using sudo and su incorrectly. The whole point of sudo (and not having a root account) is so that you run things as your self with elevated permissions. Your circumventing that.



I suggest reading:




[#29457] Thursday, May 11, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
oileweaty

Total Points: 337
Total Questions: 108
Total Answers: 105

Location: Western Sahara
Member since Mon, May 3, 2021
3 Years ago
;