5

I'm on FreeBsd 11. I have a user "user123" belonging to the group wheel and wheel has %wheel ALL=(ALL) NOPASSWD: ALL in /usr/local/etc/sudoers.

I don't know the password of root. However, I'm able to run "sudo" without one.

I've installed Postgresql and run it via "service start".

Now I want to log in as the postgresql user and create a database or other stuff:

$ su postgres
Password:


# or

$ su - postgres
Password:

But I don't know the password.

Is this the password of the user root or the user postgres? I don't know any of them. Is there a standard workaround for this?

2
  • 4
    I think your confusion is that you should be using sudo but you are actually using su. Different commands. Commented Apr 21, 2017 at 5:50
  • @roaima, I've tried to explain to him there's no connection between the sudoers file and the su command.
    – Alxs
    Commented Apr 21, 2017 at 18:59

3 Answers 3

18

If you want to login as postgres, and you have sudo access without password requirements, do:

sudo -iu postgres

The -i starts a login shell.

0
1

Caveat: the question suggests you're using a personal server. If not, and you aren't the main admin, do NOT assume root user's shell unless so instructed by the main admin


sudoers: 'wheel group' settings

%wheel ALL=(ALL) NOPASSWD: ALL

So your user123 user is in the 'wheel group' and your sudoers file grants unfettered access across ALL domains as (ALL) users with no password required for ALL commands, to members of the wheel group.

We know you have a working login so your options aren't limited. Knowing the password for postgres or even root, isn't required. You can simply:

$ sudo -iu postgres


su, sudo, sudoers confusion

  1. su = substitute user
  2. sudo = exec command as another user

The reason you can't 'substitute user' with su or su - as shown in the question is because the sudoers file has no effect on the su command.

The su command without arguments or just the dash defaults to root, so you get prompted for the root user password. This is expected behaviour: As the name sudoers implies, the sudoers file grants privilege to the sudo command only.


root shell, lost password

As it appears you've forgotten the root password. You can get the root shell using:

$ sudo -i

Leaving you back in control at: [root@local ~]#

11
  • sudo su - --> su: Sorry
    – Dorion
    Commented Apr 21, 2017 at 2:25
  • @Dorion, sudo su - allows you to switch to the root user account using your own password. If you try to su - or su instead (without the preceding sudo) you will need to know the actual root password.
    – Alxs
    Commented Apr 21, 2017 at 2:32
  • 1
    @Dorion: You should indeed be able to sudo su. Commented Apr 21, 2017 at 4:19
  • 2
    sudo su is an abomination. In this case sudo passwd ... would be sufficient. Commented Apr 21, 2017 at 5:49
  • 4
    This is the wrong solution to this user's problem. They don't need a root shell and resetting the system's root password isn't going to get them closer to their goal (and even it it did, it would be a bad practice anyway). If they did just need a root shell sudo -s or sudo -i would be a better option, sudo su for any reason is a bad idea for several reasons. It may vary depending on your goals but there is always a way with less pitfalls than that.
    – Caleb
    Commented Apr 21, 2017 at 12:05
0

Two steps:

$ sudo -i
# su - postgres

As root, you can use su without password. You can also combine these to

$ sudo su - postgres

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .