Accelerate Yourself With Advance Linux

Accelerate Yourself With Advance Linux

Β·

12 min read

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:

  1. Keep It Simple: Don't create too many groups or users. Simplicity is key to staying organized.

  2. Regular Updates: Always update your password and avoid sharing it with others. Security matters!

  3. Group Up: Use groups wisely to share files and collaborate effectively with your team.

  4. 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

  1. 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.

  1. 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:

    1. 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. (πŸ‘€πŸ‘₯πŸ‘ͺ)

    2. 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.

    3. 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:

  1. Start a service:

     systemctl start service_name
    
  2. Stop a service:

     systemctl stop service_name
    
  3. Restart a service:

     systemctl restart service_name
    
  4. Enable a service to start on boot:

     systemctl enable service_name
    
  5. Disable a service from starting on boot:

     systemctl disable service_name
    
  6. Check the status of a service:

     systemctl status service_name
    
  7. List all active services:

     systemctl
    
  8. List all services (including inactive ones):

     systemctl list-units --type=service
    
  9. Reload the systemd manager configuration:

     systemctl daemon-reload
    
  10. 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:

  1. 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
  1. 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
  1. 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
  1. 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.

  1. 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:

  1. 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.

  2. 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.

  3. 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.

  4. 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 of sed. 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:

  1. Search and replace:

     sed 's/search_string/replace_string/' input_file
    

    This will search for search_string in each line of input_file and replace it with replace_string.

  2. Search and replace with flags:

     sed 's/search_string/replace_string/gi' input_file
    

    The g flag makes the substitution globally (all occurrences), and the i flag makes the search case-insensitive.

  3. Delete lines matching a pattern:

     sed '/pattern/d' input_file
    

    This will delete all lines that contain pattern.

  4. Print specific lines or ranges:

     sed -n '1,5p' input_file
    

    This will print lines 1 to 5 from input_file.

  5. 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.

  6. 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.

  7. 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 to input_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.

Β