Linux Commands¶
The Shell - Bash¶
The shell, or the terminal is a really useful tool. Bash is the standard shell on most Linux distros.
Navigating¶
pwd
- Print working directorycd
- Change directorycd ~
- Change directory to your home directory
Looking at files¶
ls
- List files in directoryls -ltr
- Sort list by last modified. -time -reversefile
- Show info about file. What type of file it is. If it is a binary or text file for example.cat
- Output content of file.less
- Output file but just little bit at a time. Use this one. Notmore
./searchterm
Use it to search. It is the same command as in vim.n
to scroll to next search result. Pressq
to quit.more
- Output file but just little bit at a time.less
is better.
A little bit of everything¶
history
- Show commands historysudo
List what rights the sudo user has. Sudo config file is usually /etc/sudoers
Working with files¶
touch
- Create a new file.cp
- Copymkdir
- Make directory.mkdir -p new/thisonetoo/and/this/one
- Make entire directory structurerm
- Remove filerm -rf ./directory
- Remove recursively and its content. Very dangerous command!
Find
Find is slower than locate but a lot more thorough. You can search for files recursively and with regex and a lot of other features.
find / -name file 2>/dev/null
- This will send all permissions denied outputs to dev/null.
Locate
Locate is really fast because it relies on an internal database.
sudo updatedb
- Update the internal locate databaselocate example.txt
- Search for a file named example.txt
Which
Outputs the path of the binary that you are looking for. It searches through the directories that are defined in your $PATH variable.
- which bash
- Prints the path of bash. /bin/bash
Filters¶
There are certain programs that are especially useful to use together with pipes. They can also be used as stand-alone programs but you will often see them together with pipes.
sort
uniq
sort -u test.txtsort test.txt | uniqcat filename | sort -u > newFileName
grep
head
tail
tr
Editing text¶
sed
Can perform basic editing on streams, that is to say, text.
- sed -i '1d' example.txt
- Remove first line of example.txt
cut
Cut by column. This is a useful command to cut in text. Let's say that we have the following text, and we want to cut out the ip-address.
64 bytes from ip: icmp_req=1 ttl=255 time=4.86 ms
echo "64 bytes from ip: icmp_req=1 ttl=255 time=4.86 ms" | grep -oP '(\d+\.){3}\d+' | cut -d ' ' -f 4
- Cut out IP address.
tr - Translate
tr "[:lower:]" "[:upper:]" < file1 > file2
- Transform all letter into capital letters
Example Remove character - Remove characters
-
cat file.txt | tr -d "."
-
cat file.txt | tr "." "_"**
- Remove all dots and replace them with underscore.
awk
awk is an advanced tool for editing text-files. It is its own programming language to it can become quite complex. Awk iterates over the whole file line by line. Below is the basic structure of an awk command
-
awk '/search_pattern/ { action_to_take_on_matches; another_action; }' file_to_parse
- The search pattern takes regex. You can exclude the search portion or the action portion. -
awk '/172.16.40.10.81/' error.log
- Filtering out specific ip-address -
awk '/172.16.40.10.81/ {print $4}' error.log# Another exampleawk '{print $2,$5;}' error.txt
- This prints columns 2 and 5. -
awk -F ':' '{print $1}' test.txt
- Use the -F flag to add a custom delimiter
User Management¶
User management involves various commands for creating, modifying, and deleting user accounts, as well as managing user permissions and groups.
-
useradd: This command is used to add a new user account to the system.
-
passwd: This command is used to set or change the password for a user account.
-
userdel: This command is used to delete a user account from the system.
-
usermod: This command is used to modify user account properties such as username, home directory, shell, etc.
-
groupadd: This command is used to add a new group to the system.
-
groupdel: This command is used to delete a group from the system.
-
groups: This command displays the groups to which a user belongs.
-
usermod -aG: This command is used to add a user to a supplementary group.
-
chown: This command is used to change the owner of a file or directory.
-
chgrp: This command is used to change the group ownership of a file or directory.
Permissions¶
In Linux, file permissions control access to files and directories. There are three types of permissions: read (r), write (w), and execute (x). These permissions are assigned to three different entities: the file/directory owner, the group associated with the file/directory, and others (everyone else).
-
chmod: This command is used to change the permissions of a file or directory.
permissions
can be specified using symbolic notation (e.g., u+x for adding execute permission for the owner) or octal notation (e.g., - for rwxr-xr-x permissions).
-
chown: This command is used to change the owner and/or group of a file or directory.
-
chgrp: This command is used to change the group ownership of a file or directory.
-
ls: This command is used to list files and directories, and it also shows their permissions.
-
umask: This command sets the default permissions for newly created files and directories.
-
setfacl: This command is used to set Access Control Lists (ACLs) for files and directories, allowing more fine-grained control over permissions.
-
getfacl: This command is used to view the ACLs of files and directories.
https://linuxjourney.com/lesson/file-permissions
Processes¶
In Linux, process commands are used to manage and interact with processes running on the system.
-
ps: This command is used to display information about active processes.
- To display more detailed information, you can use options like
-aux
,-ef
, or-e
.
- To display more detailed information, you can use options like
-
top: This command provides dynamic real-time information about running processes, CPU usage, memory usage, etc.
-
htop: Similar to top but with a more user-friendly interface and additional features.
-
kill: This command is used to terminate processes by sending signals.
- The default signal is SIGTERM (terminate), but you can also send other signals like SIGKILL (force termination) using `-- option.
-
killall: This command is used to kill processes by name rather than by PID.
-
pgrep: This command is used to search for processes based on name or other attributes and print their PIDs.
-
pkill: This command is used to send signals to processes based on their names or other attributes.
-
nice: This command is used to launch a process with a specified priority (niceness).
-
renice: This command is used to change the priority (niceness) of an already running process.
-
jobs: This command is used to display currently running jobs in the shell.
Package management¶
Package management is typically done using the Advanced Packaging Tool (APT) and its various frontends. (in Ubuntu, other linux distributions uses other package managers)
-
apt-get: This is a command-line tool used to handle package management tasks, such as installing, updating, upgrading, and removing packages.
-
Install a package:
-
Remove a package:
-
Update the package list (repository information):
-
Upgrade installed packages to their latest versions:
-
-
apt: A more user-friendly frontend to apt-get. It provides colorful output and progress bars.
- Install a package:
-
apt-cache: This command is used to search and query information about packages available in APT's package cache.
-
Search for a package:
-
Show information about a specific package:
-
-
dpkg: This is a low-level package management tool. It can be used to install, remove, and query information about individual packages.
-
Install a package (with its full path):
-
Remove a package (keeping its configuration files):
-
Remove a package (including its configuration files):
-
-
aptitude: Another frontend for APT. It provides a text-based interface for package management tasks and dependency resolution.
- Install a package:
-
snap: Ubuntu also supports snap packages, which are self-contained applications bundled with their dependencies.
-
Install a snap package:
-
Remove a snap package:
-
-
Adding a Path: This is a non-persistent way to add binaries to your path. Might be useful if you have entered a system that has limited binaries in the path.
Cronjobs¶
Cronjobs are scheduled tasks in Unix-like operating systems, including Linux, that run at predefined times or intervals. They are managed using the cron daemon.
-
crontab: This command is used to create, edit, and list cronjobs for a user.
-
cron.allow and cron.deny: These files are used to control access to the cron daemon. If
cron.allow
exists, only users listed in it are allowed to use cron. Ifcron.allow
does not exist butcron.deny
does, users listed incron.deny
are not allowed to use cron. If neither file exists, only the superuser can use cron. -
/etc/cron.d/: This directory contains system-specific cronjobs. Instead of using the crontab command, system administrators can place cronjob files directly in this directory. The format of these files is the same as the user crontab files.
-
/etc/cron.{hourly,daily,weekly,monthly}/: These directories contain system-wide scripts that are executed hourly, daily, weekly, or monthly. Scripts placed in these directories will be run automatically at the specified intervals.
-
systemctl: On modern Linux distributions using systemd, you can control the cron service using systemctl.
Devices¶
In Linux, devices commands are used to manage and interact with hardware devices connected to the system. These commands help in viewing device information, configuring devices, and diagnosing issues.
-
lsblk: This command lists information about block devices (e.g., hard drives, solid-state drives) connected to the system.
-
lspci: This command lists all PCI buses and devices connected to them.
-
lsusb: This command lists USB buses and devices connected to them.
-
lshw: This command provides detailed information about hardware configuration, including devices and their drivers.
-
dmesg: This command displays the kernel ring buffer, which contains information about device detection and driver initialization messages.
-
hwinfo: This command provides detailed hardware information, including devices and their configuration.
-
udevadm: This command is used to manage device nodes in the udev (device manager) system.
-
hdparm: This command is used to get/set ATA/SATA device parameters like reading/writing speed, power management, etc.
-
fdisk and parted: These commands are used for disk partitioning and management.
-
lsdev: This command lists all available devices on the system.
Mount¶
In Linux, the mount
command is used to mount file systems onto directories within the Linux filesystem.
-
Mounting a File System:
- This command mounts the file system located on device
/dev/sdXY
to the directory/mnt/mydisk
. Replace/dev/sdXY
with the appropriate device identifier (e.g., `/dev/sda- for the first partition on the first SCSI/SATA/USB drive).
- This command mounts the file system located on device
-
Mounting All File Systems Listed in
/etc/fstab
:- This command mounts all file systems listed in the
/etc/fstab
file that are not already mounted.
- This command mounts all file systems listed in the
-
Unmounting a File System:
- This command unmounts the file system mounted on
/mnt/mydisk
. Replace/mnt/mydisk
with the appropriate mount point.
- This command unmounts the file system mounted on
-
Mounting a Network File System (NFS):
- This command mounts a remote NFS share located at
server:/remote/directory
to the local directory/mnt/mountpoint
.
- This command mounts a remote NFS share located at
-
Mounting a CD-ROM or DVD:
- This command mounts the CD-ROM or DVD drive to the directory
/mnt/cdrom
.
- This command mounts the CD-ROM or DVD drive to the directory
-
Viewing Mounted File Systems:
- This command displays a list of all currently mounted file systems.
-
Bind Mounting:
- This command mounts a directory at another location, making the content of both directories accessible at both mount points.
-
Remounting a File System with Different Options:
- This command remounts the file system mounted on
/mnt/mydisk
, changing its options. In this example, it remounts it with read-write permissions.
- This command remounts the file system mounted on
Controlling services¶
In Linux, services are background processes that run continuously to provide specific functionalities. Controlling services involves starting, stopping, restarting, enabling at boot time, and disabling services as needed.
-
systemctl: This command is used to control systemd services in modern Linux distributions such as Ubuntu, CentOS, and Fedora.
-
Start a service:
-
Stop a service:
-
Restart a service:
-
Enable a service to start automatically at boot time:
-
Disable a service from starting automatically at boot time:
-
Check the status of a service:
-
-
service: This command is used to control SysVinit services, commonly found in older Linux distributions.
-
Start a service:
-
Stop a service:
-
Restart a service:
-
Check the status of a service:
-
-
chkconfig: This command is used to enable or disable services to start at boot time on SysVinit-based systems.
-
Enable a service at boot time:
-
Disable a service from starting at boot time:
-
-
update-rc.d: This command is used to manage System V (SysV) init scripts on Debian-based systems.
-
Enable a service at boot time:
-
Disable a service from starting at boot time:
-
These commands allow administrators to control the operation of services on Linux systems effectively. They are crucial for managing server processes, ensuring that essential services start automatically after system reboots, and troubleshooting service-related issues. The specific commands to use may vary depending on the Linux distribution and the init system being used (SysVinit or systemd).
Network basics¶
In Linux, there are several commands available to manage and troubleshoot network-related tasks.
-
ifconfig: This command displays the configuration of network interfaces, including IP addresses, MAC addresses, and network-related statistics.
- Note:
ifconfig
has been deprecated on many modern Linux distributions in favor ofip
command.
- Note:
-
ip: This command is a more modern replacement for
ifconfig
and provides extensive functionality for configuring network interfaces, routing tables, and more.-
Show information about network interfaces:
-
Show routing table:
-
-
ping: This command is used to send ICMP echo requests to a specified host to check network connectivity.
-
traceroute: This command is used to trace the route that packets take from your computer to a specified destination host.
-
netstat: This command displays network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.
- Show network connections:
-
ss: This command is a modern replacement for
netstat
and provides similar functionality.- Show socket statistics:
-
dig: This command is used to perform DNS (Domain Name System) queries such as looking up IP addresses associated with domain names.
-
nslookup: This command is used to query DNS servers to obtain domain name or IP address mapping.
-
hostname: This command displays the hostname of the system.
-
ifup/ifdown: These commands are used to bring network interfaces up or down manually.
Firewall¶
In Linux, a firewall is a network security system that monitors and controls incoming and outgoing network traffic based on predetermined security rules. The firewall can be implemented using various tools such as iptables, nftables, and firewalld. These tools allow administrators to define rules that specify which network packets are allowed or denied based on criteria such as source and destination IP addresses, ports, and protocols.
Here are some key concepts related to firewalls in Linux:
-
Packet Filtering: A firewall examines each network packet passing through it and decides whether to allow or block it based on predefined rules. Packet filtering is the fundamental functionality of a firewall.
-
Chains: In Linux firewall configurations, rules are organized into chains. Each chain is a list of rules that are applied sequentially to incoming or outgoing packets. Commonly used chains in Linux firewalls include:
-
INPUT chain: Used for packets destined for the local system.
- OUTPUT chain: Used for packets originating from the local system.
-
FORWARD chain: Used for packets that are being routed through the system.
-
Default Policies: Each chain has a default policy that specifies what action should be taken if a packet does not match any of the rules in the chain. Common default policies are ACCEPT, DROP, and REJECT.
-
Rules: Rules are the individual instructions that determine whether a packet is allowed or denied. Each rule consists of criteria (such as source and destination addresses, ports, and protocols) and an action (such as ACCEPT, DROP, or REJECT). Rules are evaluated in order, and the action of the first matching rule is applied.
-
Stateful Inspection: Some firewall configurations support stateful inspection, which tracks the state of active network connections. This allows the firewall to make more informed decisions based on the state of the connection, such as allowing return traffic for established connections.
-
Logging: Firewalls can be configured to log information about packets that are blocked or allowed. Logging helps administrators monitor network traffic and troubleshoot firewall issues.
-
Network Address Translation (NAT): In addition to packet filtering, firewalls can also perform Network Address Translation (NAT) to modify the source or destination IP addresses of packets as they pass through the firewall. NAT is often used to hide internal IP addresses or to map multiple internal IP addresses to a single external IP address.
iptables¶
In Linux, iptables
is a powerful firewall utility that allows administrators to configure rules for packet filtering and network address translation (NAT).
- Listing Current Rules:
-
List all current rules:
-
Creating Rules:
- Add a rule to allow traffic on a specific port (e.g., TCP port 80):
- Add a rule to allow traffic from a specific IP address:
-
Add a rule to block traffic from a specific IP address:
-
Deleting Rules:
-
Delete a specific rule (use
-D
followed by the rule number as listed iniptables -L
): -
Flushing Rules:
-
Flush all rules (delete all rules from the specified chain):
-
Setting Policies:
-
Set the default policy for a chain (e.g., INPUT, OUTPUT, FORWARD) to DROP:
-
Saving and Restoring Rules:
- Save current rules to a file:
-
Restore rules from a file:
-
Miscellaneous Commands:
- Check detailed information about packet counters and byte counts: