Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
67 views

Basic Commands Linux-1

The document provides a summary of basic Linux commands for file operations, including: 1. Commands for listing, changing directories, printing the working directory, creating/removing directories, copying/moving files, and setting file permissions. 2. Commands for viewing file contents like cat, more, less, head, tail, and grep. As well as editing files using vi, nano, emacs, sed, and awk. 3. Additional commands for compressing/archiving files with tar, gzip, zip, and unzip. The document explains the basic usage of each command and provides examples.

Uploaded by

ASURA KAAL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
67 views

Basic Commands Linux-1

The document provides a summary of basic Linux commands for file operations, including: 1. Commands for listing, changing directories, printing the working directory, creating/removing directories, copying/moving files, and setting file permissions. 2. Commands for viewing file contents like cat, more, less, head, tail, and grep. As well as editing files using vi, nano, emacs, sed, and awk. 3. Additional commands for compressing/archiving files with tar, gzip, zip, and unzip. The document explains the basic usage of each command and provides examples.

Uploaded by

ASURA KAAL
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

BASIC COMMANDS LINUX

Here is a list of Linux commands for file operations:

A. FILE OPERATIONAL

1. List Files and Directories:


Here is a list of Linux commands to list files and directories:

1. `ls`: List files and directories in the current directory.


2. `ls -l`: List files and directories in long format with detailed information.
3. `ls -a`: List all files and directories, including hidden ones.
4. `ls -h`: List files and directories with human-readable file sizes.
5. `ls -t`: List files and directories sorted by modification time.
6. `ls -R`: List files and directories recursively.
7. `ls -S`: List files and directories sorted by size.
8. `ls -i`: List files and directories with their inode numbers.
9. `ls -F`: Append indicators (like `/` for directories) to file and directory names.
10. `ls -d */`: List only directories in the current directory.
11. `ls -l <directory>`: List files and directories in the specified directory in long format.
12. `ls -l <directory>/*`: List files and directories inside the specified directory in long format.

You can combine multiple options to achieve the desired output. For example, `ls -al` will list all files and directories in the current
directory in long format, including hidden ones.

Remember to replace `<directory>` with the actual directory path you want to list.

These commands provide various options to customize the listing of files and directories in Linux. Experiment with them to find the
format that suits your needs.

2. Change Directory:
Here is a list of Linux commands to change directories:

1. `cd`: Change to the home directory.


2. `cd <directory>`: Change to a specific directory. Replace `<directory>` with the actual directory name or path.
3. `cd ..`: Change to the parent directory.
4. `cd -`: Change to the previous directory.
5. `cd ~`: Change to the home directory.
6. `cd /`: Change to the root directory.
7. `cd /<directory>/<subdirectory>`: Change to a directory by providing the full path.

Note: In Linux, the tilde (`~`) represents the home directory of the current user.

Examples:
- To change to the `/var/log` directory, use the command: `cd /var/log`
- To change to the previous directory, use the command: `cd -`

These commands allow you to navigate through the directory structure in Linux. Remember to provide the correct directory name or
path for successful navigation.

3. Print Working Directory:


- `pwd`: Print the current working directory.

4. Create Directory:
- `mkdir <directory>`: Create a new directory.
5. Remove Files and Directories:
- `rm <file>`: Remove a file.
- `rm -r <directory>`: Remove a directory and its contents recursively.
- `rm -f <file>`: Forcefully remove a file without prompting for confirmation.
- `rm -rf <directory>`: Forcefully remove a directory and its contents without prompting for confirmation.

6. Copy Files and Directories:


- `cp <source> <destination>`: Copy a file.
- `cp -r <source> <destination>`: Copy a directory and its contents recursively.

7. Move or Rename Files and Directories:


- `mv <source> <destination>`: Move or rename a file or directory.

8. File Permissions:
- `chmod <permissions> <file>`: Change the permissions of a file.
- `chown <user> <file>`: Change the owner of a file.
- `chgrp <group> <file>`: Change the group ownership of a file.
Here is a list of Linux commands for managing file permissions:

1. `chmod <permissions> <file>`: Change the permissions of a file.


- The `<permissions>` parameter specifies the new permissions to be set. It can be represented as a three-digit octal number (e.g.,
755) or symbolic notation (e.g., u+rwx).
- The `<file>` parameter represents the file for which permissions need to be changed.

2. `chown <user> <file>`: Change the owner of a file.


- The `<user>` parameter specifies the new owner of the file.
- The `<file>` parameter represents the file for which the owner needs to be changed.

3. `chgrp <group> <file>`: Change the group ownership of a file.


- The `<group>` parameter specifies the new group ownership of the file.
- The `<file>` parameter represents the file for which the group ownership needs to be changed.

Examples:
- To set read, write, and execute permissions for the owner, and read and execute permissions for the group and others, on a file
named `script.sh`, use the command: `chmod 755 script.sh`
- To change the owner of a file named `file.txt` to a user named `user1`, use the command: `chown user1 file.txt`
- To change the group ownership of a file named `file.txt` to a group named `group1`, use the command: `chgrp group1 file.txt`

These commands are used to manage file permissions, ownership, and group ownership in Linux. Ensure that you have appropriate
permissions to modify the files and directories.

9. Display File Content:


- `cat <file>`: Display the content of a file.
- `more <file>`: Display the content of a file page by page.
- `less <file>`: Display the content of a file with backward navigation.
- `head <file>`: Display the beginning lines of a file.
- `tail <file>`: Display the ending lines of a file.
- `tail -f <file>`: Monitor the changes in a file in real-time.

10. File Compression and Archiving:


Here is a list of Linux commands for file compression and archiving:

1. `tar <options> <archive-file> <files>`: Create a tarball or extract files from a tarball.
- `<options>`: Various options can be used with the `tar` command to specify the operation, compression, and more.
- `<archive-file>`: The name of the tarball archive file to create or extract from.
- `<files>`: The files or directories to include in the tarball or extract from it.

