English

Linux File Editor

  • A text editor is a software program that allows users to create, view, and modify text files. In the context of Linux, text editors are crucial tools for interacting with configuration files, scripts, and other text-based documents. Here are some common text editors on Linux systems:
    • vi(Visual editor) - A widely used text editor with two modes - command mode and insert mode.
    • ed(Standard line editor) - A line-oriented text editor.
    • ex(Extended line editor) - An extended version of ed.
    • emacs(Full-screen editor) - A powerful, extensible text editor with a wide range of features.
    • pico(Beginner’s editor) - A simple and user-friendly text editor.
    • vim(Vi Improved) - An enhanced version of vi.

We use vi in our context, and here's an introduction to its commands:

Introduction to vi Editor

The vi editor is a powerful tool with various commands for efficient text editing:

1. Basic Commands:

a. Opening vi Editor: To open a file in the vi editor, open a terminal and type vi filename where "filename" is the name of the file.

b. Modes in vi:

  • Command Mode: The default mode when you open a file. Used for navigation and executing commands.
  • Insert Mode: To enter this mode, press i in command mode. Used for adding or editing text.

c. Entering Text: While in command mode, press i to enter insert mode. Type the desired text.

d. Replacing Characters: In command mode, press r. Enter the replacement character.

e. Deleting Text: In command mode, press d. For example, dd deletes the entire line.

f. Exiting and Saving: Press Esc to exit any mode and return to command mode. Use :q! to quit without saving. Use :wq! to save and quit.

2. Navigating the File:

a. Basic Movement: In command mode, use h, j, k, l for left, down, up, and right movement.

b. Moving Through the File: Use 0 to move to the beginning of a line and $ to move to the end.

c. Moving to Specific Lines: In command mode, use :line_number to move to a specific line.

d. Moving to the End or Beginning: Use G to move to the end of the file and gg to move to the beginning.

3. Finding and Replacing:

a. Searching Forward: In command mode, use /search_term to search forward.

b. Advanced Editing: Combine commands for advanced tasks, e.g., deleting a word (dw).

4. Copying and Pasting:

a. Copying: In command mode, use yy to yank (copy) a line.

b. Pasting: Use p to paste the copied text after the cursor. Use P to paste before the cursor.

Difference Between vi and vim Editor

Both vi and vim editors function similarly, but vim includes additional features. Learning vim is recommended because it encompasses all vi features with valuable additions. Vim offers a comprehensive help system and customization options.

"vim" Interactive Learning Tools

"sed" Command

The "sed" command has various applications:

  • Replace a string in a file with a new string
  • Find and delete a line
  • Remove empty lines
  • Remove the first or n lines in a file
  • Replace tabs with spaces
  • Show defined lines from a file
  • Substitute within the vi editor
  • And much more...

User Account Management Commands

User account management involves commands like useradd, groupadd, userdel, groupdel, and usermod. Key files include /etc/passwd, /etc/group, and /etc/shadow. An example user creation command is:

useradd –g superheros –s /bin/bash –c “user description” –m –d /home/spiderman spiderman

The /etc/login.def File

The /etc/login.def file, along with the chage command, allows per-user settings. Example usage of chage:

chage [-m mindays] [-M maxdays] [-d lastday] [-I inactive] [-E expiredate] [-W warndays] user

File: /etc/login.def

  • PASS_MAX_DAYS 99999
  • PASS_MIN_DAYS 0
  • PASS_MIN_LEN 5
  • PASS_WARN_AGE 7

Switch Users and sudo Access

Commands include su – username, sudo command, and visudo.

Terminal

  • Lines: 3
  • Encoding: UTF-8
  • Monitor Users with commands like who, last, w, finger, id.

Talking to Users

Use commands like users, wall, and write for communication.

Linux Account Authentication

Different types of accounts include local accounts, domain/directory accounts, and client-server account authentication. Windows uses Active Directory, while Linux may use LDAP.

Difference between Active Directory, LDAP, IDM, WinBIND, OpenLDAP, etc.

  • Active Directory: Microsoft
  • IDM: Identity Manager
  • WinBIND: Used in Linux to communicate with Windows (Samba)
  • OpenLDAP: Open source
  • LDAP: Lightweight Directory Access Protocol
  • Others: IBM Directory Server, JumpCloud, etc.

System Utility Commands

Various system utility commands include date, uptime, hostname, uname, which, cal, bc.

Processes and Jobs

Understand concepts like application, script, process, daemon, threads, and jobs.

Process/Services Commands

Use commands like systemctl or service, ps, top, kill, crontab, and at.

systemctl command

  • systemctl command is a new tool to control system services, available in version 7 and later. It replaces the service command
  • Usage example:
    systemctl start|stop|status servicename.service (firewalld)
    systemctl enable servicename.service
    systemctl restart|reload servicename.service
    systemctl list-units --all
    

"ps" command

  • ps command stands for process status and displays all currently running processes in the Linux system
  • Usage examples:
    • ps = Shows the processes of the current shell
    • ps –e = Shows all running processes
    • ps aux = Shows all running processes in BSD format
    • ps –ef = Shows all running processes in full format listing (Most commonly used)
    • ps –u username = Shows all processes by username

"top" Command

