Notes and Resources for Linux OS.
_ _ _ _ _ _ _ _ __ __
| | |__ __| | \ | | | | | | \ \/ /
| | | | | \| | | | | | \/\/
| |____ _| |_ | |\ | | | | | /\/\
|______| |_ _ _| |_| \_| \___/ /_/\_\
Linux was created by Linus Torvalds in 1991 as an open-source operating system kernel. Initially a hobby project, Linux rapidly evolved with contributions from developers worldwide, incorporating features from Unix-like systems and gaining stability.
Throughout the mid-1990s and early 2000s, Linux gained traction in the server market due to its robustness, scalability, and cost-effectiveness. Distributions like Red Hat, Debian, and Slackware emerged, catering to various user needs.
In the late 2000s and early 2010s, Linux expanded into desktop computing and mobile devices. Desktop-oriented distributions such as Ubuntu and Linux Mint gained popularity, while Android, based on the Linux kernel, became the dominant OS for mobile devices globally.
Linux solidified its position in cloud computing, powering infrastructure for major cloud providers like AWS, GCP, and Azure. Its flexibility, performance, and compatibility with virtualization technologies have made it a cornerstone of modern cloud-native architectures.
Today, Linux enjoys widespread popularity across servers, desktops, embedded systems, and cloud computing. Its vibrant open-source community actively contributes to its development and improvement, emphasizing freedom, security, and innovation in the digital infrastructure landscape.
uptime
- Tell how long the system has been running.
Usage Case:
uptime
Output:
22:23:24 up 9:21, 1 user, load average: 0.85, 1.10, 1.27
free
- Display amount of free and used memory in the system.
Usage Case:
free
Output:
total used free shared buff/cache available
Mem: 7965944 2380788 3041184 378216 2543972 4908056
Swap: 2097148 1104344 992804
date
- print or set the system date and time.
Usage Case:
date
Output:
Fri 26 Jan 2024 22:32:48 IST
date +%x
- print locale's date representation (e.g., 12/31/99)
date +%x
Output:
26/01/2024
head
- output the first part of files. (10 lines)
Usage Case:
head test.txt
Output:
Line 1
Line 2
Line 3
Line 4
Line 5
Line 6
Line 7
Line 8
Line 9
Line 10
head <file_name> -n 5
- output the first 5 lines of file.
head test.txt -n 5
Output:
Line 1
Line 2
Line 3
Line 4
Line 5
tail
- output the last part of files. (10 lines)
Usage Case:
tail test.txt
Output:
Line 10
Line 11
Line 12
Line 13
Line 14
Line 15
Line 16
Line 17
Line 18
Line 19
Line 20
tail <file_name> -n 5
- output the last 5 lines of file.
tail test.txt -n 5
Output:
Line 1
Line 2
Line 3
Line 4
Line 5
history
- display a list of previously executed commands in a terminal session. It shows a numbered list of commands along with their execution sequence. This command is particularly useful for reviewing and reusing commands from your command-line history.
Usage Case:
history
Output:
....
....
....
1152 sudo apt-get update
1153 sudo apt-get upgrade
1154 dmesg
1155 sudo dmesg
1156 cd Desktop/
1157 cd Tmp/
1158 ls
1159 mkdir Meqdad
1160 ls
1161 cd Meqdad/
1162 ls
1163 sudo code .
1164 ls
1165 touch hi.txt
1166 python3
1167 python --version
....
....
....
!<command_id>
- Rerun a specific command from history by using an exclamation mark (!) followed by the line number.
Running ls
command from history (line 1164):
!1164
Output:
LICENSE README.md hi.txt
touch
- Create new empty files or update the timestamp of existing files. When used to create a new file, it's followed by the name of the file you want to create.
Usage Case:
touch example.txt
man
- Stands for "manual". It's used to display the manual pages for other commands installed on your system. These manual pages provide detailed information about commands, their options, syntax, and often include examples and explanations.
man <command_name>
Usage Case:
man ls
id
- To display the user and group identifiers associated with the current user or a specified username. It provides information about the user's UID (User Identifier), GID (Group Identifier), and supplementary group IDs.
id [username]
Usage Case:
id meqdad
Output:
uid=1000(meqdad) gid=1000(meqdad) groups=1000(meqdad),4(adm),20(dialout),24(cdrom),27(sudo),30(dip),46(plugdev),122(lpadmin),135(lxd),136(sambashare)
Running id
command for the root user:
id root
Output:
uid=0(root) gid=0(root) groups=0(root)
id
- When you run theid
command without specifying a username, it displays information about the current user.
Running id
command the current user (meqdad
):
id
Output:
uid=1000(meqdad) gid=1000(meqdad) groups=1000(meqdad),4(adm),20(dialout),24(cdrom),27(sudo),30(dip),46(plugdev),122(lpadmin),135(lxd),136(sambashare)
useradd
- Create a new user account.
useradd [options] [User_name]
Usage Case:
sudo useradd meqdad
Output:
A new user account "meqdad" is created.
sudo useradd -d [home_dir_path] [new_user]
- To give a home directory path for new users.
sudo useradd -d /home/test_user test_user
Output:
A new user account "test_user" is created and the Home directory is created.
userdel
- Delete a user account.
userdel [options] [User_name]
Usage Case:
sudo userdel meqdad
Output:
The user account "meqdad" is deleted.
groupadd
- Create a new group.
groupadd [options] [group_name]
Usage Case:
sudo groupadd test_group
Output:
A new group "test_group" is created.
cat /etc/passwd
- Display user account information.
Usage Case:
cat /etc/passwd
OR
vim /etc/passwd
Output:
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/usr/sbin/nologin
...
...
...
Explanation of Output:
-
Username (Login Name): daemon - This is the username or login name of the user account.
-
Password Placeholder: x - In modern Unix-like operating systems, the actual encrypted password is stored in the /etc/shadow file for security reasons. This placeholder (x) indicates that the password is stored in /etc/shadow.
-
User ID (UID): 1 - This is the user's unique numerical identifier. User IDs 0-99 are typically reserved for system users and services.
-
Group ID (GID): 1 - This is the primary group ID of the user. In this case, both the user ID and the group ID are 1, which is often the case for system-related accounts.
-
User Information: daemon - This field typically contains additional information about the user, such as the full name or description. In this example, it's the same as the username.
-
Home Directory: /usr/sbin - This is the path to the user's home directory. For system accounts like daemon, this might be set to a directory related to the system's operation.
-
Login Shell: /usr/sbin/nologin - This is the path to the user's login shell. If the user is not intended to log in interactively, a shell like /usr/sbin/nologin is used to prevent login.
passwd [username]
- Change or set the password for a user account.
passwd [username]
Usage Case:
passwd meqdad
Output:
Changing password for user meqdad.
New password:
Retype new password:
passwd: password updated successfully
usermod
- Modify user account settings.
Usage Case:
- Changing the login shell for user "meqdad" to
/bin/zsh
.
sudo usermod -s /bin/zsh meqdad
Output:
The login shell for user "meqdad" is changed to "/bin/zsh".
- Adding user "meqdad" to the "admin" group.
sudo usermod -G admin meqdad
Output:
User "meqdad" is added to the "admin" group.
- Adding a comment for user "meqdad".
sudo usermod -c "Meqdad Dev" meqdad
Output:
The comment for user "meqdad" is updated to "Meqdad Dev".
chage
- Change user password expiry information.
Usage Case:
- Changing the maximum password age for user "meqdad" to 90 days.
sudo chage -M 90 meqdad
Output:
The maximum password age for user "meqdad" is set to 90 days.
- Changing the maximum password age for user "meqdad" to 10 days with warning message on the 7th day.
sudo chage -I 10 -W 7 meqdad
Output:
The password for user "meqdad" will expire in 10 days, and a warning will be issued 7 days prior.
- Changing the maximum password age for user "meqdad" to a specific date.
sudo chage -E 2024-12-31 meqdad
Output:
The password for user "meqdad" is set to expire on December 31, 2024.
find
- Search for files and directories in a directory hierarchy.
find [directory] [options] [expression]
Usage Case:
find /home/user -name "*.txt"
Output:
/home/user/file1.txt
/home/user/directory/file2.txt
...
- Removing the modified files for more than 7 days ago.
find /var/log -type f -mtime +7 -exec rm {} \;
Output:
Removes all files in /var/log that were modified more than 7 days ago.
grep
- Search for patterns in text using regular expressions.
grep [options] pattern [files/directories]
Usage Case:
grep "keyword" file.txt
Output:
Line containing the keyword in file.txt.
- Searching for the specified pattern in all files within the given directory and its subdirectories.
grep -r "pattern" /path/to/directory
Output:
Lines containing the pattern in files within the directory and its subdirectories.
- Showing information about Python processes running on the system:
ps aux | grep python
Output:
user 12345 0.0 0.0 1234 5678 ? S Jan01 0:00 python script.py
user 23456 0.0 0.0 1234 5678 ? S Jan01 0:00 /usr/bin/python3 another_script.py
locate
- Find files by name quickly using a pre-built index of file names.
locate [options] [pattern]
Usage Case:
locate test.txt
Output:
/home/user/test.txt
/var/www/html/test.txt
...
- Searching for files with the ".pdf" extension, ignoring case.
locate -i "*.pdf"
Output:
/home/user/Documents/Document1.pdf
/home/user/Documents/Document2.PDF
...
- Find files with ".pdf" extension and filter for those containing "example" in the filename.
locate -i "*.pdf" | grep "report"
Output:
/home/user/Documents/annual_report.pdf
/home/user/Downloads/example_report.pdf
...
systemctl
- Control the systemd system and service manager.
systemctl [options] [command] [unit]
Usage Case:
systemctl start nginx
Output:
Starts the nginx service.
- To display the status of the sshd service.
systemctl status sshd
Output:
Displays the status of the sshd service.
- To start a service automatically at boot.
systemctl enable apache2
Output:
Enables the apache2 service to start automatically at boot.
ps
- Report a snapshot of current processes.
.....
Usage Case:
- To display a list of processes running in the current session.
ps
Output:
PID TTY TIME CMD
5277 pts/1 00:00:00 bash
5284 pts/1 00:00:00 ps
- To display a detailed list of all processes, including those owned by other users.
ps aux
Output:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.1 0.1 167164 11740 ? Ss 21:50 0:01 /sbin/init splash
root 2 0.0 0.0 0 0 ? S 21:50 0:00 [kthreadd]
root 3 0.0 0.0 0 0 ? I< 21:50 0:00 [rcu_gp]
root 4 0.0 0.0 0 0 ? I< 21:50 0:00 [rcu_par_gp]
root 5 0.0 0.0 0 0 ? I< 21:50 0:00 [slub_flushwq]
root 6 0.0 0.0 0 0 ? I< 21:50 0:00 [netns]
...
...
root 1115 0.0 0.2 118256 23296 ? Ssl 21:50 0:00 /usr/bin/python3 /usr/share/unattended-upgrades/
mosquit+ 1122 0.0 0.1 15512 8320 ? Ss 21:50 0:00 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.
colord 1168 0.0 0.1 245572 13012 ? Ssl 21:50 0:00 /usr/libexec/colord
postgres 1214 0.0 0.3 221024 29696 ? Ss 21:50 0:00 /usr/lib/postgresql/14/bin/
postgres 1245 0.0 0.1 221136 9232 ? Ss 21:50 0:00 postgres: 14/main: checkpointer
- To display a full-format listing of all processes with additional information.
ps -ef
Output:
UID PID PPID C STIME TTY TIME CMD
root 1 0 0 21:50 ? 00:00:01 /sbin/init splash
root 2 0 0 21:50 ? 00:00:00 [kthreadd]
root 3 2 0 21:50 ? 00:00:00 [rcu_gp]
root 4 2 0 21:50 ? 00:00:00 [rcu_par_gp]
root 5 2 0 21:50 ? 00:00:00 [slub_flushwq]
root 6 2 0 21:50 ? 00:00:00 [netns]
...
...
lp 1221 1103 0 21:50 ? 00:00:00 /usr/lib/cups/notifier/dbus dbus://
rtkit 1241 1 0 21:50 ? 00:00:00 /usr/libexec/rtkit-daemon
postgres 1245 1214 0 21:50 ? 00:00:00 postgres: 14/main: checkpointer
postgres 1246 1214 0 21:50 ? 00:00:00 postgres: 14/main: background writer
postgres 1247 1214 0 21:50 ? 00:00:00 postgres: 14/main: walwriter
- To display a full-format listing of all processes with additional information and filters for Python processes.
ps -ef | grep python
Output:
root 1115 1 0 21:50 ? 00:00:00 /usr/bin/python3 /usr/share/unattended-upgrades/unattended-upgrade-shutdown --wait-for-signal
meqdad 5663 5277 0 22:22 pts/1 00:00:00 grep --color=auto python