Linux
In this page you'll find Linux commands and other useful information.
1. System Related Commands
| Command | What does it do? |
|---|---|
cat /etc/*release* | Check system version |
ls -ltrh /dev/video* | List all video devices picked up by the kernel |
lsusb | List all devices attached with USB |
lspci | List all devices attached with PCI |
top | Displays real time view of performance related data of all running processes in a system |
htop | Graphical top. Shows usage of CPU and Ram |
uptime | Shows the uptime and the load-average of the system |
sudo pkill <service> | Kill the service with the name or PID |
clear | Clear commandline |
which python3 | Which will tell where the file/program is in the system. If it Exist |
history | Shows the history of the commands in the command line |
!40 | Rerun the command with ID 40 without the need to rewrite the command |
!-2 | Rerun the 2-nd previous command |
!p | Rerun the command that starts with the letter (python) |
!! | Rerun the previous command |
sudo !! | Rerun the previous command with sudo |
^helllo^hello | Rerun the command with fixed grammar |
systemctl status apache2 | Checking the services that are running on the machine |
sudo systemctl disable apache2 | Removing the "Loaded" so the process does not start with the machine |
sudo systemctl enable apache2 | Adding "Loaded" so the process does starts with the machine |
sudo systemctl stop apache2 | This command will stop the process |
sudo systemctl start apache2 | This command will run the process |
sudo systemctl restart apache2 | This command will restart the process |
tail -n 4 /var/log/syslog | Viewing only the last 4 rows of logs |
head -n 4 /var/log/syslog | Viewing only the first 4 rows of logs |
jouurnalctl -u apache2 | View the logs only of the apache2 |
jouurnalctl -fu apache2 | View the logs in real time only of the apache2 |
<command> > file.txt | Overwrites the output to the file |
<command> >> file.txt | Appends the output to the file |
ls -l > file.txt | Saving the ls -l command output to the file.txt |
| ` | ` |
| `ls -l | grep file` |
| `cat file.txt | sort |
grep | grep limits the search criteria |
grep -v file.txt | Don't grep rows that contain the word file.txt ( -v is the same as not in python) |
status can be:
- Loaded: Enabled #Start when the machine is started.
- Active (running) #Telling us that the process is running.
- Active Time #We can check when did the process started
2. Resource Management Commands
| Command | What does it do? |
|---|---|
free | Tells us the amount of Memory used by the system |
free -m | The same but in MB |
df | Means disc free. Tells us how much diskspace is being used |
df -h | Makes it better and easier to read. "h" is human-readible. |
"free" memory is the memory that is not used at all by the system. "available" memory is the memory that exist but reserved for caches/system logs and stuff.
It's weird but Linux has a finite number of files. So we can have free disk space but if the nr.files is full we can't add more. These are called i-nodes.
3. File System Commands
| Command | What does it do? |
|---|---|
pwd | Print Working Directory. Tell where you are |
. | Current Directory |
ls | List Files and Folders in the working Dir |
ls -l | Give Long Listing |
ls -a | List Everything. Even Hidden Files ("." before the name) |
cd <dir_name> | Change Directory to the specified one |
cd .. | Go Above 1 Dir |
cd - | Go to the Previous Dir |
cd / | Go to Root Folder |
cd /home/<user_name> | Go to the Home Directory. Where Everything is. Like C-Drive |
cd ~ | Go to the Home Directory. Same as the above command |
mkdir <folder_name> | Make a new Directory |
touch <file_name>.<extention> | Create a file If it doesn't Exist |
cat > <file_name>.<extention> | Create a file from cat and write to it. CTR + C for closing it |
echo "" > <file_name>.<extention> | Create a file |
cat <file_name>.<extention> | Open file to check it |
cp <file_name>.<extention> <file_name_2>.<extention> | cp stands for copy |
rm test.txt | rm stands for remove. If you use rm it will remove the file from the system. You can't get it back |
mv test.txt ini | mv stands for move. File to the Directory |
mv *txt ini | Move all the files ending with txt to the Directory |
mv *txt .. | Move all the txt files to the Dir above |
mv ../test.txt . | 2 dots = parent Directory. 1 dot = current Directory. Move the File from the parent to the current Dir |
mv test1.txt test2.tx | mv is still used to rename files. Be careful with this. May overwrite without permission |
scp * <user>@<ip_address>:~/ | Secure copy all files from a Dir to another device at it's Home Dir |
scp -P <port_nr> <user>@ip_address:~/ | Secure copy files from a device to another at remote specified port |
mv <file_name>.<extention> <dir_path> | Move files from a directory to another |
mv <file_name>.<extention> <file_name_2>.<extention> | Rename files |
nano test.txt | Open nano = text editor to edit the test.txt file. Press "control + o" to save the edit |
cat test.txt | Will show the content of the file |
| `cat test.txt | head - 2` |
echo $? | Special variable. Returns 0 if the last command was succesful or not 0 if the last command was with error |
find / -name *.log | Find everyfile in the root-folder. Search by name that ends with .log |
find tes* | Find files that start with "tes" |
find *tes* | Find files that has "tes" somewhere in the name |
4. Network Commands
| Command | What does it do? |
|---|---|
ssh <user>@<ip_address> | Connect with SSH client with specified user |
| `sudo lsof -i -P -n | grep LISTEN` |
| `sudo netstat -ap | grep <port_nr>` |
ping -I wlan0 time.google.com | Ping with defined network interface. Here wlan0 |
5. Shortcut Commands
| Command | What does it do? |
|---|---|
| CTRL + Z | Shunts the process into the background, suspended |
| CTRL + C | Interrupt (kill) the current foreground process running in in the terminal |
| CTRL + D | Close the bash shell. Similar to exit |
| CTRL + L | Clear the screen. This is similar to running the “clear” command |
| CTRL + A or Home | Go to the beginning of the line |
| CTRL + E or End | Go to the end of the line |
| CTRL + ALT + (UP or Down) | Go to the other Workspace in Ubuntu |
6. User management
Root is the user with the most proviledge in the System. Permission string that we get when we press ls -l
| Beginning | Groups | Meaning |
|---|---|---|
| -rwxrwxr-x | 4 Sections(groups of 3 letters) | d = directory or - =file |
| rwx = user. Which user has the file | ||
| rwx = group | ||
| r-x = the others that are not in the group | ||
| r = read access | ||
| w = write access | ||
| x = execute access | ||
| - = No access/permission |
This command shows us all the users:
~$ cat /etc/passwd:shows ini : x : 1000: 1000 : /home/ini: /bin/bash #X is for the password.
#The numbers are the group ID and user ID| Command | What does it do? |
|---|---|
sudo cat /etc/shadow | Show the password-Hash saved in the system |
sudo !! | !! are used to rewrite the code that was used before |
groups | shows all the groups |
groups ini | shows the group ini is part of |
adduser batman | Adding the user with the name batman |
sudo - batman | Logging in into the new user |
logout | Logging out from the user |
sudo su - batman | Logging in into the new user without the need for the password |
passwd | Changing the password of the user |
sudo su - | Change to root. Do what ever you want |
sudo userdel -r batman | Removing the user with the name batman |
sudo groupadd heroes | Creating a group in the system |
sudo usermod -aG heroes ini | Adding the used ini to the group heroes. (logout and login to work) |
sudo gpasswd - d ini heroes | Removing a user from a group |
sudo groupdel heroes | Removing a group |
7. MISC
spd-say command can be a nifty tool.
Use this tool, for the system to say something or to inform you that a task finished:
~$ sudo apt-get update; spd-say "Task Finished"install gnome-tweak tool [sudo apt install gnome-tweak-tool] you can actually choose between coherent or independent behavior of your multiple monitors across workspaces. Once installed, launch tweak and then click on workspaces. then choose " Workspaces span displays" (instead of the default choice "Workspaces on primary display only" !)
\time #time option doesn't work #time is not a builtin; I mean it's not treated as a command; it's a keyword. #That's why tricks like \time
Streams in Linux: Input, Output and Error Streams
stdin (standart input) Input: stdout (standart output) Output: Every thing that returns info from the terminal is a standart Output Stream stderr (standart error) Error : >> ls ini3 #cannot access. #This is Error stream
Bash Alias
To create Bash aliases we do the following
~$ alias <alias_name>="<command>"
#Examples
~$ alias ll="ls -lh" #Better List
~$ alias lt="ls -h --size -1 -S --classify" #List files by size
~$ alias myip="curl ipinfo.io/ip" #Get Public IPTo make this alias permanent we need to modify the ./bashrc file.
~$ nano ~/.bashrcAfter adding the alias in the file and saving the changes we close the file and run:
~$ source ~/.bashrcExtra Tools and Programs:
vim
vim is a text editor like nano. vim works with "modes". Normally is the command mode.
| Command | What does it do? |
|---|---|
:q | Exit vim |
:q! | Exit vim without saving changes |
:x | Exit vim saving changes |
:wq | Exit vim saving changes, same as x |
i | Enter the Insert Mode |
:w | Save the file |
:w test.txt | Save a created file |
SHIFT + a | Enter Insert Mode and jump to the end of the Line |
o | Create a new line |
0 | Go to the beginning of the line |
$ | Go to the end of the line |
When we are in Command Mode
| Command | What does it do? |
|---|---|
x | Delete a Characters one at a time |
dd | Delete a Line |
3dd | Delete 3 Lines |
. | Redo the same command u used before |
/text | Find the first use of text string in the file |
n | Check the next time it was used |
Shift + n | Check the previous used case of the string |
:%s/string/string2 | Replace the string with the other string |
u | Undo the last change |
u then CTRL + r | Undo the previous changes |
30 then SHIFT + G | Go to line number 30 |
Screen
If you run a long running process or task on a machine that you are connected with SSH, and the connection drops, we lose the console we were connected to and the work with it. To be able to leave the task running, even if we lose connection, we can use the tool screen. Screen uses "virtual separate terminals". Even if we lose connection we can still attach to the session terminal.
To install screen we run:
~$ sudo apt-get install screenTo use screen, the most useful commands are:
~$ screen -S <name_screen_session> # Create a new screen terminal with a given name
~$ screen -ls # List all the screen terminals currently running
< CTRL + a + d on_keyboard > # Detach from screen terminal. Leave it running on background
~$ screen -r <id_screen> # Reattach to the screen terminal specified
~$ screen -d <id_screen> # Detach to the screen terminal specifiedwatch
watch is a very useful command in Linux to check the result of a command for regular interval times. We use it when we want to check the command output and how it changes with time. In order to not write the command ourselfs many time, we use this tool.
~$ watch -n 1 <linux_command>The -n tag is used to set the time interval that the command will be run. Default is 2 seconds. In order to use watch with commands that have a pipe | we need to put all the command in brackets
~$ watch -n 1 "<linux_command> | <linux_command>"wondershaper
wondershaper is a tool that is used to limit the bandwidth on a specific network card. Install wondershaper from Github. Do not install it with sudo apt-get because it will install an older not maintained version.
| Command | What does it do? |
|---|---|
sudo wondershaper -c -a eth0 | Clean the limits from adaptor. Needs to be done everytime |
sudo wondershaper -a eth0 -u 4096 -d 50000 | Limit Downlink and Uplink Bandwidth of Client |
iperf3
iperf3 is a tool used to measure the maximum bandwidth of a link. See Iperf Documentation for more information. This tool is a CPU intensive one and the results we get are always lower than theoretical maximum speed of a network link.
| Command | What does it do? |
|---|---|
iperf3 -s | Iperf Server |
iperf3 -s -D | Iperf Server in background as a Daemon |
iperf3 -c 192.168.1.8 | Iperf client |
iperf3 -c 192.168.1.8 -t 10 | Time in seconds to transmit |
iperf3 -c -u 192.168.1.8 | Use UDP instead of TCP. (Check Documentation) |
iperf3 -c 192.168.1.8 -R | -R. Client in reverse test mode – Server sends, client receives |
crontab
crontab is a tool used to schedule jobs in fixed time intervals. It is used to automate commands either time based or on system reboot See Crontab Documentation for more information.
To undertand more use this cool online tool Crontab Guru
Commands that need admin rights should be added to the root crontab file, that can be accessed with sudo:
~$ crontab -e
~$ sudo crontab -e# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday= 0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed| Command | What does it do? |
|---|---|
* * * * * | Every Minute |
1 * * * * | At Minute 1 each Hour Day etc |
5 0 * * * | At 00:05 each Day etc |
* * 1 * * | At each Minute each Day of month 1 (January) |
15 14 1 * * | At 14:15 each day of January |
0 22 * * 1-5 | At 22:00 for Monday till Friday |
23 0-23/2 * * * | At 00:23 01:23 02:23 each Day |
5 4 * * sun | At 04:05 each Sunday |
0,20,40 * * * * | Every 20 minute |
*/20 * * * * | Every 20 minute. Same as Above |
To add a new route in the route table on reboot we write the following command in the crontab file:
@reboot sleep 60 && route add -net <ip_address>/<CIDR> gw <gateway_ip> dev <device_name>