How do I log out of `sudo su`?

94

26

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 $?

KDecker

Posted 2012-04-05T17:49:12.177

Reputation: 1 657

6Use exit command .. – None – 2012-04-05T17:50:17.873

11exit or a simple Ctrl+D. I remember when I first discovered the latter and my life got ten times simpler :-D . – Daniel Andersson – 2012-04-06T09:42:36.993

1Apart from the good answers below there remains one point: if you need a shell with root permissions on Ubuntu you type sudo -i (and leave it with CTRL+D) – guntbert – 2013-07-13T21:12:06.663

Answers

115

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

Posted 2012-04-05T17:49:12.177

Reputation: 1 274

2@Rob or sudo -s for shell. – Joel Mellon – 2015-09-03T20:11:40.200

Nothing suggested here works – Totty.js – 2016-01-20T23:52:38.307

10sudo su will switch to the root account even if you don't know the root password. – Rob – 2012-04-05T18:19:08.593

2

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 – 2012-04-05T18:22:40.040

@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 – 2013-07-13T21:15:02.257

19

Use

su username

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

Or just press Ctrl-D to exit out of root

UbunTom

Posted 2012-04-05T17:49:12.177

Reputation:

This is the only answer that actually works. – Totty.js – 2016-01-20T23:52:20.047

1@Totty.js Then you're using su wrong. – Fund Monica's Lawsuit – 2018-09-26T17:28:59.153

7you don't want to su deeper... Ctrl-D or exist or logout are all good choices – Ram – 2012-04-05T20:08:50.940

@Ram - you made an important point. But there's a typo in your comment. It should be exit (not exist). – MountainX – 2013-07-13T18:08:30.623

typo indeed @MountainX ... CTRL-D, exit, logout etc. – Ram – 2013-07-13T20:39:38.457

6

  • logout if used sudo su -
  • exit if used sudo -s

Nakilon

Posted 2012-04-05T17:49:12.177

Reputation: 843

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

Posted 2012-04-05T17:49:12.177

Reputation: 3 366