2. `gzip <file>`: Compress a file.


- `<file>`: The name of the file to be compressed. The original file will be replaced with a compressed version.
3. `gunzip <file>`: Decompress a compressed file.
- `<file>`: The name of the compressed file to be decompressed. The original file will be replaced with the decompressed version.

4. `zip <archive-file> <files>`: Create a ZIP archive.


- `<archive-file>`: The name of the ZIP archive file to create.
- `<files>`: The files or directories to include in the ZIP archive.

5. `unzip <archive-file>`: Extract files from a ZIP archive.


- `<archive-file>`: The name of the ZIP archive file from which to extract files.

These are some commonly used Linux commands for file operations. Each command has various options and arguments to perform
specific tasks. You can refer to the respective command's man pages for detailed information on their usage and options by typing
`man <command>` in the terminal.

B. File Viewing and Editing


Here is a list of Linux commands for viewing and editing files:

1. File Viewing:
Here is a list of Linux commands for file viewing:

1. `cat <file>`: Display the content of a file. It prints the entire file to the terminal.
Example: `cat myfile.txt`

2. `more <file>`: View the content of a file page by page. Press the Spacebar to scroll to the next page and press Q to exit.
Example: `more myfile.txt`

3. `less <file>`: View the content of a file with backward navigation. It provides more advanced features compared to `more`, such as
searching and scrolling through the file. Press the Spacebar to scroll forward, and press Q to exit.
Example: `less myfile.txt`

4. `head <file>`: Display the first few lines of a file. By default, it shows the first 10 lines, but you can specify a different number using
the `-n` option.
Example: `head -n 5 myfile.txt` (Displays the first 5 lines)

5. `tail <file>`: Display the last few lines of a file. By default, it shows the last 10 lines, but you can specify a different number using
the `-n` option.
Example: `tail -n 5 myfile.txt` (Displays the last 5 lines)

6. `tail -f <file>`: Monitor the changes in a file in real-time. It continuously displays the appended lines as they are added to the file.
Example: `tail -f myfile.txt`

7. `grep <pattern> <file>`: Search for a specific pattern within a file. It displays the lines containing the pattern.
Example: `grep "error" myfile.log`

These commands provide different ways to view the content of files in Linux. Choose the appropriate command based on your
specific requirements and the desired output.

2. File Editing:
Here is a list of Linux commands for file editing:

1. `vi <file>`: Open a file in the Vi text editor. Vi has multiple modes for editing. Press I to enter insert mode, Esc to exit insert mode,
:wq to save and exit, and :q! to exit without saving.
Example: `vi myfile.txt`

2. `nano <file>`: Open a file in the Nano text editor. Nano provides a user-friendly and straightforward interface. Use the arrow keys to
navigate, and press Ctrl + O to save and Ctrl + X to exit.
Example: `nano myfile.txt`
3. `emacs <file>`: Open a file in the Emacs text editor. Emacs is a powerful and extensible text editor with its own set of commands
and keybindings.
Example: `emacs myfile.txt`

4. `sed -i 's/<pattern>/<replacement>/g' <file>`: Replace occurrences of a pattern with a replacement string in a file. The `-i` option
modifies the file in-place.
Example: `sed -i 's/foo/bar/g' myfile.txt` (Replaces all occurrences of "foo" with "bar" in myfile.txt)

5. `awk '<pattern> {<action>}' <file>`: Perform actions on specific patterns within a file using the AWK scripting language.
Example: `awk '/pattern/ {print $1}' myfile.txt` (Prints the first field of lines containing "pattern" in myfile.txt)

These commands offer different options for editing files in Linux. Choose the one that suits your needs and preferences. Remember
to exercise caution while editing files, and make sure to have appropriate permissions to modify the files.

3. File Searching:
Here is a list of Linux commands for file searching:

1. `grep <pattern> <file>`: Search for a specific pattern within a file. It displays the lines containing the pattern.
Example: `grep "error" myfile.log`

2. `grep -r <pattern> <directory>`: Recursively search for a pattern within a directory and its subdirectories.
Example: `grep -r "keyword" /path/to/directory`

3. `find <directory> -name "<filename>"`: Search for files by name within a directory and its subdirectories.
Example: `find /path/to/directory -name "file.txt"`

4. `locate <filename>`: Quickly find files by name using a pre-built database. It provides fast search results but requires regular
updating of the database using the `updatedb` command.
Example: `locate file.txt`

5. `ack <pattern> <file(s)>`: A tool similar to `grep` specifically designed for code searching. It searches for a pattern within specified
files or directories.
Example: `ack "function_name" myfile.py`

6. `ag <pattern> <file(s)>`: Another code searching tool similar to `ack`. It is known for its speed and support for regular expressions.
Example: `ag "class_name" myfile.py`

These commands help you search for specific patterns or files within your Linux system. Choose the appropriate command based on
your requirements, whether it's searching for patterns in file contents or locating files by name.
.

4. File Comparison:
Here is a list of Linux commands for file comparison:

1. `diff <file1> <file2>`: Compare two files line by line and display the differences between them.
Example: `diff file1.txt file2.txt`

2. `vimdiff <file1> <file2>`: Compare two files in the Vim editor and highlight the differences.
Example: `vimdiff file1.txt file2.txt`

3. `cmp <file1> <file2>`: Compare two files byte by byte and report the first differing byte and its offset.
Example: `cmp file1.txt file2.txt`

4. `comm <file1> <file2>`: Compare two sorted files line by line and display lines that are unique to each file or common to both files.
Example: `comm file1.txt file2.txt`

5. `sdiff <file1> <file2>`: Compare two files side by side and highlight differences.
Example: `sdiff file1.txt file2.txt`

These commands provide different ways to compare files in Linux. Choose the appropriate command based on your specific needs
and the type of comparison you want to perform, whether it's comparing line by line, byte by byte, or side by side.
5. File Content Manipulation:
Here is a list of Linux commands for file content manipulation:

1. `sed 's/<pattern>/<replacement>/g' <file>`: Replace occurrences of a pattern with a replacement string in a file.
Example: `sed 's/foo/bar/g' myfile.txt` (Replaces all occurrences of "foo" with "bar" in myfile.txt)

2. `awk '<pattern> {<action>}' <file>`: Perform actions on specific patterns within a file using the AWK scripting language.
Example: `awk '/pattern/ {print $1}' myfile.txt` (Prints the first field of lines containing "pattern" in myfile.txt)

3. `cut -d'<delimiter>' -f <fields> <file>`: Extract specific fields from a file based on a delimiter.
Example: `cut -d',' -f 1,3 myfile.csv` (Extracts the first and third fields from myfile.csv using comma as the delimiter)

4. `sort <file>`: Sort the lines of a file alphabetically or numerically.


Example: `sort myfile.txt`

5. `uniq <file>`: Remove duplicate consecutive lines from a file.


Example: `uniq myfile.txt`

6. `tr '<old>' '<new>' <file>`: Translate or delete characters in a file.


Example: `tr '[:lower:]' '[:upper:]' myfile.txt` (Converts lowercase letters to uppercase in myfile.txt)

7. `paste -d'<delimiter>' <file1> <file2>`: Merge lines from multiple files side by side with a specified delimiter.
Example: `paste -d',' file1.txt file2.txt` (Merges lines from file1.txt and file2.txt with comma as the delimiter)

These commands allow you to manipulate the content of files in various ways, such as replacing text, extracting fields, sorting,
removing duplicates, and more. Choose the appropriate command based on your specific requirements and the desired content
manipulation.
.

These are some commonly used Linux commands for viewing and editing files. Each command has its own set of options and
functionalities to suit different needs. Refer to the respective command's documentation or use the `man` command for more
information on their usage and options.
.

C. File Permissions
Here is a list of Linux commands related to file permissions:

1. `ls -l`: List files and directories in long format, displaying detailed information including permissions.
Example: `ls -l`

2. `chmod <permissions> <file>`: Change the permissions of a file or directory.


Example: `chmod +x script.sh` (Adds execute permission to the script.sh file)

3. `chown <user>:<group> <file>`: Change the owner and group of a file or directory.
Example: `chown user1:group1 myfile.txt` (Changes the owner to user1 and the group to group1 for myfile.txt)

4. `chgrp <group> <file>`: Change the group of a file or directory.


Example: `chgrp group2 myfile.txt` (Changes the group to group2 for myfile.txt)

5. `umask`: Set the default file permissions mask for newly created files and directories.
Example: `umask 0022` (Sets the default permissions mask to allow read and write access to the owner and read access to others)

6. `su <user>`: Switch to another user account with their permissions.


Example: `su user2` (Switches to user2 account)

7. `sudo`: Execute a command with elevated privileges.


Example: `sudo rm myfile.txt` (Deletes myfile.txt with superuser privileges)
These commands allow you to manage file permissions, change ownership and group, and perform actions with elevated privileges.
It's important to exercise caution when modifying permissions and to have appropriate privileges for performing certain operations.

D. System Information
Here is a list of Linux commands to obtain system information:

1. `uname`: Display system information such as the kernel name, network node hostname, kernel release, kernel version, machine
hardware name, and processor type.
Example: `uname -a`

2. `hostname`: Show or set the system's hostname.


Example: `hostname`

3. `whoami`: Display the username of the current user.


Example: `whoami`

4. `uptime`: Show the system's uptime, load average, and number of logged-in users.
Example: `uptime`

5. `free`: Display information about available and used system memory (RAM).
Example: `free -h`

6. `df`: Show disk space usage for filesystems.


Example: `df -h`

7. `du`: Estimate file and directory space usage.


Example: `du -sh /path/to/directory`

8. `top`: Display real-time system resource usage including CPU, memory, and process information.
Example: `top`

9. `ps`: Show information about active processes running on the system.


Example: `ps aux`

10. `lscpu`: Display detailed information about the CPU architecture and characteristics.
Example: `lscpu`

11. `lsblk`: List block devices (disks and partitions) on the system.
Example: `lsblk`

12. `lshw`: Show detailed hardware information about the system.


Example: `lshw`

13. `ifconfig` or `ip addr`: Display network interface configuration.


Example: `ifconfig` or `ip addr`

14. `netstat`: Display network connections, routing tables, and network statistics.
Example: `netstat -tuln`

15. `lsusb`: List USB devices connected to the system.


Example: `lsusb`

These commands provide various system information, including kernel details, hostname, user information, uptime, memory usage,
disk space, process information, CPU details, network configuration, hardware information, and more. Use the appropriate command
to obtain the specific information you need.

E. Package Management:
Here is a list of Linux commands for package management:
1. `apt`: Advanced Package Tool (APT) is a package management system used by Debian-based distributions, such as Ubuntu.

- `apt update`: Update the package lists to get the latest version information of packages.
- `apt upgrade`: Upgrade installed packages to their latest versions.
- `apt install <package>`: Install a package.
- `apt remove <package>`: Remove a package.
- `apt search <keyword>`: Search for packages based on a keyword.
- `apt show <package>`: Display detailed information about a package.

2. `yum`: Yellowdog Updater Modified (YUM) is a package management system used by Red Hat-based distributions, such as CentOS
and Fedora.

- `yum update`: Update installed packages to their latest versions.


- `yum install <package>`: Install a package.
- `yum remove <package>`: Remove a package.
- `yum search <keyword>`: Search for packages based on a keyword.
- `yum info <package>`: Display detailed information about a package.

3. `dnf`: Dandified YUM (DNF) is the next-generation package manager used by Fedora and CentOS 8+.

- `dnf update`: Update installed packages to their latest versions.


