Table of contents
In a previous blog, we saw some information and basic commands of Linux. In this blog, we will see some advanced parts of Linux.
π©βπ»Users and Groups Management
π€ User: A user account is a systematic approach to tracking and monitoring the usage of system resources. each user account contains two unique identifiers i.e. username and UID. when the user account is created, its username is mapped to a unique UID. username is flexible. it can be changed as per requirement but UID is fixed. it cannot be changed. username is used by the user but UID is used by the system. the /etc/passwd file stores user information.
π₯ Groups: Groups are collections of users. Creating and managing groups is one of the simplest ways to deal with multiple users simultaneously, especially when dealing with permissions. users are placed in various groups based on their roles and functions. The /etc/group file stores group information and is the default configuration file. when the user is created group with the same is also created by default.
Types of Users
Root User (Super User)
This is the main account in the Linux system. it is automatically created during installation. it can do any task and can access any service in the system because it has all permission of the system.
Service User
Service accounts are created by installation packages when they are installed. these accounts are used by services to run processes and execute functions.
Regular User
This is the normal user account. during installation, one regular user account is created automatically. after the installation, we can create as many as we need.
Commands
Creating a User:
useradd username
Changing Password:
passwd username
Creating a Group:
groupadd groupname
Adding User to a Group:
usermod -aG groupname username
To delete a User:
userdel -r username
Remove user from Group:
userdel username groupname
Adding multiple users in a group:
gpasswd -M usernames groupname
π Supercharge Your Linux Experience! πͺ
Now that you've unlocked the secret of Linux users and groups, you can supercharge your productivity and security. π Here are a few tips:
Keep It Simple: Don't create too many groups or users. Simplicity is key to staying organized.
Regular Updates: Always update your password and avoid sharing it with others. Security matters!
Group Up: Use groups wisely to share files and collaborate effectively with your team.
Explore More: Linux has countless exciting features and possibilities. Don't be afraid to explore and learn more!
File Permissions
In the Linux operating system, files and directories have specific permissions that determine who can access, modify, or execute them. These permissions play a vital role in keeping your data secure and organized. Think of them as virtual "locks" on your files and folders. Let's break it down step by step, using emojis to make it even more enjoyable! π
π Directories and π Files
First, let's understand the basic difference between directories and files. π Directories (also known as folders) are like containers that hold other files and directories inside them, while π files contain data, like documents, images, or music. The ls -l
command in Linux displays a detailed listing of files and directories in the current directory. Here's an example of sample output:
-rw-r--r-- 1 user users 4096 Aug 2 10:30 file1.txt
drwxr-xr-x 2 user users 4096 Aug 2 10:30 folder1
-rwxr-xr-- 1 user users 2048 Aug 2 10:30 script.sh
In the above output first character represent whether it's a file or directory. if the first character is - then it's a file and if the first character is d then it's a directory. the next 3 characters represent the user's permissions and middle 3 characters represent the group's permissions and last 3 characters represent other permissions.
ποΈ Three Main Permissions
Now, let's talk about the three main permissions in Linux:
π Read - The ability to view the content of a file or list the contents of a directory.
βοΈ Write - The power to modify or delete a file or create, delete, or rename files within a directory.
β‘οΈ Execute - The authority to run a program file/shell file or enter a directory.
π₯ Users, Groups, and Others
Linux organizes users into three categories for file permissions:
π₯ User: The owner of the file or directory.
π₯ Group: A collection of users with similar permissions. Files can belong to a specific group.
π₯ Others: Anyone else who is not the user or part of the group.
π§ Types of Permissions
There are two types of file permissions in Linux
- Absolute Mode (Numeric Mode)
File permissions can also be represented using numeric mode, also known as octal notation. The numeric mode uses three digits to represent the three sets of permissions (read, write, and execute) for the owner, group, and others, respectively.
Each digit corresponds to a set of permissions, and the values are as follows:
4: Read permission (π)
2: Write permission (π)
1: Execute permission (πββοΈ)
To calculate the numeric mode, you simply add the values for each permission type that you want to set. For example:
7 (4+2+1) means read, write, and execute permissions (πππββοΈ) for the owner.
5 (4+1) means read and execute permissions (ππββοΈ) for the group.
4 (4) means only read permission (π) for others.
So, a file with numeric mode "755" would have read, write, and execute permissions (πππββοΈ) for the owner, and read and execute permissions (ππββοΈ) for both the group and others.
Using numeric mode can be helpful when setting permissions using the chmod
command, as you can specify the permissions directly without the need for symbolic notation. For example:
chmod 755 filename
This would set the permissions of "filename" to "755" as described above.
a numeric mode is a convenient way to manage file permissions in Linux and can be particularly useful when dealing with scripts or automation tasks.
Symbolic Mode
File permissions can also be represented using symbolic mode. Symbolic mode is a more human-readable way of expressing file permissions, using letters and symbols to represent the permissions for the owner, group, and others.
The symbolic mode consists of three parts:
Who the permissions are applied to:
u
: Represents the owner of the file. (π€)g
: Represents the group that the file belongs to. (π₯)o
: Represents others, which includes everyone else. (πͺ)a
: Represents all, meaning both the owner, group, and others. (π€π₯πͺ)
The Operation to be performed on the permissions:
+
: Adds the specified permissions.-
: Removes the specified permissions.=
: Sets the permissions exactly as specified, overriding any previous permissions.
Permission types:
r
: Read permission (π)w
: Write permission (π)x
: Execute permission (πββοΈ)X
: Special execute permission for directories and files with the executable bit already set.
With symbolic mode, you can use these components to modify the file permissions. For example:
To add write (π) permission for the owner of the file:
chmod u+w myfile.txt
To remove execute (πββοΈ) permission for others:
chmod o-x myfile.txt
To set read (π) and execute (πββοΈ) permissions for all users:
chmod a=rx myfile.txt
You can also use multiple permissions at once or change permissions for multiple users. For instance:
- To give read (π) and execute (πββοΈ) permissions to the owner and group of the file:
chmod ug+rx myfile.txt
Symbolic mode provides a flexible and easy-to-understand way to manage file permissions in Linux. It allows you to modify permissions on individual files or directories with precision. When combined with the chmod
command, the symbolic mode is a powerful tool for controlling access to your files and ensuring the security and integrity of your system. π‘οΈπ
Systemctl Command
systemctl
is a command-line tool used in Linux systems with systemd, which is a system and service manager that has become the default init system in many modern Linux distributions. systemctl
allows users to manage various aspects of the system and its services, such as starting, stopping, enabling, disabling, restarting, and checking the status of services.
Here are some common systemctl
commands:
Start a service:
systemctl start service_name
Stop a service:
systemctl stop service_name
Restart a service:
systemctl restart service_name
Enable a service to start on boot:
systemctl enable service_name
Disable a service from starting on boot:
systemctl disable service_name
Check the status of a service:
systemctl status service_name
List all active services:
systemctl
List all services (including inactive ones):
systemctl list-units --type=service
Reload the systemd manager configuration:
systemctl daemon-reload
View the service unit's details:
systemctl show service_name
SSH/Secure Shell
SSH (Secure Shell) is a cryptographic network protocol used to securely access and manage remote machines over an unsecured network, such as the Internet. It provides a secure channel for communication between the client and the server, allowing users to execute commands remotely and transfer files securely.
Here's a step-by-step guide on how to use SSH in Linux:
Install OpenSSH (if not already installed): Most modern Linux distributions come with OpenSSH pre-installed. However, if it's not installed on your system, you can install it using the package manager specific to your Linux distribution. For example,
On Debian/Ubuntu-based systems, you can use:
sudo apt-get install openssh-server
On Red Hat/CentOS-based systems:
sudo yum install openssh-server
- Start the SSH service: After installation, make sure the SSH service is running. The service name may differ based on your distribution. On most systems, it's called
sshd
. To start the SSH service, use:
systemctl start sshd
If you want to enable the SSH service to start automatically on boot:
sudo systemctl enable sshd
- Connect to a remote server: To connect to a remote server, use the
ssh
command followed by the username and the remote server's IP address or hostname:
ssh username@remote_server_ip
For example, if your username is "john" and the server IP is "192.168.1.100," the command would be:
ssh john@192.168.1.100
- Authentication: Upon your first connection to a remote server, SSH will ask you to verify the authenticity of the server by presenting its fingerprint. If the fingerprint matches, type "yes" to continue.
Next, you'll be prompted to enter your password for the remote server's user account.
Alternatively, you can use SSH key-based authentication, which is more secure and convenient. To set up SSH key authentication, you can follow these steps:
- Generate an SSH key pair on your local machine if you haven't already:
ssh-keygen
- Copy the public key to the remote server:
ssh-copy-id username@remote_server_ip
Now, you should be able to log in without entering a password, as long as your private key is accessible and protected on your local machine.
- Closing the SSH session: To close the SSH session and return to your local shell, type:
exit
That's it! You can now securely manage your remote Linux server using SSH. Remember to keep your SSH keys and server configurations secure to maintain a safe connection.
It's important to note that using some of the above commands typically requires administrative privileges (root access or using sudo means super user do). For example, to start a service, you might need to use sudo systemctl start servic_name
or to add user you might need to use sudo useradd username
awk Command
awk
is a versatile text-processing tool used in Linux and Unix-like operating systems. It allows you to manipulate text files and perform various operations on structured data using patterns and actions. The basic syntax of the awk
command is as follows:
awk 'pattern { action }' input_file
Here's a breakdown of the components:
awk
: The command itself.'pattern'
: A condition or pattern to match against each line of the input file.{ action }
: The action to be performed if the pattern is matched.input_file
: The name of the input file to be processed. If omitted,awk
reads from standard input (usually from the terminal).
awk
processes each line of the input file and checks if the specified pattern is present. If the pattern matches, it performs the associated action. If no pattern is provided, the action is executed for every line.
Some common use cases of awk
include:
Print specific columns from a file:
awk '{ print $1, $3 }' input_file
This will print the first and third columns of each line in
input_file
.Filter data based on a condition:
awk '$3 > 50 { print $1, $2 }' input_file
This will print the first and second columns of lines where the value in the third column is greater than 50.
Calculate and summarize data:
awk '{ total += $1 } END { print "Sum:", total }' input_file
This will calculate the sum of the values in the first column and print the result at the end.
Using a custom field separator (delimiter):
awk -F',' '{ print $2 }' input_file
This will use a comma as the field separator and print the second column.
awk
is a powerful tool that can handle more complex operations as well, and it's often used in combination with other shell commands like grep
, sort
, and sed
for more advanced text processing tasks.
sed Command
sed
(stream editor) is another powerful text-processing tool used in Linux and Unix-like operating systems. It is designed to perform text transformations on an input stream (a file or input from a pipeline) and can be used for various editing tasks, such as search and replace, insertion, deletion, and more. The basic syntax of the sed
command is as follows:
arduinoCopy codesed OPTIONS 'expression' input_file
Here's a breakdown of the components:
sed
: The command itself.OPTIONS
: Optional flags that modify the behavior ofsed
. Some common options include-i
for in-place editing (modify the file directly),-e
for specifying multiple expressions, and-n
for suppressing default output (only print explicitly requested output).'expression'
: The sed expression that defines the operation to be performed on each line of the input.input_file
: The name of the input file to be processed. If omitted,sed
reads from standard input (usually from the terminal).
Some common sed
commands include:
Search and replace:
sed 's/search_string/replace_string/' input_file
This will search for
search_string
in each line ofinput_file
and replace it withreplace_string
.Search and replace with flags:
sed 's/search_string/replace_string/gi' input_file
The
g
flag makes the substitution globally (all occurrences), and thei
flag makes the search case-insensitive.Delete lines matching a pattern:
sed '/pattern/d' input_file
This will delete all lines that contain
pattern
.Print specific lines or ranges:
sed -n '1,5p' input_file
This will print lines 1 to 5 from
input_file
.Insert a new line before or after a pattern:
sed '/pattern/iNew line before pattern' input_file
This will insert a new line before the lines containing
pattern
.Append a new line after a pattern:
sed '/pattern/aNew line after pattern' input_file
This will append a new line after the lines containing
pattern
.Using a file for replacements:
sed -f script_file input_file
You can store
sed
commands in a file (e.g.,script_file
) and apply them toinput_file
.
sed
is a powerful tool, and its capabilities can be expanded with more complex expressions and regular expressions. It is commonly used in shell scripts and one-liners for text processing tasks.