List Users in Linux

System Administrators needs to configure and manage users and groups in the system. There are different commands to Add User, Delete User etc. but to list all the users this may be a critical task. In this tutorial, you are going to learn how to list users in Linux.

List all users using /etc/passwd file.

For each user account on system /etc/passwd file contains one line. So you can easily display login information of each user using this file.

Display login information of each user using cat command.

cat passwd

The output should be similar like this:

How to list users in Linux - Cat Command
How to list users in Linux – Cat Command

In the above screenshot each line contains 7 fields delimited by colons which contain the following data:

  1. Username
  2. Encrypted password where x means password is saved in etc/shadow file.
  3. UID (User ID)
  4. GID (User’s Group ID)
  5. Full Name of User
  6. User $HOME directory
  7. Login shell path

By using awk and cut commands you can list only username’s in the file

awk -F: '{ print $1}' /etc/passwd
cut -d: -f1 /etc/passwd

The output should be similar to this:

Output
root
daemon
bin
sys
sync
...
...
...
mail
news
uucp
proxy
www-data
backup

List all users using getent command

Getent command is used to read entries from database. Getent lists users from both LDAP database and /etc/passwd file.

List all users with getent command.

getent passwd
How to List Users in Linux - Getent Command
How to List Users in Linux – Getent Command

You can also print only usernames by using cut and awk command.

getent passwd | cut -d: -f1
getent passwd | awk -F: '{ print $1}'

Checking if user exists in Linux system

You have learned to list users in Linux what if you want to check if a user exists or not. You can do so by using following commands.

There are two ways using getent command to check specific user exists or not. First is using grep command or without grep command. Assume we want to check user john exists or not, then the commands will be:

getent passwd | grep john

and without grep command.

getent passwd john

NOTE: if the user exists it will print user login info otherwise nothing will be printed.

To check the total number of users present in the system use following command.

getent passwd | wc -l

The output should be similar like this:

Output
47

There are 47 users present on the system.

Differentiate System and Normal Users

When installing the Operating System or installing or updating new packages, new users are created in the system.

Normal users are the users created by root user or users created with sudo privileges. Normal users have real login shell and $HOME directory.

Each user has a UID (User ID) either he is system user or normal user. When normal user adds user by using adduser command. User will be added and UID will be assigned automatically with reference of UID_MIN and UID_MAX from file /etc/login.defs.

To check UID_MIN and UID_MAX type:

grep -E '^UID_MIN|^UID_MAX' /etc/login.defs

The output should be:

Output
UID_MIN			 1000
UID_MAX			60000

The above output shows all normal users lied between 1000 and 60000.

Now to print all normal users between 1000 to 60000 enter following command.

getent passwd {1000..60000}

The output should be:

Output
admin:x:1000:1000:admin:/home/rahul:/bin/bash
temp:x:1001:1001:,,,:/home/temp:/bin/bash

To print only usernames type following command

eval getent passwd {$(awk '/^UID_MIN/ {print $2}' /etc/login.defs)..$(awk '/^UID_MAX/ {print $2}' /etc/login.defs)} | cut -d: -f1

List all Groups in Linux

You can list all groups in Linux by using the following command.

getent group

And to list all groups with specific user run following command.

getent group | grep username

Conclusion

You have successfully learned how to List Users in Linux. If you have any queries regarding this please don’t forget to comment below.

Migrate MySQL database between two servers

Many times we need to clone/transfer database from one server to another. Whenever we are moving projects to new server from old server we need to copy files between to servers. SCP (Secure Copy) command is used to transfer files between two servers. In this tutorial you are going to learn to migrate MySQL database between two servers by using SCP command.

Prerequisites

Before you start to migrate MySQL databases between two servers. You must have the non-root user account on both of your servers with sudo privileges.

Migrate Database between two servers

First you will need to backup the database then transfer it remotely to destination server and finally you will restore backup on destination server.

To backup the database enter following command.

sudo mysqldump -u root -p --opt [database_name]> [database_name].sql

Example:

sudo mysqldump -u root -p --opt test_database > test_database.sql

Copy backup file to destination server by using following command.

sudo scp [database_name].sql [user_name]@[server_name]:path/on/destination/

Example:

sudo scp test_database.sql [email protected]:/temp/backup/

Import backup file on destination server.

sudo mysql -u root -p database_name < /path/to/database_name.sql

Example:

sudo mysql -u root -p test_database < /temp/backup/test_database.sql

Useful Commands

To backup database on remote server using port and host. Use following command.

sudo mysqldump -P [port]-h [host]-u [user_name]-p [database_name]> database_name.sql

To backup multiple databases, Use following command

sudo mysqldump -u [user_name]-p --databases [database_name1] [database_name2] > databases.sql

To import multiple databases on destination server.

sudo mysql -u root -p < databases.sql

Backup all the databases, enter following command.

sudo mysqldump -u [user_name]-p --all-databases > databases.sql

When you need to backup specific database tables then use following command.

sudo mysqldump -u [user_name]-p [database_name][table1] [table2] > database_name.sql

Conclusion

You have successfully learned to migrate MySQL databases between two servers by using SCP command. If you have any queries regarding this then dont forget to comment bellow.