- `dnf install <package>`: Install a package.
- `dnf remove <package>`: Remove a package.
- `dnf search <keyword>`: Search for packages based on a keyword.
- `dnf info <package>`: Display detailed information about a package.

4. `zypper`: Zypper is the package management tool used by openSUSE.

- `zypper update`: Update installed packages to their latest versions.


- `zypper install <package>`: Install a package.
- `zypper remove <package>`: Remove a package.
- `zypper search <keyword>`: Search for packages based on a keyword.
- `zypper info <package>`: Display detailed information about a package.

These are some of the commonly used package management commands on Linux. The exact commands may vary depending on the
specific Linux distribution you are using.

F. Networking
Here is a list of commonly used Linux networking commands:

1. `ifconfig`: Display or configure network interfaces.


Example: `ifconfig eth0`

2. `ip`: Show or manipulate routing, devices, and policy routing.


Example: `ip address show`

3. `ping`: Send ICMP echo requests to a specified host.


Example: `ping google.com`

4. `traceroute`: Print the route packets take to a network host.


Example: `traceroute google.com`

5. `netstat`: Print network connections, routing tables, interface statistics, and more.
Example: `netstat -a`

6. `ss`: Another tool to investigate sockets and network connections.


Example: `ss -tunap`

7. `nslookup`: Query DNS servers for DNS-related information.


Example: `nslookup google.com`

8. `dig`: DNS lookup utility for querying DNS servers.


Example: `dig google.com`

9. `host`: DNS lookup utility for querying DNS servers.


Example: `host google.com`

10. `wget`: Retrieve files from the web using HTTP, HTTPS, or FTP.
Example: `wget https://example.com/file.txt`

11. `curl`: A versatile command-line tool for making HTTP, HTTPS, and FTP requests.
Example: `curl https://example.com`

12. `ssh`: Secure Shell client for secure remote login and command execution.
Example: `ssh user@hostname`

13. `scp`: Securely copy files between hosts over SSH.


Example: `scp localfile.txt remoteuser@remotehost:/path/to/destination`

14. `iptables`: A powerful firewall management tool for configuring IP packet filtering and NAT rules.
Example: `iptables -L`

15. `iptraf`: Interactive IP LAN monitor to analyze network traffic in real-time.


Example: `iptraf`

16. `nmap`: Network exploration and security auditing tool for scanning hosts and services.
Example: `nmap -sP 192.168.0.0/24`

17. `route`: View or manipulate IP routing table.


Example: `route -n`

18. `iwconfig`: Configure wireless network interfaces.


Example: `iwconfig wlan0`

19. `hostapd`: Host Access Point daemon for creating a wireless access point.
Example: `hostapd /etc/hostapd/hostapd.conf`

These commands cover a wide range of networking tasks, such as configuring interfaces, checking network connectivity, performing
DNS lookups, retrieving files, managing firewalls, monitoring network traffic, and more. Use the appropriate command based on your
specific networking requirements.

G. Process Management:
Here is a list of commonly used Linux commands for process management:

1. `ps`: Report a snapshot of the current processes.


Example: `ps aux`

2. `top`: Monitor system processes in real-time.


Example: `top`

3. `htop`: Interactive process viewer with more features than 'top'.


Example: `htop`

4. `kill`: Send a signal to terminate a process.


Example: `kill <PID>`

5. `killall`: Terminate a process by name.


Example: `killall firefox`
6. `pgrep`: List processes based on name.
Example: `pgrep firefox`

7. `pkill`: Send a signal to terminate processes based on name.


Example: `pkill firefox`

8. `pidof`: Find the process ID of a running program.


Example: `pidof firefox`

9. `jobs`: List active jobs and their status.


Example: `jobs`

10. `fg`: Bring a background job to the foreground.


Example: `fg %1`

11. `bg`: Move a job to the background.


Example: `bg %1`

12. `nice`: Set the priority of a process.


Example: `nice -n 10 command`

13. `renice`: Change the priority of a running process.


Example: `renice +5 <PID>`

14. `pstree`: Display a tree of processes.


Example: `pstree`

15. `systemctl`: Control and manage system services.


Example: `systemctl start <service>`

16. `service`: Command to start, stop, or restart system services.


Example: `service apache2 restart`

17. `lsof`: List open files and the processes using them.
Example: `lsof /path/to/file`

18. `strace`: Trace system calls and signals of a process.


Example: `strace -p <PID>`

19. `top`: Monitor system processes in real-time.


Example: `top`

These commands help you manage processes on a Linux system, including listing processes, terminating processes, changing
process priorities, managing jobs, monitoring system resources, controlling system services, and more. Use the appropriate
command based on your specific process management needs.

H. Compression and Archiving


Here are some commonly used Linux commands for compression and archiving:

1. `tar`: Create or extract tar archives.


- Create a tar archive: `tar -cvf archive.tar /path/to/directory`
- Extract a tar archive: `tar -xvf archive.tar`
- Create a compressed tar archive (gzip): `tar -cvzf archive.tar.gz /path/to/directory`
- Extract a compressed tar archive (gzip): `tar -xvzf archive.tar.gz`
- Create a compressed tar archive (bzip2): `tar -cvjf archive.tar.bz2 /path/to/directory`
- Extract a compressed tar archive (bzip2): `tar -xvjf archive.tar.bz2`

2. `gzip` / `gunzip`: Compress or decompress files using gzip compression.


- Compress a file: `gzip file.txt` (creates file.txt.gz)
- Decompress a file: `gunzip file.txt.gz` (restores file.txt)

3. `bzip2` / `bunzip2`: Compress or decompress files using bzip2 compression.


- Compress a file: `bzip2 file.txt` (creates file.txt.bz2)
- Decompress a file: `bunzip2 file.txt.bz2` (restores file.txt)

4. `zip` / `unzip`: Create or extract ZIP archives.


