Photo by Gabriel Heinzer on Unsplash
Linux Empowerment: Mastering User & Group Management, File Permissions, and Systemctl Commands
User Management
User management in Linux involves various commands and configurations to create, modify, and delete user accounts, as well as managing their permissions and settings.
1.Creating Users:
useradd
: This command is used to add a new user to the system.
sudo useradd -m username
-m
flag ensures that a home directory is created for the user.
2.Setting Passwords:
passwd
: This command is used to set or change a user's password.
sudo passwd username
3.Deleting Users:
userdel
: This command is used to delete a user account from the system.
sudo userdel username
4.Listing Users:
cat /etc/passwd
: This command displays a list of all user accounts on the system.
cat /etc/passwd
Group Management
Group management in Linux involves creating, modifying, and deleting groups to organize users with similar permissions.
Creating Groups:
groupadd
: Use this command to create a new group. For example:sudo groupadd mygroup
Listing Groups:
cat /etc/group
: This command displays a list of all groups on the system.cat /etc/group
Modifying Groups:
groupmod
: Use this command to modify group properties such as group name or GID (Group ID).sudo groupmod -n newgroupname oldgroupname
-n
flag specifies the new name for the group.
Deleting Groups:
groupdel
: Use this command to delete a group from the system. For example:sudo groupdel mygroup
How to add the user to a group
Method 1 :
To add a user to a group in Linux, you can use the usermod
command with the -aG
option, where:
-a
stands for "append," which ensures that the user is added to the group without removing them from any other groups.-G
specifies the group(s) to which the user should be added.
sudo usermod -aG groupname username
Replace groupname
with the name of the group you want to add the user to, and username
with the username of the user you want to add to the group.
Method 2 :
To add a user to a group in Linux is by using the gpasswd
command. This command allows you to manage group passwords and group memberships.
sudo gpasswd -M username groupname
-M
specifies the multiple user
Replace username
with the username of the user you want to add to the group and groupname
with the name of the group you want to add the user to.
Note : This method will overwrite your previous user in the group.
File Permission
In Linux, file permissions determine who can read, write, or execute files and directories. They are represented by a series of letters and symbols associated with each file or directory. Here's a breakdown of how file permissions work:
Owner Permissions: The owner of the file or directory
Owner Permissions: The owner of the file or directory has specific permissions.
Group Permissions: Users who are members of the group associated with the file or directory have these permissions.
Other (or World) Permissions: All other users on the system who are not the owner or members of the group have these permissions.
The permissions themselves are represented by three types of operations:
Read (r): Permission to read the contents of a file or view the contents of a directory.
Write (w): Permission to modify or delete a file, or add, remove, or rename files within a directory.
Execute (x): Permission to execute a file (for programs or scripts) or access the contents of a directory (if granted along with read permission).
In the command line, you can view and modify file permissions using the ls
command with the -l
option to show detailed information about files, including permissions, and the chmod
command to change permissions.
-rw-r--r--
In this example:
The first character (
-
) indicates the type of file. A dash-
represents a regular file.The next three characters (
rw-
) represent the permissions for the owner. The owner has read and write permissions, but not execute permission.The next three characters (
r--
) represent the permissions for the group. The group has only read permission.The last three characters (
r--
) represent the permissions for others. Others also have only read permission.
To change file permissions, you use the chmod
command followed by the desired permission setting and the file or directory name. For example:
chmod u+x filename
This command adds execute permission to the file for the owner (u
). You can also use symbolic or octal notation to modify permissions.
Installation Commands
Update (apt update
):
The apt update
command updates the local package index. This means it retrieves the latest information about available packages from the repositories specified in /etc/apt/sources.list
or in files in /etc/apt/sources.list.d/
.
sudo apt update
This command does not actually upgrade any packages on your system; it simply fetches the latest package lists from the repositories.
Upgrade (apt upgrade
):
The apt upgrade
command, on the other hand, installs the newer versions of the packages currently installed on your system. It does not remove packages or install new packages; it only upgrades the existing ones.
sudo apt upgrade
This command will prompt you to confirm the upgrade and then proceed to upgrade the packages to their latest versions.
Typically, you would run apt update
to update the package lists first, and then apt upgrade
to actually upgrade the packages.
In summary:
apt update
: Updates the local package index to get information on the latest packages available.apt upgrade
: Upgrades the installed packages to their latest available versions.
It's essential to regularly perform both update and upgrade tasks to keep your system secure and up-to-date with the latest software patches and improvements.
apt-get
performs actions such as installing, upgrading, and removing software packages on the system. It interacts with the Advanced Packaging Tool (APT) library and package management system.
Install a Package:
sudo apt-get install package_name
This command installs the specified package and any dependencies it requires.
Remove a Package:
sudo apt-get remove package_name
This command removes the specified package from the system, but it doesn't remove configuration files associated with the package.
Remove a Package Completely:
sudo apt-get purge package_name
This command removes the specified package along with its configuration files.
Update Package Lists:
sudo apt-get update
This command updates the local package index with the latest information from the repositories.
Upgrade Installed Packages:
sudo apt-get upgrade
This command upgrades all installed packages to their latest versions.
Show Information about a Package:
apt-get show package_name
This command displays detailed information about the specified package.
apt-get
provides powerful package management capabilities, but some of its functionality has been integrated into the newer apt
command, which is considered more user-friendly and offers additional features. However, apt-get
remains widely used and supported.
Systemctl commands
systemctl
is a command-line utility in Linux systems that is used to control and manage the systemd system and service manager.systemd is a system and service manager for Linux operating systems, which provides a number of features such as parallelization of system startup, service supervision, and logging.
systemctl
allows users to interact with systemd to start, stop, enable, disable, reload, or check the status of system services, manage system targets (which define system states such as shutdown or multi-user mode), view system logs, and perform other administrative tasks related to system and service management.
Start a Service:
systemctl start servicename
This command starts a specific service. It triggers the initialization and execution of the service's main process.
Stop a Service:
systemctl stop servicename
This command stops a specific service. It halts the execution of the service's main process and any associated sub-processes.
Restart a Service:
systemctl restart servicename
This command stops and then starts a specific service. It is effectively equivalent to running
systemctl stop servicename
followed bysystemctl start servicename
.Reload Configuration of a Service:
systemctl reload servicename
This command reloads the configuration of a specific service without stopping it. It instructs the service to reload its configuration files or settings while continuing to run.
Enable a Service to Start on Boot:
systemctl enable servicename
This command configures a service to start automatically during system boot. It creates symbolic links or other mechanisms to ensure that the service is started during the system initialization process.
Disable a Service from Starting on Boot:
systemctl disable servicename
This command disables a service from starting automatically during system boot. It removes any configurations that cause the service to start during the system initialization process.
Check Status of a Service:
systemctl status servicename
This command shows the status of a specific service, including whether it's running or not. It provides information about the service's current state, its PID (process ID), memory usage, and recent log messages.
List All Loaded Units:
systemctl list-units
This command lists all units (services, sockets, devices, etc.) loaded in the systemd manager. It provides a comprehensive overview of all active units managed by systemd.
List Running Services:
systemctl list-units --type=service
This command lists all running services. It filters the output of
systemctl list-units
to show only units of type "service", providing a focused view of currently active services.List Failed Units:
systemctl --failed
This command lists all units that have failed. It provides a summary of units that encountered errors or failed to start during system operation.
Show Service Dependency Tree:
systemctl list-dependencies servicename
This command displays a tree of units upon which a specific service depends. It illustrates the interdependencies between services, targets, and other units required for the functioning of the specified service.
View Logs of a Service:
journalctl -u servicename
This command shows the logs related to a specific service. It retrieves and displays log messages generated by the specified service, allowing administrators to troubleshoot issues or monitor service activity.