The "top" command offers real-time insights into Linux processes, providing a dynamic view of the running system. Its usage is straightforward:

  • top: Initiates the real-time process monitoring interface.

"kill" Command

The "kill" command serves the purpose of terminating processes manually. By sending a signal, it effectively terminates or kills a specific process or group of processes. Usage involves:

  • kill [OPTION] [PID]: Terminates the process identified by the specified process ID.

"crontab" Command

The "crontab" command facilitates the scheduling of tasks. Common usage includes:

  • crontab -e: Opens the crontab file for editing.
  • crontab -l: Lists the current crontab entries.
  • crontab -r: Removes the current user's crontab entries.

"at" Command

Similar to "crontab," the "at" command allows scheduling jobs but for a one-time execution. Usage entails:

  • at HH:MM PM: Schedules a job to run at the specified time.
  • atq: Lists the pending at jobs.
  • atrm #: Removes a specific at job identified by its job number.

Additional Cron Jobs

The Linux system includes default cron jobs categorized as hourly, daily, weekly, and monthly. These are configured in:

  • /etc/cron.___ (directory)
  • For hourly jobs: /etc/cron.d/0hourly
    • /etc/cron.d/0hourly

Process Management

  • Background processes: Utilize Ctrl-z, jobs, and bg.
  • Foreground processes: Employ fg.
  • Running processes after exit: Use nohup process & or nohup process > /dev/null 2>&1 &.
  • Killing a process by name: Utilize pkill.
  • Adjusting process priority: Utilize nice (e.g., nice -n 5 process).
  • Process monitoring: Rely on the top command.
  • Listing processes: Utilize ps.

System Monitoring

  • Key commands include top, df, dmesg, iostat 1, netstat, free, cat /proc/cpuinfo, and cat /proc/meminfo.

Log Monitoring

Monitor logs in the /var/log directory for critical information related to boot, NTP, cron, mail, security, messages, and Apache HTTP server (httpd).

System Maintenance Commands

Perform essential system maintenance tasks using commands like shutdown, init 0-7, reboot, and halt.

Changing System Hostname

Adjusting the system hostname can be achieved through commands like:

  • hostnamectl --set-hostname newhostname
  • For Version 7: Edit /etc/hostname
  • For Version 6: Edit /etc/sysconfig/network

Finding System Information

Retrieve system information using commands like cat /etc/redhat-release, uname -a, and dmidecode.

System Architecture

Differentiate between 32-bit and 64-bit CPUs, checking architecture using arch (Linux) or accessing system properties on Windows.

Terminal Control Keys

Master terminal control using keys like CTRL-u to erase the command line, CTRL-c to stop/kill a command, CTRL-z to suspend a command, and CTRL-d to exit an interactive program.

Terminal Commands

Execute essential terminal commands such as clear, exit, and script.

Recover Root Password

Regain access to the root password by restarting the computer, editing the GRUB menu, changing the password, and rebooting.

SOS Report

Generate a diagnostic and support data report using the sosreport command.

Environment Variables

Environment variables are settings that determine how processes behave in a Linux system.

  • View all environment variables:

    • Command: printenv OR env
    • Explanation: This command shows a list of all the environment variables Environment variables are settings that determine how processes behave in a Linux system. These variables contain information about the system configuration and user preferences.
  • View ONE environment variable: To view the value of a specific environment variable, you can use the echo command followed by the variable's name preceded by a dollar sign. Eg:

    • Command: echo $SHELL

Environment variables are like placeholders that store information about your system or preferences, and $SHELL holds the path to your default shell.

  • Set environment variables: Environment variables can be used to store information that programs or scripts might need to reference. You can set an environment variable temporarily within a session using the export command. This is handy for testing or setting variables for a specific task.

    • Command: export TEST=1
    • Explanation: This command creates a new environment variable named TEST and sets its value to 1.
  • Set environment variable permanently:

    • Command: vi .bashrc
    • Explanation: This opens the .bashrc file, a script that runs every time you open a new terminal. You can add export TEST=1 to this file to make the TEST variable available every time you start a new terminal session.
  • Set global environment variable permanently:

    • Command: vi /etc/profile or vi /etc/bashrc
    • Explanation: This involves modifying system-wide configuration files. By adding export TEST=1 to /etc/profile or /etc/bashrc, you set the TEST variable globally for all users on the system.

Special Permissions with setuid, setgid, and Sticky Bit

  • setuid (Set User ID):

    • Explanation: When the setuid bit is set on an executable file, the program runs with the privileges of the file owner, regardless of who executes it. This is often used for programs that need special permissions, and only the owner (or root) can set the setuid bit.
  • setgid (Set Group ID):

    • Explanation: Similar to setuid, the setgid bit on an executable file makes the program run with the group permissions of the file owner. This is useful when multiple users need access to a group's resources without having the same level of overall system access.
  • Sticky Bit: The sticky bit is a permission that can be set on directories. When the sticky bit is set on a directory, only the owner of a file in that directory (or the root user) can delete or rename the file, even if other users have write permissions to the directory. This is commonly used on directories like /tmp to ensure users can only remove their own files.

Note: chmod is a command used to change permissions on files and directories, including setting setuid, setgid, and sticky bits. It's a crucial command for managing file permissions in Linux.

0
0
0
0