- Create a zip archive: `zip archive.zip /path/to/file1 /path/to/file2`
- Extract a zip archive: `unzip archive.zip`

5. `rar` / `unrar`: Create or extract RAR archives (if the `rar` package is installed).
- Create a rar archive: `rar a archive.rar /path/to/file1 /path/to/file2`
- Extract a rar archive: `unrar x archive.rar`

6. `7z`: Create or extract 7z archives (if the `p7zip` package is installed).


- Create a 7z archive: `7z a archive.7z /path/to/file1 /path/to/file2`
- Extract a 7z archive: `7z x archive.7z`

7. `gzip` / `gunzip`: Compress or decompress individual files using gzip compression.


- Compress a file: `gzip file.txt` (creates file.txt.gz)
- Decompress a file: `gunzip file.txt.gz` (restores file.txt)

8. `xz` / `unxz`: Compress or decompress files using LZMA compression.


- Compress a file: `xz file.txt` (creates file.txt.xz)
- Decompress a file: `unxz file.txt.xz` (restores file.txt)

9. `tar.gz` / `tar.bz2` / `tar.xz`: Create or extract tar archives with compression directly.
- Create a compressed tar archive (gzip): `tar -cvzf archive.tar.gz /path/to/directory`
- Extract a compressed tar archive (gzip): `tar -xvzf archive.tar.gz`
- Create a compressed tar archive (bzip2): `tar -cvjf archive.tar.bz2 /path/to/directory`
- Extract a compressed tar archive (bzip2): `tar -xvjf archive.tar.bz2`
- Create a compressed tar archive (xz): `tar -cvJf archive.tar.xz /path/to/directory`
- Extract a compressed tar archive (xz): `tar -xvJf archive.tar.xz`

These commands allow you to create and extract compressed and archived files

I. System Administration
Here are some commonly used Linux commands for system administration tasks:

1. User and Group Management:


Here are some commonly used Linux commands for user and group management:

1. User Management:
Here are some commonly used Linux commands for user management:

1. Create a new user: `useradd <username>`


Example: `useradd john`

2. Create a new user with additional information: `useradd -c "<comment>" <username>`


Example: `useradd -c "John Smith" john`

3. Set password for a user: `passwd <username>`


Example: `passwd john`

4. Delete a user: `userdel <username>`


Example: `userdel john`
5. Modify user account: `usermod <options> <username>`
Example: `usermod -c "New Comment" john`

6. Lock a user account: `passwd -l <username>`


Example: `passwd -l john`

7. Unlock a user account: `passwd -u <username>`


Example: `passwd -u john`

8. Switch to another user: `su <username>`


Example: `su john`

9. Assign a shell to a user: `chsh -s <shell> <username>`


Example: `chsh -s /bin/bash john`

10. View user account information: `id <username>`


Example: `id john`

11. List user accounts: `cat /etc/passwd`

12. View password expiration information: `chage -l <username>`


Example: `chage -l john`

13. Grant sudo access to a user (requires sudo privileges): `visudo`


This opens the sudoers file. Add the following line to grant sudo access:
`<username> ALL=(ALL:ALL) ALL`
Example: `john ALL=(ALL:ALL) ALL`

14. Revoke sudo access from a user (requires sudo privileges): `visudo`
This opens the sudoers file. Remove the line granting sudo access for the user.
Example: `john ALL=(ALL:ALL) ALL`

These commands allow you to create, modify, delete, and manage user accounts in a Linux system. Make sure to
execute these commands with appropriate privileges or as a superuser (e.g., using `sudo`) when required.

2. Group Management:
Here are some commonly used Linux commands for group management:

1. Create a new group: `groupadd <groupname>`


Example: `groupadd developers`

2. Delete a group: `groupdel <groupname>`


Example: `groupdel developers`

3. Modify group settings: `groupmod <options> <groupname>`


Example: `groupmod -n newname oldname`

4. Assign a user to a group: `usermod -aG <groupname> <username>`


Example: `usermod -aG developers john`

5. View group information: `id -Gn <groupname>`


Example: `id -Gn developers`

6. List all groups: `cat /etc/group`

7. Add a user to multiple groups at once: `usermod -aG <group1>,<group2> <username>`


Example: `usermod -aG developers,admins john`

8. Remove a user from a group: `gpasswd -d <username> <groupname>`


Example: `gpasswd -d john developers`

9. Change the primary group of a user: `usermod -g <groupname> <username>`


Example: `usermod -g developers john`

10. List members of a group: `getent group <groupname>`


Example: `getent group developers`

11. Check a user's group membership: `groups <username>`


Example: `groups john`

These commands allow you to create, modify, and manage groups in a Linux system. Make sure to
execute these commands with appropriate privileges or as a superuser (e.g., using `sudo`) when required.

3. Password Management:
Here are some commonly used Linux commands for password management:

1. Set password for a user: `passwd <username>`


Example: `passwd john`

2. Force a user to change their password on next login: `chage -d 0 <username>`


Example: `chage -d 0 john`

3. Set password expiration date for a user: `chage -E <YYYY-MM-DD> <username>`


Example: `chage -E 2023-12-31 john`

4. View password expiration information for a user: `chage -l <username>`


Example: `chage -l john`

5. Lock a user account: `passwd -l <username>`


Example: `passwd -l john`

6. Unlock a user account: `passwd -u <username>`


Example: `passwd -u john`
7. Disable password authentication for a user (useful for SSH key-based authentication): `usermod -p '*'
<username>`
Example: `usermod -p '*' john`

8. Enable password authentication for a user (if previously disabled): `usermod -p '' <username>`
Example: `usermod -p '' john`

9. Require a minimum password length: `passwd -minlen <length>`


Example: `passwd -minlen 8`

10. Set password complexity requirements: This may vary depending on the Linux distribution and
configuration. Please refer to the specific documentation for your distribution.

