Basics of linux
This is a huge chapter. I could divide it up in many subchapters but I like to have it all at one place so I can just do ctr-f
, and search for whatever I am looking for.
1. The Shell - Bash
The shell, or the terminal is a really useful tool. Bash is the standard shell on most Linux distros.
One really useful trick when working with bash is to search for old commands that you have used. You can access this search function by doing ctr-r
in the terminal.
Navigating
pwd
- Print working directory
cd
- Change directory
cd ~
- Change directory to your home directory
cd -
- Go back to previous directory
Looking at files
ls
- List files in directory
ls -ltr
- Sort list by last modified. -time -reverse
file
- 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. Not more
.
Use /searchterm
to search. It is the same command as in vim. n
to scroll to next search result. Press q
to quit.
more
- Output file but just little bit at a time. less
is better.
Working with files
touch
- Create a new file.
cp
- Copy
mkdir
- Make directory.
rm
- Remove file
Watch the command destroy an entire machine: https://www.youtube.com/watch?v=D4fzInlyYQo
rmdir
- Remove empty directory
A little bit of everything
history
- Show commands history
sudo
List what rights the sudo user has.
Sudo config file is usually /etc/sudoers
Finding files
There are mainly three ways to find files on Linux: find, locate, and which.
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.
Locate
Locate is really fast because it relies on an internal database. So in order to have it updated you need to run:
Then you can easily find stuff like this:
Which
Outputs the path of the binary that you are looking for. It searches through the directories that are defined in your $PATH variable.
2. Editing text
First let's just clear out something about standard streams, or I/O-streams. Standard streams are the streams that are used to interact between the human computer-user and the machine. There are three standard streams: standard input (stdin), standard output (stdout), and standard error (stderr).The stdin stream can be seen as an abstractions of the real keyboard input. So when you issue a command/program that requires input the program does not read straight from the keyboard input, instead it reads from the file STDIN.
Stdin
Stdin is the data that gets inputed into the program. An example of a program that requires stdin data is cp
. In order for the program to do anything it needs input data. For example cp file1 copy_of_file1
. Here file1
and copy_of_file1
is the stdin.
So the default Stdin comes from the STDIN-file that is a text-file representation of the keyboard input. But often times we do not want to input stuff from the keyboard, sometimes we want to input something into a program that comes from another file. That is when we can use redirection symbol: >
.
So an example could be cat < my_text_file.txt
. The data from my_text_file.txt will now be used as input instead of the keyboard input.
The file descriptor for stdin is: 0
Stdout
Stdout is the data that get ouputed from the program.
For example, when you use the command cat file1
that data/text that gets outputed is the stdout The same with the program ls
. Not all programs have stdout. For example when you use mv
or cp
successfully you get no stdout back from the program.
The stdout can be redirected to another file by using these symbols >
and >>
. So now we can do the following:
Another incredibly useful feature is the pipe feature, reprsented with this symbol |
. It will take the stdout and redirect it into another program. Here is an example:
This will take the stdout from ls -la
and forward/redirect it into the less
program. Using the pipe you can now chain different commands.
The file descriptor for stdout is: 1
Stderr
Stderr is the stream used for outputting error messages. So if a program fails for whatever reason. For example, if we try to copy a file that does not exist, this will be the stdrr output:
This is a common way for stderr to present itself, just straight out into the terminal. But sometimes stderr gets sent to a log file.
Stderr is useful because with it we can separate between stdout and stderr. However, to the eye it might be difficult to distinguish what output is stdout and what output is stderr.
One easy way to determine is the output is stderr or stdout is to simply redirect it into a file. Because by default you only redirect stdout, and not stderr.
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
grep
head
tail
tr
sed
Editing text
sed
Can perform basic editing on streams, that is to say, text.
Remove first line of file/stream
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.
-d
stands for delimiter. and -f
for field.
tr - Translate
Transform all letter into capital letters
Example Remove character
http://www.thegeekstuff.com/2012/12/linux-tr-command/
awk
So 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.
This is the basic structure of an awk command
The search pattern takes regex. You can exclude the search portion or the action portion.
This just prints every line of the file.
Filtering out specific ip-address:
Now we want to print out the fourth column of that file, we can just pipe this to cut, but we can also use awk for it, like this:
We can use the -F flag to add a custom delimiter.
So if you are manipulating some text you might want to start the output with some info about the columns or something like that. To do that we can use the BEGIN-keyword.
Here we are printing IP-address PORT to the first line of the file.
3. User management
To add a user we do:
To add user to sudo-group:
On some machines we might not be able to edit the sudoers file because we don't have an interactive shell, in this case can you can just redirect the text into the file, like this:
Check which users are in the sudo group:
Switch user in terminal:
Remove/delete user:
4. Permissions
Shows all the files and directories and their permission settings.
Here we have 10 letters in the beginning. The first one d
shows that it is a directory.
The next three letters are for read, w
for write and x
for execute. The first three belong to the owner, the second three to the group, and the last three to all users.
https://linuxjourney.com/lesson/file-permissions
5. Processes
To display information regarding the systems processes you can use the ps
command.
-a
stands for all
-u
stands for all processes by all users
-x
stands for all processes that don't run a tty
If you run this command you will probably see a pretty big output. In the column for command you will see what command has been run. Every process has a Process Identification Number (PID). Something you will also see in the output.
All of theses processes can actually be found in /proc
. You just go to /proc/[pid]
. In /proc
you can find information about the system, and you can actually change the system if you change those files! But more on that later. What I wanted to explain is that if we look at the output from ps
we see that some commands are in brackets. Like this:
Those are usually kernel processes, and you can safely assume that no user has started them.
If you want to monitor processes in real time you can use top
or htop
. top
comes preinstalled on most distros. But htop
is really a lot nicer.
For htop
the F1-10 keys might trigger OS-events. So you can use the shortcuts instead.
http://www.thegeekstuff.com/2011/09/linux-htop-examples/
6. Packages
Something that difference Linux from windows is how it handles installing new software. In windows you usually have to google around and then click on random scary download buttons that might fuck up your computer, or not. It's like a constant lottery where you win by no installing malware. In Linux that is usually not really an issue. That is because distros have their own software repositories from where you can download your software. This is kind of like an app-store except everything is free.
The different major branches of teh GNU/Linux OS have their own software repositories. Ubuntu has their own, debian has their own, and so on.
Different distros also have their own package-amangers. For example, Debian and ubuntu uses apt
, while Redhat uses rpm
, and Arch uses pacman
. You should strick to your own package-manager, because even though chaning package-manager is possible it will probably just cause you more headache than benefits.
Install package
Example of how to install something with apt:
If you only have a .deb file you do this to install from the terminal:
Remove packages
This can be tricky. First find the package
Then you find it in your list.
When you remove some package it might have requires some other dependencies. To remove those you run
Organizing your $path variable
I am talking about debian/ubuntu here. On other systems I don't know.
You can define your path in /etc/environment
. If you don't have it you can create it and add the path like this:
If you are using zsh (which you should) you have to add it here
And add this line somewhere:
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.
Installing custom packages
If you download a package that is not in the official repository you can put the binary in /opt
. That is good place to put your binaries.
Now you need to add that path to your path-variable. Remember how we set that in /etc/environment
. So now open up that file and add /opt
to it, so i looks like this.
I always add custom binaries last. That means that if we have two binaries with the same name the machine will first select the original binary. This way you won't have to fear screwing up, by accidentally creating a new ls
binary for example.
7. Cronjobs
There are two ways to configure cronjobs. The first one is by putting scripts in the following folders.
The second way is to write the command in the crontab
8. Devices
List all devices
9. The Filesystem
The Filesystem Hierarchy Standard
Difference between sbin and bin
sbin is system binaries. A normal user do not have access to these binaries. It is only root and users with sudo privileges that do.
We have echo, cp, grep. The normal stuff a user needs.
In sbin we have binaries that control the system.
Mount
So everything on the linux-filesystem belongs to some part of the filesystem-tree. So if we plug in some device we need to mount it to the filesystem. That pretty much means that we need to connect it to the filesystem. Mount is like another word for connect.
So if you want to connect a CD-rom or USB to your machine. You need to mount it to a specific path on the filesystem.
So if you plug in the usb it might be accessible at /dev/usb. But that it not enough for you to be able to browse the usb content. You need to mount it. You do this by writing
Or whereever you want to mount it.
So when you click on Eject or Safetly remove you are just unmounting.
Knowing how to mount and unmount might be useful if you want to get access to a remote NFS-directory. You will need to mount it to your filesystem to be able to browse it.
10. Controlling services
Systemctl
Systemctl can be used to enable and disable various services on your linux machine. Start ssh
You can verify that the service is listening for connection by running network status.
Make ssh start upon boot
Init.d
Init.d is just a wrapper around Systemctl. I prefer it.
rcconf
This is a tool to control services more easily, what is running upon boot and so on.
11. Kernel
The Kernel is responsible for talking between the hardware and the software, and to manage the systems resources.
The Linux Kernel is a monolithic kernel, unlike the OSX and the Windows kernels which are hybrid.
You can find the kernel file in /boot
. It might look like something like thisvmlinuz-4.4.0-57-generic
. In the beginning of time the kernel was simply called linux
. But when Virtual Memory was introduced they changed the name to vmlinux
to reflect that the kernel could handle virtual memory. When the kernel later became too big it was compressed using zlib , therefore the name was changed to vmlinuz
.
The Linux Kernel differs from Windows in that it contains drivers by default. So you don't have to go around looking for drivers like you do on windows when you want to install a printer, or something like that.
It is really easy to upgrade to the latest Linux kernel, all you have to do tis this:
If you are using a distro that is Long Term Supported (LTS). You will not get the latest Kernel version, but you will get the latest Long Term Supported version.
14. Logging
Logs can be viewed here on debian distros /var/log/
16. Network basics
Netstat - Find outgoing and incoming connections
Netstat is a multiplatform tool. So it works on both mac, windows and linux.
A few interesting things to observe here is that my machine is using any port over 1024 to connect to the outside. So it is not like just because we communicate with https and connect to port 443 that we use that port on our machine. On our machine it can be any port (over 1024) and usually any port over 10000.
Find out what services are listening for connection on your machine Flags
To easily check out what process is using lots of bandwidth you can use nethogs.
Or you can use tcpdump, or iptables.
Every listening process of course has a PID, but unless you are root you can't might not see them all.
Firewall - Iptables
Iptables is a firewall tool in linux. A firewall is basically a tool that scans incoming and/or outgoing traffic. You can add rules to the iptables to filter for certain traffic.
Types of chains
So you can filter traffic in three different ways input, forward, and output. These are called three different chains.
INPUT This is for incoming connections. If someone wants to ssh into your machine. Or a web-server responds to your request.
FORWARD This chain is used for traffic that is not aimed at your machine. A router for example usually just passes information on. Most connections are just passing through. As you can see this will probably not be used so much on your machine, as a normal desktop or a server doesn't router that much traffic.
OUTPUT
This chain is used for outgoing traffic.
Active rules
To view your active rules you do
So as we can see the current policy is to accept all traffic in all directions.
If you for some reason has been tampering with the iptables and maybe fucked up. This is how you return it to the default setting, accepting all connections
If you instead want to forbid all traffic you do
Okay, so let's block out some connections. To do that we want to add/append a new rule. We want to block all connections from our enemy 192.168.1.30.
Now if we want to see our current rules we just do
And we can now see our new rule.
To add line-numbers for each rule, so that you can then specify which rule you want to reset or change or something you can output the rluels with line-numbers
Remove/delete a rule To remove a rule you just do
Save your changes Your changes will only be saved and therefore in action until you restart iptables. So they will disappear every time you reboot unless you save the changes. To save the changes on ubuntu you do
Measuring bandwidth usage
There are a few different tools in hour arsenal that we can use to measure bandwidth usage. We will start with iptables.
To view the input and output traffic we just list the rules with some verbosity.
So clean this up and reset the count we can do the following
So now we just need to add our rules. A simple script for this would be
Then check out the traffc with
Examples
Block outgoing connections to a specific ip
https://www.digitalocean.com/community/tutorials/how-to-list-and-delete-iptables-firewall-rules
Troubleshooting
Have you tried turning it on and off?
I have had problems with the network-adapter not starting or something like that, on Ubuntu. You can try to restart the network manager if this happens:
Magical rfkill
If for some reason the wifi is blocked you can unblock it (or block it) with rfkill.
To block or unblock the phy0 from the example above you do:
If there is a hard block it means that there is a physical switch on you machine that you need to switch off.
17. Subnetting
18. Routing
21. DNS
References
https://linuxjourney.com/ https://github.com/jlevy/the-art-of-command-line
Last updated