bash - Can you delete all users but specified accounts in Ubuntu?

07
2014-07
  • Dylan

    I am trying to find a way to quickly delete all user accounts except for the ones I specify in Ubuntu. Is there a good way of doing this? Perhaps a bash script that could do this function? I searched around and didn't really see anything.

    I am doing this as a way to harden a system I am getting ready to take control over so I can remove all of the users except the ones I specify. There are several users.

  • Answers
  • John1024

    Put the names of the accounts that you want to keep, one name per line, in the file keepers and run:

    cut -d: -f1 /etc/passwd | grep -vFf keepers | while read name ; do deluser "$name" ; done
    

    The above uses cut to get all the current user names from /etc/passwd. The command grep -vFf keepers removes from that list all names except the ones in listed keepers. Note that there are many system users that you will need to keep such as root, daemon, sys, mail, lp, etc. The do while loop deletes each name.

    deluser has many options. See man deluser.

    You might want to backup up your /etc/passwd, /etc/group, and other files before doing this just to be safe.

    For the cautious, a two step approach

    The first step creates a file with list of all accounts to be removed:

    cut -d: -f1 /etc/passwd | grep -vFf keepers >goners
    

    The file goners should be carefully inspected to assure that no important accounts are in it. It may be edited by hand if desired. Then run:

    while read name ; do deluser "$name" ; done <goners
    

  • Related Question

    How to set my Ubuntu account to super user at all times?
  • Questioner

    I have the latest Ubuntu installed and I'll be the only one using it off the network. My question is: how can I make myself super user at all times?

    Because when I try to delete a file it says I don't have privileges to do so. I know you are going to say it's a security risk but I'm off the network and want to learn all that I can. I don't want to delete the files through the terminal but want to do it through the user interface/explorer. I've installed LAMP and can't copy my site to the www directory. I've tried to remove the preinstalled index file and it won't let me.


  • Related Answers
  • MDMarra

    I'm off the network and want to learn all that I can

    If you really want to learn all that you can, you should be doing things the proper way. Lesson 1 is never log in as root directly. Lesson two is how to use the terminal. If you're not willing to learn how to use the terminal then maybe WAMP is more appropriate for you than LAMP. Linux is a very powerful operating system, but it is not Windows and trying to treat it as such is dangerous.

  • Pascal Thivent

    Use sudo (or gskudo for graphical applications) to run commands with super user privileges (at your own risks).

    (EDIT: As people mentioned in comments, it's not possible by default to login as root as root doesn't have a password, which is a good thing, and I'm not going to tell you how to change this as I consider setting a password for root as a huge security flaw and a very bad practice. There is actually no reason to be permanently logged a root, this can only lead to mistakes and disasters. Just DON'T DO IT.)

  • mac

    As already reiterated to the boredom, staying logged as superuser all the time is a very bad idea. That said, it's your computer, your project, your choice, so I don't see why one should not share the information you asked, so...

    Login with your normal user and from a prompt shell you can enter:

    sudo passwd root
    

    this will allow you to enter a password for your root user. You can now logout and login again using your root user.

    Should you change your idea about using root, then you could still implement a drag&drop mechanism for super-user operations. Have a look to the ubuntu wiki!

  • 8088

    Why don't you just Alt + F2 and type:

    gksudo nautilus
    

    Enter your password once and use nautilus as a superuser - don't close it. You can also use Ctrl + T or Ctrl + N to open tabs or new windows without re-entering your password.

  • mathepic

    There is a reason Ubuntu disables this and gives you "sudo" instead.

    Doing this is a compromise to your system, and being lazy is not enough reason.

  • Oliver Ridgway

    Despite all the judging comments here I think people should be able to decide for themselves.

    The ubuntu wiki explains how to access the root account, and alternatives.

    https://help.ubuntu.com/community/RootSudo