11. Reset a forgotten root password: This process can vary depending on the Linux distribution. It often
involves booting into recovery mode or using a live CD/USB. Please refer to the specific documentation for
your distribution.

Remember to execute these commands with appropriate privileges or as a superuser (e.g., using `sudo`)
when required. Additionally, ensure that you follow best practices for password security, such as using
strong and unique passwords, regularly updating passwords, and implementing multi-factor
authentication where possible.

4. User and Group Information:


Here are some commonly used Linux commands to obtain user and group information:

1. Display user information: `id <username>`


Example: `id john`

2. Display group information: `id -Gn <groupname>`


Example: `id -Gn developers`

3. View user account information: `finger <username>`


Example: `finger john`

4. List user accounts: `cat /etc/passwd`

5. List group accounts: `cat /etc/group`

6. Check a user's group membership: `groups <username>`


Example: `groups john`

7. Check the primary group of a user: `id -g <username>`


Example: `id -g john`

8. List all groups a user belongs to: `id -G <username>`


Example: `id -G john`

9. Display information about the current user: `whoami`


10. Get the home directory of a user: `echo ~<username>`
Example: `echo ~john`

11. Show the users currently logged in: `who` or `w`

12. Show the groups a user belongs to (alternative to `groups` command): `getent group | grep
<username>`
Example: `getent group | grep john`

These commands allow you to obtain information about users and groups in a Linux system. Make sure to
execute these commands with appropriate privileges or as a superuser (e.g., using `sudo`) when required.

5. File Ownership and Permissions:


Here are some commonly used Linux commands for file ownership and permissions management:

1. chown: Change the ownership of a file or directory.


- Syntax: `chown [OPTIONS] OWNER[:GROUP] FILE`
- Example: `chown john:users file.txt` (Change the owner of `file.txt` to user `john` and the group to
`users`)

2. chgrp: Change the group ownership of a file or directory.


- Syntax: `chgrp [OPTIONS] GROUP FILE`
- Example: `chgrp developers file.txt` (Change the group ownership of `file.txt` to `developers`)

3. chmod: Change the permissions of a file or directory.


- Syntax: `chmod [OPTIONS] MODE FILE`
- Example: `chmod u+rwx file.txt` (Give the owner of `file.txt` read, write, and execute permissions)

4. ls: List files and directories with their permissions.


- Syntax: `ls [OPTIONS] [FILE]`
- Example: `ls -l file.txt` (Display the permissions and ownership of `file.txt`)

5. chown and chgrp recursive: Change ownership and group ownership recursively for a directory and its
contents.
- Syntax: `chown -R [OPTIONS] OWNER[:GROUP] DIRECTORY`
- Syntax: `chgrp -R [OPTIONS] GROUP DIRECTORY`
- Example: `chown -R john:users directory` (Recursively change the owner of `directory` and its contents
to user `john` and the group to `users`)

6. chmod symbolic mode: Change permissions using a symbolic mode.


- Syntax: `chmod [OPTIONS] [MODE] FILE`
- Example: `chmod u+x file.txt` (Give the owner execute permission on `file.txt`)

7. umask: Set the default file permissions for newly created files and directories.
- Syntax: `umask [OPTIONS] [MODE]`
- Example: `umask 0022` (Set the default file permissions to `644` and default directory permissions to
`755`)
These commands allow you to manage file ownership and permissions in a Linux system. Make sure to
execute these commands with appropriate privileges or as a superuser (e.g., using `sudo`) when required.

2. File and Directory Management:


Here are some commonly used Linux commands for file and directory management in system
administration:

1. ls: List files and directories.


- Syntax: `ls [OPTIONS] [FILE]`
- Example: `ls -l /path/to/directory` (List files and directories in long format)

2. cd: Change directory.


- Syntax: `cd [DIRECTORY]`
- Example: `cd /path/to/directory` (Change the current directory to `/path/to/directory`)

3. pwd: Print working directory.


- Syntax: `pwd`
- Example: `pwd` (Display the current working directory)

4. mkdir: Create a directory.


- Syntax: `mkdir [OPTIONS] DIRECTORY`
- Example: `mkdir /path/to/new/directory` (Create a new directory named `directory`)

5. rm: Remove files and directories.


- Syntax: `rm [OPTIONS] FILE`
- Example: `rm file.txt` (Remove the file named `file.txt`)

6. cp: Copy files and directories.


- Syntax: `cp [OPTIONS] SOURCE DESTINATION`
- Example: `cp file.txt /path/to/destination` (Copy `file.txt` to `/path/to/destination`)

7. mv: Move or rename files and directories.


- Syntax: `mv [OPTIONS] SOURCE DESTINATION`
- Example: `mv file.txt /path/to/destination` (Move `file.txt` to `/path/to/destination`)

8. chmod: Change file permissions.


- Syntax: `chmod [OPTIONS] MODE FILE`
- Example: `chmod 755 file.txt` (Set the file `file.txt` with permissions `755`)

9. chown: Change file ownership.


- Syntax: `chown [OPTIONS] OWNER[:GROUP] FILE`
- Example: `chown john:users file.txt` (Change the owner of `file.txt` to user `john` and the group to
`users`)

10. chgrp: Change group ownership.


- Syntax: `chgrp [OPTIONS] GROUP FILE`
- Example: `chgrp developers file.txt` (Change the group ownership of `file.txt` to `developers`)
These commands allow you to manage files and directories in a Linux system. Make sure to execute these
commands with appropriate privileges or as a superuser (e.g., using `sudo`) when required.

3. System Information:
Here are some commonly used Linux commands for obtaining system information in system
administration:

1. uname: Print system information.


- Syntax: `uname [OPTIONS]`
- Example: `uname -a` (Print all system information)

2. hostname: Display or set the system's hostname.


- Syntax: `hostname [OPTIONS]`
- Example: `hostname` (Display the current hostname)

3. whoami: Display the current username.


