130

I'm using sudo su to start mysql and do some homework with it.

When I finish with mysql (or any other command), then I'm still in sudo.

How do I "log out", so my prompt changes back from # to $?

Mokubai
  • 92,720
KDecker
  • 2,261

6 Answers6

154

You don't need to use sudo and su together--su switches your user account (without arguments it switches you to root). sudo just elevates your privileges to root for the current command.

It's reccomended to use sudo instead of su if possible, but to return to your normal account after calling su, simply use the exit command

Paul
  • 1,664
  • 10
    sudo su will switch to the root account even if you don't know the root password. – Rob Apr 05 '12 at 18:19
  • 3
    There are differences between sudo su, sudo and su, and it's worth knowing those differences for safety reasons but also for your convenience. http://johnkpaul.tumblr.com/post/19841381351/su-vs-sudo-su-vs-sudo-u-i – Jeff Welling Apr 05 '12 at 18:22
  • @Rob but still it may not set the environment in a desired way - use sudo -i instead (in Ubuntu the root account is disabled by default = there exists no valid password) – guntbert Jul 13 '13 at 21:15
  • 2
    @Rob or sudo -s for shell. – Joel Mellon Sep 03 '15 at 20:11
  • Nothing suggested here works – Totty.js Jan 20 '16 at 23:52
  • If you type su multiple times, you get into a nested su. You also need to exit multiple times then. – LPChip Nov 26 '21 at 18:42
40

Use

su username

to get back to your user level (or a different user)

Or just press Ctrl+D to exit out of root

10
  • logout if used sudo su -
  • exit if used sudo -s
Nakilon
  • 953
2

if your stuck after using sudo su as root: to exit use this command

su -l <user_name>
0

if you get access denied message -for example - while login to mysql with root password like:

mysql -u root -p

try:

sudo mysql -u root -p

for one time elevation of privileges, for multiple commands use:

sudo su

you will notice that your terminal changed to [root@yourpc]# then whatever commands you like to run when you want to return back to your own user hit ctrl + d , type exit or

su username

your terminal will be back to [username@yourpc]# and you are now using you own user permissions

sh.e.salh
  • 101
  • 1
0

There isn't any reason to use sudo or su to run the MySQL command-line client. It defaults to using your current Unix user as your MySQL user, but instead you should pass it the user you want to connect to as arguments:

$ mysql -u root # connect as MySQL's root user (without password)
$ mysql -u root -p # -p means prompt for a password

Hopefully, your MySQL root account has a password, and you'll need to use the second form.

Other than that, if you need to run MySQL under sudo (e.g., for file permissions) then do it like this:

$ sudo -u unix-user mysql -u mysql-user -p

You can leave out the arguments (sudo will default to user root, MySQL will default to using the same user as sudo).

derobert
  • 4,402