Basic Linux commands for managing users and files

From launching my own websites with virtual private servers and working at a few tech startups over the years, I’ve gained a basic understanding of Linux system administration. In this article I’d like to share with you the commands I use most often when launching a new server in a team environment.

Introduction

This article reviews commands most frequently used by Linux system administrators to manage users and files on various Linux systems. As a Linux system administrator (or sysadmin), your main role is to maintain and secure a computer system. The commands in this article will help you achieve this.

These commands aren’t just useful for Linux sysadmins though. They are useful for anyone interacting with an operating system based on Linux. Memorizing them will help you save a lot of time on system administration tasks.

You can use these commands across most Linux-based operating systems.

User Management Commands

users

The users command displays a list of users currently logged in to the server or workstation. If you’re curious about which team members are currently logged in, or suspicious of an unauthorized user, this is a useful command.

# users

If you are the only user logged in as root, you will receive the simple output of root. If there are other users logged in, their usernames will be listed.

adduser

The adduser command is used to add a new user to the system, or to update an existing user’s information. It creates a ~/home/username directory on the server.

# adduser username

Replace username with the username you want to add.

When prompted, enter and re-enter the password for the user. You can then add the user’s full name, room number, work phone, home phone, and other information. For fields you want to leave blank (like room number), just hit Enter.

useradd

The useradd command is used to add a new user to the system as well. The difference between this command and adduser is that it does not create a new directory. It adds the user to the existing home directory and should only be used for temporary users.

# useradd username

Replace username with the username you want to add.

If you want to add a user to a directory apart from the home directory, you can use the following command.

# useradd -d /project username

Replace project with the name of your project and username with the username you want to add. This command is useful for when you’re working on different team projects and want to organize users.

You can also create a user with a numerical group ID. This is a way to organize users in the home directory if you don’t want multiple project directories.

# useradd -u 180 username

Replace 180 with the numerical ID you want to use and username with the username you want to add.

An expiration date can be added to each user as well. This helps administrators create temporary accounts for a specific period of time.

# useradd -e 2017-02-20 username

userdel

The userdel command deletes a user account and related files. You must be logged in as a root user to run this command.

# userdel username

Putting an -f in front of the username (with a space) removes the user account even if the user is logged in. This can be useful if you have a rogue user.

passwd

The passwd command is used to change the password for you or another user. A normal user can also use this command to change the password for their own system.

To change the password for a user you manage, enter the command followed by their username.

# passwd username

To display the password status of a user, use the following command.

# passwd -S username

This will tell you if the user is not using a password (NP) or if they have a password (P).

If the user doesn’t have a password, you can prompt them to create one using the following command.

# passwd -e username

last

The last command shows users that were most recently logged in. This command is useful for project management if you need to verify that a user was logged in when they said they were.

# last

Group Management Commands

groupadd

The groupadd command creates a new group account.

Creating groups is useful for managing permissions for two or more people who are part of the same team. For instance, if you have a team of support engineers, you could add them to the same group for easy management.

# groupadd support

Replace support with the group name you want.

usermod

After you create a group, you can add users to that group with the following usermod command.

# usermod -a -G support username

Replace support with the group name you want and username with the username you want to add to the group.

groupdel

You can use the groupdel command to delete a group. However, you can’t remove the primary group of an existing user. You have to remove the user before you remove the primary group.

# groupdel support

Replace support with the group name you want to delete.

chown

The chown command is used to change ownership of select files. To see who currently has ownership over files, use the following command.

# ls -ltr

After this, you can change the owner of a file or directory.

# chown username test.txt

This command will give ownership of the text.txt file to the user username. Replace each value appropriately.

You can also change which group has ownership of a file or directory.

# chown :group test.txt

Replace group with the name of the group you want to give ownership to.

Or, you can change both the owner and group of a file or directory with a single command.

# passwd username:group test.txt

chmod

The chmod command is used to change file or directory permissions. You can use it to change the read, write, and execute permission of a particular file using an alphabetical or numerical approach.

With the alphabetical approach, we use the combination of letters.

An owner of a file is represented with u. Users of a file a in a particular group are represented with g. Users not in a file’s group are represented with o. All users on the system are represented with a.

Read, write, and execute are represented with rwx, respectively.

Also, the operator + is used for granting permissions while - is used for removing permissions.

Let’s take a look at some examples.

To grant read, write, and execute permissions for all users for a particular file, use the following command.

# chmod a+rwx test.txt

To grant execute permission to users outside of the file’s group, use the following command.

# chmod o+x test.txt

With the numerical approach we use numbers to change permissions. We can change the permission by using three digits.

Every digit is a combination of 1, 2 and 4, where 1 is for execute, 2 is for write, and 4 is for read. The number 7 is used to give execute, write, and read access.

If you want to give all access to all users, use the following command.

# chmod 777 test.txt

The first digit in 777 is for owner permission, the second for group permission, and the third for other user permission.

For instance, if you want to give the owner of a file read access only, the command would look like this:

# chmod 400 test.txt

File Management Commands

locate

The locate command is used to locate files or directories in a Linux file system.

# locate text.txt

Replace text.txt with the name of the file or directory you want to locate.

mkdir

The mkdir command is used to create a new directory in a Linux file system.

# mkdir test

Replace test with the name of the directory you want to create.

You can also use the -p option to create a directory under an existing parent directory.

# mkdir -p /admin/test

In this example, /admin is the parent directory and /test is the new directory.

rm

The rm command is used to delete (or remove) files and directories. However, with this command, you can only delete empty directories.

To remove a file, use the following command.

# rm test.txt

To remove a directory, use the following command.

# rm -r test

mv

The mv command is used to move files.

# mv test.txt test

This command moves the file test.txt into the /test directory.

Conclusion

In this article we covered the basics of managing users and files with Linux commands. If you want to learn Linux system administration as it extends beyond the basics, grab this book on Amazon.