- Syntax: `whoami`
- Example: `whoami` (Display the current logged-in username)

4. uptime: Display system uptime.


- Syntax: `uptime`
- Example: `uptime` (Display system uptime)

5. dmesg: Print or control the kernel ring buffer.


- Syntax: `dmesg [OPTIONS]`
- Example: `dmesg` (Display kernel ring buffer messages)

6. lscpu: Display information about the CPU architecture.


- Syntax: `lscpu`
- Example: `lscpu` (Display CPU information)

7. free: Display amount of free and used memory in the system.


- Syntax: `free [OPTIONS]`
- Example: `free -h` (Display memory information in a human-readable format)

8. df: Display disk space usage of file systems.


- Syntax: `df [OPTIONS] [FILESYSTEM]`
- Example: `df -h` (Display disk space usage in a human-readable format)

9. lspci: List all PCI devices connected to the system.


- Syntax: `lspci [OPTIONS]`
- Example: `lspci` (List PCI devices)

10. lsusb: List USB devices connected to the system.


- Syntax: `lsusb [OPTIONS]`
- Example: `lsusb` (List USB devices)
These commands provide information about the system's hardware, kernel, memory usage, disk space,
and more. Execute these commands in the terminal to obtain the relevant system information.

4. Process Management:
- List running processes: `ps aux`
- Kill a process: `kill <PID>`
- Monitor system processes: `top` or `htop`
- Start a background process: `command &`
Here are some commonly used Linux commands for process management in system administration:

1. ps: Report snapshot of current processes.


- Syntax: `ps [OPTIONS]`
- Example: `ps aux` (Display information about all running processes)

2. top: Monitor system processes in real-time.


- Syntax: `top`
- Example: `top` (Display dynamic information about system processes)

3. htop: Interactive process viewer (alternative to top).


- Syntax: `htop`
- Example: `htop` (Display an interactive view of system processes)

4. kill: Terminate a process.


- Syntax: `kill [OPTIONS] PID`
- Example: `kill 1234` (Terminate the process with PID 1234)

5. killall: Terminate processes by name.


- Syntax: `killall [OPTIONS] NAME`
- Example: `killall firefox` (Terminate all processes named "firefox")

6. nice: Set process priority.


- Syntax: `nice [OPTIONS] COMMAND`
- Example: `nice -n 10 command` (Start a command with a lower priority)

7. renice: Alter priority of running processes.


- Syntax: `renice [OPTIONS] PRIORITY PID`
- Example: `renice +5 1234` (Increase the priority of process 1234 by 5)

8. pstree: Display process hierarchy in tree format.


- Syntax: `pstree [OPTIONS]`
- Example: `pstree -p` (Display process tree with PIDs)

9. pgrep: List processes based on name or attributes.


- Syntax: `pgrep [OPTIONS] PATTERN`
- Example: `pgrep firefox` (List PIDs of processes named "firefox")

10. pkill: Terminate processes based on name or attributes.


- Syntax: `pkill [OPTIONS] PATTERN`
- Example: `pkill firefox` (Terminate processes named "firefox")
These commands allow you to monitor running processes, terminate processes, adjust process priorities, and view
process hierarchy. Make sure to execute these commands with appropriate privileges or as a superuser (e.g., using
`sudo`) when required.

5. Networking:
- Here are some commonly used Linux commands for networking in system administration:
1. ifconfig: Configure and display network interfaces.
- Syntax: `ifconfig [OPTIONS] [INTERFACE]`
- Example: `ifconfig eth0` (Display information for the interface "eth0")

2. ip: Network configuration and routing.


- Syntax: `ip [OPTIONS] OBJECT {COMMAND | help}`
- Example: `ip addr show` (Display IP addresses assigned to network interfaces)

3. netstat: Network statistics.


- Syntax: `netstat [OPTIONS]`
- Example: `netstat -tuln` (Display listening TCP and UDP sockets)

4. ss: Another utility to investigate sockets.


- Syntax: `ss [OPTIONS] [FILTER]`
- Example: `ss -tunap` (Display TCP, UDP, and UNIX sockets)

5. ping: Test network connectivity.


- Syntax: `ping [OPTIONS] HOST`
- Example: `ping 8.8.8.8` (Send ICMP echo requests to the IP address 8.8.8.8)

6. traceroute: Print the route packets take to a network host.


- Syntax: `traceroute [OPTIONS] HOST`
- Example: `traceroute google.com` (Trace the route to the host "google.com")

7. nslookup: Query DNS servers to retrieve DNS information.


- Syntax: `nslookup [OPTIONS] HOST`
- Example: `nslookup google.com` (Retrieve DNS information for the host "google.com")

8. dig: DNS lookup utility for querying DNS servers.


- Syntax: `dig [OPTIONS] HOST`
- Example: `dig google.com` (Perform a DNS lookup for the host "google.com")

9. ssh: Secure shell client for remote login and secure file transfer.
- Syntax: `ssh [OPTIONS] [USER@]HOST`
- Example: `ssh user@example.com` (Establish an SSH connection to the host "example.com" as the
user "user")

10. scp: Securely copy files between hosts using SSH.


- Syntax: `scp [OPTIONS] [USER@]HOST:SOURCE [DESTINATION]`
- Example: `scp file.txt user@example.com:/path/to/destination` (Copy "file.txt" to the remote host
"example.com")
These commands are commonly used for network configuration, troubleshooting, and connectivity testing
in Linux system administration. Make sure to execute these commands with appropriate privileges or as a
superuser (e.g., using `sudo`) when required.

6. Package Management:
- Here are some commonly used Linux commands for package management in system administration:
1. apt-get: Command-line package handling utility for Debian-based systems (e.g., Ubuntu).
- Syntax: `apt-get [OPTIONS] COMMAND`
- Example: `apt-get install packageName` (Install a package)

