-2

Hi want to create a new user in Ubuntu that is same as root who can login using SSH keys, the only difference is the username. (No use of sudo)

I know the risks but it's in my private Network with strict authentication. Thanks ☺️

terdon
  • 100,812
  • 4
    You either give them SSH access to the root account directly (NOT RECOMMENDED!) or you give them sudo access. There is no way to directly 'replicate' the root user and its permissions to a non-root user – Thomas Ward Oct 02 '21 at 03:19
  • 1
    @ThomasWard what if you create a new user with UID 0? – terdon Oct 02 '21 at 12:04
  • Can you explain why you don't just use the root user? Why do you need a second root account? – terdon Oct 02 '21 at 12:05
  • @terdon it can cause some odd issues. And can be problematic in the long term for tracking users by uid. – Thomas Ward Oct 02 '21 at 18:50
  • 2
    @ThomasWard oh, it's a bad idea for all sorts of reasons, no argument there. I just meant that it is possible (sorta): you can both change the name of the root user and you can create a second one with the same UID. You should not, but you can. – terdon Oct 02 '21 at 19:36

2 Answers2

4

You can actually do this, although I can't imagine any reason why you would want to. You can create a new user with the same user ID (UID) as root. Note that this isn't actually a "new" user, it is just a different user name for the same user. However, you will be able to log in using this name instead of root.

First, create a new user and set their UID to 0, the UID of the root account:

useradd -d /fool -g root -m -N -o -u 0 -s /bin/bash fool 

The options used are:

  • -d /fool : set this user's $HOME to /fool.
  • -g root : set this user's default group to root.
  • -m: create the user's home directory if it does not exist.
  • -N: do not create a group with the same name as the user, just add the user to the group specified by -g.
  • -o: allow the creation of a user with the same UID as another, existing user.
  • -u 0: set this user's UID to 0 (same as root).
  • -s /bin/bash: set the user's default login shell to bash.
  • fool: the user name will be fool.

After running this command, you will be able to log in as fool:

terdon@ub20:~$ sudo -iu fool
root@ub20:~# whoami
root
root@ub20:~# cd
root@ub20:~# pwd
/fool

As you can see, I have logged in as fool, but whoami (which is based on the UID) sees me as root, while cd will take me to /fool. I have all the rights of the root user, because I am the root user, but my user name and home directories are different. You can now proceed to allow root access over ssh (at your own peril) and log in as fool instead of root. This is all cosmetic, you're still really logging in as root, but that's what you seem to want.

terdon
  • 100,812
2

From a safety perspective, what you want to do obviously is not done and highly recommended against.

Still, it is possible in linux to have different user names representing the same user. So if you want to set up a user name that can act fully as root, you can in principle make a different login name for the root user. One of the reasons why it is extremely imprudent to log in as root by default is that human error easily can cause breakage of the entire system.

vanadium
  • 88,010