Linux Saviours

Few Basic linux (Ubuntu) stuff that will save you time and get the job done with ease.

Linux and it's programs are by default built to allows users to accomplish tasks easily. But many people dont even know that there are many other useful commands and tools.

Many people know the basics or only what they use more often, but there are more than what a general end user will know/use and what is included in the tool that the users may not be aware. These will make life easier for us. We shall see some of them here.

Help

There are a bunch of tool and places where we can get information and help about a certain tool or a command. Most of the the well build packages/application/tools have these things well documented and put in place as per need. But some custom tools/application/packages may not have it. Some of the things that can help us to get help are

  • man command(Manual) # Man Pages
    • man passwd
  • whatis command
    • whatis passwd
  • help/-help/–help argument
    • passwd -help
  • info command
    • info passwd
  • /usr/share/doc location
    • cat /usr/share/doc/passwd/*

These are the commands and arguments and locations where we can find many useful information about any tool that has proper documentation.

File System Structure

Everything in linux is a file (just like how everything is a object in python) and so these needs to be well defined and well structured to have everything working properly and to have things in the right place.

The "/" (Forward leaning slash) is the start of the file system (Also called as root of the file system). Everything in linux follow this tree structure only. Some most used and important directories are as follows.

  • Home Directories: /root/home/username
  • User Executables: /bin/usr/bin/usr/local/bin
  • System Executables: /sbin/usr/sbin/usr/local/sbin
  • Shared Libraries: /lib/usr/lib/usr/local/lib
  • Kernels & Boot-loaders: /boot
  • Configuration Files: /etc
  • Device Files: /dev
  • Temporary Files: /tmp
  • Other Mount-points: /media/mnt
  • Server Data: /var/srv
  • System Informations: /proc/sys
  • Optional Applications: /opt

Sometimes when installing linux people tend to put different paths in different partitions. Even when it is in different partition’s the file system structure remains the same.

This remains same by mounting it in the path or symlinking the partition in the root filestructure.

File System (FS) is different from this. FS is all about partition scheme and partitioning and its methods and optimization etc.

File(s) and Folder(s) Permission(s)

Every file and folder has default and predefined permissions, they can be changed/removed/revoked etc by few commands. We need to be aware of what are the file and folder permissions to know what is happening and how to access it or why it is not accesible and who can access it.

Yes linux and access control method of users and groups which can control access to a group of files/folders depending on the permission to a user or group. We can have more/less permission even without the group/user having enough permission over a file/folder/. This can be done by anyone who has higher privelages (root).

These can be done by commands like

  • chown (Change Owner)
    • chown <owner name> <file|folder>
    • chown -R immanuel /
sudo /etc/shadow # Will show the list of all owners
  • chmod (Change Mode)
    • chmod <permission> <file|folder>
    • chmod 777 /
WhoPermission
1st Digit: Owner Permission4 Read
2nd Digit: Group Permission2 Write
3rd Digit: Other Permission1 Execute
  • chgrp (Change Group)
    • chgrp <group> <file|folder>
cat /etc/group # Shows all the groups

Tab (Key/Space)

The Tab key is one of the cool keys in keyboard for linux. It helps us in autofill and autcomplete.

Say if you are going to a reposity that has averylongname then you dont have to type it completely. You just type a few characters like avery and then press tab and it will autocomplete it.

If there are more than one of the same patterend names then press the tab key twice to show all the files/folders with the same names then you can choose which one to use.

Tiled (~)

This is used to go to home instantly or to create somethin in home directory.

  • cd ~ (Normally just cd will do the same)
  • cd ~testfolder/testfile

Pipes (|,>,<,>>,&)

Pipes are used in terminal to process or send input/output/error to a files or to the next command.

command < fileSend file as a Input to the command.
command > fileRedirect STDOUT of command to file.
command >> fileAppend STDOUT of command to file.
command 2> fileRedirect STDERR of command to file.
command 2>> fileAppend STDERR of command to file.
command1 | command2Redirecting Output to Program

Text Processin in Terminal

Here’s a description of the text processing in terminal

  • Cut
  • Wc
  • Head
  • Cat
  • Tail

Cut:

  • Purpose: Extracts specific sections from each line of a file or input.
  • Syntax: cut [options] [file]
  • Common options:
    • -f fields: Selects specific fields (columns) based on delimiters.
    • -d delimiter: Specifies the delimiter to use (default is tab).
    • -c characters: Selects specific characters from each line.
  • Example: cut -f 1,3 -d ":" /etc/passwd
    • # Extract first and third fields (username and UID)

WC:

  • Purpose: Counts the number of lines, words, and characters in a file or input.
  • Syntax: wc [options] [file]
  • Common options:
    • -l: Counts lines.
    • -w: Counts words.
    • -c: Counts characters.
  • Example: wc -lw myfile.txt
    • # Count lines and words in myfile.txt

Head:

  • Purpose: Displays the first few lines of a file.
  • Syntax: head [options] [file]
  • Common options:
    • -n number: Specifies the number of lines to display (default is 10).
  • Example:head -n 5 myfile.txt
    • # Show the first 5 lines of myfile.txt

Cat:

  • Purpose: Concatenates files and prints their contents to the standard output.
  • Syntax: cat [options] [file1] [file2]...
  • Common options:
    • -n: Numbers all lines.
    • -e: Displays non-printing characters.
  • Example:cat -n file1.txt file2.txt
    • # Display contents of both files with line numbers

Tail:

  • Purpose: Displays the last few lines of a file.
  • Syntax: tail [options] [file]
  • Common options:
    • -n number: Specifies the number of lines to display (default is 10).
    • -f: Follows the file, displaying new lines as they are added.
  • Example:tail -f mylog.txt
    • # Monitor the log file for new entries

Diff:

  • Purpose: Compares two files and displays their differences. Syntax: diff [options] file1 file2
  • Common options:
    • -u: Produces a unified diff, showing differences in a more readable format.
    • -c: Produces a context diff, showing differences along with surrounding lines.
    • -y: Produces a side-by-side diff, showing files in two columns. 
  • Example: diff -u old_file.txt new_file.txt
    • # Shows differences between the files in a unified format

Grep:

  • Purpose: Searches for lines matching a pattern in a file or input. Syntax: grep [options] pattern [file]
  • Common options:
    • -i: Ignores case distinction.
    • -v: Inverts the match, printing lines that don’t match the pattern.
    • -r: Recursively searches for patterns in all files within a directory.
    • -n: Prints line numbers of matching lines.
  • Example: grep -i “error” log.txt
    • # Finds lines containing “error” (case-insensitive)

Sed:

  • Purpose: Stream editor for modifying text files. Syntax: sed [options] 'command' file
  • Common commands:
    • s/pattern/replacement/: Substitutes pattern with replacement.
    • d: Deletes lines matching a pattern.
    • p: Prints lines matching a pattern.
    • i: Inserts text before a line.
    • a: Appends text after a line.
  • Example: sed ‘s/localhost/myserver.com/g’ config.txt
    • # Replaces “localhost” with “myserver.com” globally

Network Interface Configuration

Purpose: Manages network interfaces on a Linux system.

Tools:

  • ifconfig: Displays information about network interfaces and enables/disables them.
  • system-config-network: Graphical tool for configuring network interfaces (may not be installed by default).
  • Network Configuration Files: Store settings in /etc/sysconfig/network-scripts/ directory.

Key Concepts:

Network Interface Files:

  • Located in /etc/sysconfig/network-scripts/ifcfg-ethX (replace ethX with the interface name).
  • Contain settings in the format VARIABLE=VALUE.
  • Common settings:
    • DEVICE: Interface name.
    • HWADDR: MAC address (optional).
    • BOOTPROTO: DHCP or static IP configuration.
    • IPADDR: IP address (for static configuration).
    • NETMASK: Network mask (for static configuration).
    • GATEWAY: Default gateway (for static configuration).
    • ONBOOT: Whether to bring up the interface at boot.
    • USERCTL: Whether to allow non-root users to control the interface.
    • TYPE: Interface type (e.g., Ethernet).

Global Network Settings:

  • Located in /etc/sysconfig/network.
  • Common settings:
    • NETWORKING: Enables/disables networking.
    • HOSTNAME: System hostname.
    • GATEWAY: Default gateway (can be overridden in interface files).

Enabling/Disabling Interfaces:

  • ifup ethX: Brings up interface ethX.
  • ifdown ethX: Brings down interface ethX.

DNS Configuration:

  • Domain Name Service (DNS) translates hostnames to IP addresses.
  • DNS server addresses specified in /etc/resolv.conf or by DHCP.
  • Common settings:
    • search: Domain names for incomplete hostnames.
    • nameserver: IP addresses of DNS servers.

Local DNS Server Configuration:

  • Uses /etc/resolv.conf.
  • Order of nameserver entries is important (fastest and available servers first).

Process Management

Purpose: Manages processes running on a Linux system.

Key Concepts:

Processes:

  • Sets of instructions loaded into memory.
  • Identified by unique Process IDs (PIDs).
  • Have UIDs, GIDs, and SELinux contexts for filesystem access.
  • Tracked in the /proc filesystem.

Viewing Processes:

  • ps: Lists processes, with options for:
    • All terminals (a)
    • Non-terminal processes (x)
    • User information (u)
    • Parentage (f)
    • Custom output (o)
  • pgrep: Finds processes by predefined patterns.
  • pidof: Finds processes by exact program names.

Process States:

  • Running: Actively using the CPU.
  • Sleeping: In memory but inactive.
  • Uninterruptable Sleep: Waiting for a resource, cannot be woken by signals.
  • Zombie: Terminated but not fully flushed from process list.

Signals:

  • Simple messages sent to processes using commands like kill.
  • Processes respond to signals they’re programmed to recognize.
  • Common signals:
    • HUP (1): Reread configuration files.
    • KILL (9): Terminate immediately.
    • TERM (15): Terminate cleanly.
    • CONT (18): Continue if stopped.
    • STOP (19): Stop process.

Sending Signals:

  • kill: By PID.
  • pkill: By pattern.
  • killall: By name.

Scheduling Priority:

  • Determines how processes share the CPU.
  • Affected by “nice” value (-20 to 19, default 0).
  • Lower nice value = higher priority.

Altering Priority:

  • nice: When starting a process.
  • renice: After process has started (root only can decrease).

Process Management Tools:

  • tophtop: CLI tools for real-time process monitoring and management.
  • gnome-system-monitor: GUI tool for process management.

Alias

Purpose: Creates shortcuts for frequently used commands or combinations of commands.

Syntax:

  • alias [new_name]="[original_command]"

Examples:

  • alias ll="ls -l"
  • alias rm="rm -i" (prompts for confirmation before deletion)
  • alias grep="grep --color=auto" (adds color highlighting to grep output)
  • alias myip="curl ifconfig.me"
  • alias hist="history | grep"

Key Points:

  • Aliases are temporary by default, lasting only for the current shell session.
  • To make aliases permanent, add them to your shell configuration file (~/.bashrc for Bash).
  • Aliases can be nested (aliases within aliases).
  • Use alias by itself to list all defined aliases.
  • Use unalias [alias_name] to remove an alias.

Common Use Cases:

  • Shortening long commands.
  • Adding default options to commands.
  • Creating custom commands for specific tasks.
  • Correcting common typos.
  • Personalizing your shell environment.

Find and Locate

Purpose: Find files and directories on a Linux system.

Key Differences:

Featurefindlocate
Search methodSearches the filesystem in real timeUses a pre-built database for faster searches
AccuracyAlways up-to-dateMight miss recently added files if database is not updated
SpeedSlower for large searchesFaster for most searches
FlexibilityMore options for fine-grained controlLimited options

Find Command:

Syntax:

  • find [path] [expression]

Common Expressions:

  • -name: Find by filename.
  • -type: Find by file type (e.g., f for files, d for directories).
  • -size: Find by file size (e.g., +10M for files larger than 10MB).
  • -mtime: Find by modification time (e.g., -7 for files modified within the last 7 days).
  • -exec: Execute a command on each found file.

Examples:

  • find /home -name "*.txt": Find all text files in /home.
  • find . -type f -size +10M: Find files larger than 10MB in the current directory.
  • find /var/log -mtime +30 -exec rm {} \;: Delete log files older than 30 days.

Locate Command:

Syntax:

  • locate [pattern]

Key Points:

  • Relies on a database updated by updatedb (usually run daily by cron).
  • Use updatedb manually to update the database before using locate.
  • Faster for general searches but might miss recently added files.

Examples:

  • locate "*.jpg": Find all JPEG files.
  • locate "report.pdf": Find a file named “report.pdf”.

Best Practices:

  • Use find for real-time, accurate searches with flexible options.
  • Use locate for quick, general searches when database is up-to-date.
  • Consider using updatedb before locate to ensure best results.

System Logs

Purpose: Centralized storage and management of messages, errors, and debugging information from applications on Red Hat Enterprise Linux systems.

Common Log Files:

  • /var/log/dmesg: Kernel messages from the boot process.
  • /var/log/messages: Standard system log containing messages from system software, non-kernel boot issues, and dmesg output. (Readable by root only.)
  • /var/log/secure: Security-related messages and errors (logins, TCP wrappers, xinetd). (Readable by root only.)
  • /var/log/audit/audit.log: Audited messages from the kernel, including SELinux messages. (Use ausearch and aureport to view.)

Monitoring System Logs:

  • Use text editors or tools like less or tail to view log files.
  • Employ tools like grep to filter specific content.

Generating Log Messages:

  • Use logger(1) to manually generate log messages.

Audit Log Tools:

  • ausearch: Search for specific events in the audit log.
  • aureport: Generate reports based on audit log data.

Example:

  • Generate a log message: logger This is a test message.
  • View the message in /var/log/messagestail -n1 /var/log/messages

Comments

Leave a Reply