2. apt: Command-line interface for package management in Debian-based systems (e.g., Ubuntu). It is the
successor to `apt-get` and provides more advanced features.
- Syntax: `apt [OPTIONS] COMMAND`
- Example: `apt install packageName` (Install a package)

3. yum: Package manager for RPM-based systems (e.g., CentOS, Fedora).


- Syntax: `yum [OPTIONS] COMMAND`
- Example: `yum install packageName` (Install a package)

4. dnf: Next-generation package manager for RPM-based systems (e.g., Fedora).


- Syntax: `dnf [OPTIONS] COMMAND`
- Example: `dnf install packageName` (Install a package)

5. zypper: Package management tool for SUSE-based systems.


- Syntax: `zypper [OPTIONS] COMMAND`
- Example: `zypper install packageName` (Install a package)

6. pacman: Package manager for Arch Linux and its derivatives.


- Syntax: `pacman [OPTIONS] COMMAND`
- Example: `pacman -S packageName` (Install a package)

7. dpkg: Low-level package manager for Debian-based systems (e.g., Ubuntu).


- Syntax: `dpkg [OPTIONS] COMMAND`
- Example: `dpkg -i package.deb` (Install a package from a .deb file)

8. rpm: Package manager for RPM-based systems (e.g., CentOS, Fedora).


- Syntax: `rpm [OPTIONS] COMMAND`
- Example: `rpm -i package.rpm` (Install a package from a .rpm file)

9. apt-cache: Query the APT package cache.


- Syntax: `apt-cache [OPTIONS] COMMAND`
- Example: `apt-cache search packageName` (Search for a package in the cache)

10. yum search: Search package repositories for a specific package.


- Syntax: `yum search packageName`
- Example: `yum search packageName` (Search for a package in the repositories)
These commands allow you to install, remove, update, and manage software packages on your Linux
system. The specific command and package management tool you use may depend on the Linux
distribution you are using.

7. System Services:
- Here are some commonly used Linux commands for managing system services in system
administration:

1. systemctl: Control the systemd system and service manager.


- Syntax: `systemctl [OPTIONS] COMMAND`
- Example: `systemctl start serviceName` (Start a service)

2. service: Run a System V init script to start, stop, or restart a service.


- Syntax: `service serviceName [COMMAND]`
- Example: `service serviceName start` (Start a service)

3. chkconfig: Update and query runlevel information for system services.


- Syntax: `chkconfig [OPTIONS] SERVICE [ON|OFF|RESET]`
- Example: `chkconfig serviceName on` (Enable a service to start at boot)

4. init.d: Directory containing System V init scripts for services.


- Syntax: `/etc/init.d/serviceName [COMMAND]`
- Example: `/etc/init.d/serviceName start` (Start a service)

5. systemctl status: Show the status of a service.


- Syntax: `systemctl status serviceName`
- Example: `systemctl status sshd` (Display the status of the SSH service)

6. systemctl enable: Enable a service to start at boot.


- Syntax: `systemctl enable serviceName`
- Example: `systemctl enable apache2` (Enable the Apache service to start at boot)

7. systemctl disable: Disable a service from starting at boot.


- Syntax: `systemctl disable serviceName`
- Example: `systemctl disable nginx` (Disable the Nginx service from starting at boot)

8. systemctl restart: Restart a service.


- Syntax: `systemctl restart serviceName`
- Example: `systemctl restart mysql` (Restart the MySQL service)

9. systemctl stop: Stop a service.


- Syntax: `systemctl stop serviceName`
- Example: `systemctl stop httpd` (Stop the Apache service)

10. journalctl: Query the systemd journal for service logs.


- Syntax: `journalctl [OPTIONS] [SERVICE]`
- Example: `journalctl -u serviceName` (Display logs for a specific service)

These commands are commonly used for managing system services, such as starting, stopping,
enabling/disabling services, and checking their status. Note that the exact commands and service
management tools may vary depending on the Linux distribution you are using and whether it utilizes
System V init or systemd.

8. Log Files:
- Here are some commonly used Linux commands for working with log files in system administration:
1. tail: Display the last lines of a file (typically used for log files).
- Syntax: `tail [OPTIONS] FILE`
- Example: `tail -f /var/log/syslog` (Display the last lines of the syslog file and continuously update as
new lines are added)

2. head: Display the first lines of a file (can be useful for inspecting log files).
- Syntax: `head [OPTIONS] FILE`
- Example: `head -n 20 /var/log/auth.log` (Display the first 20 lines of the auth.log file)

3. cat: Concatenate and display the contents of a file (can be used to view log files).
- Syntax: `cat [OPTIONS] FILE`
- Example: `cat /var/log/messages` (Display the contents of the messages log file)

4. less: View and navigate large log files interactively.


- Syntax: `less [OPTIONS] FILE`
- Example: `less /var/log/syslog` (View the syslog file and scroll through its contents)

5. grep: Search for specific patterns or keywords in log files.


- Syntax: `grep [OPTIONS] PATTERN FILE`
- Example: `grep "error" /var/log/nginx/error.log` (Search for the word "error" in the nginx error log file)

6. journalctl: Query and view logs from the systemd journal.


- Syntax: `journalctl [OPTIONS]`
- Example: `journalctl -u serviceName` (Display logs for a specific service)

7. dmesg: Print and control the kernel ring buffer messages (includes boot and hardware-related logs).
- Syntax: `dmesg [OPTIONS]`
- Example: `dmesg | grep "error"` (Display kernel messages containing the word "error")

8. find: Search for log files based on various criteria (e.g., filename, modified date).
- Syntax: `find [PATH] [CRITERIA]`
- Example: `find /var/log -name "*.log"` (Find all log files with the .log extension in the /var/log directory)

These commands provide various ways to view, search, and analyze log files in Linux. They can help you
troubleshoot issues, monitor system activities, and extract relevant information from log files. Remember
to use appropriate options and filters to refine your search or display specific log entries as needed.

You might also like