2021-04-07 13:08:22 +02:00
# Linux
2021-04-06 16:21:38 +02:00
2021-04-07 12:56:36 +02:00
## Absolute Path vs Relative Path in Linux/Unix
One of this blog follower asked us that whatâs the difference between absolute and relative path?
To understand this we have to know what is a path in Linux.
### What is a path?
A path is a unique location to a file or a folder in a file system of an OS. A path to a file is a combination of / and alpha-numeric characters.
What is an absolute path?
An absolute path is defined as the specifying the location of a file or directory from the root directory(/). In other words we can say absolute path is a complete path from start of actual filesystem from / directory.
### Some examples of absolute path:
```bash
/var/ftp/pub
/etc/samba.smb.conf
/boot/grub/grub.conf
```
If you see all these paths started from / directory which is a root directory for every Linux/Unix machines.
### What is the relative path?
Relative path is defined as path related to the present working directory(pwd). Suppose I am located in /var/log and I want to change directory to /var/log/kernel. I can use relative path concept to change directory to kernel
changing directory to /var/log/kernel by using relative path concept.
2021-04-08 10:30:33 +02:00
```bash
$ pwd/var/logcd kernel
```
2021-04-07 12:56:36 +02:00
Note: If you observe there is no / before kernel which indicates itâs a relative directory to present working directory.
2021-04-06 11:56:51 +02:00
2021-04-07 12:56:36 +02:00
Changing directory to /var/log/kernel using absolute path concept.
2021-04-08 10:30:33 +02:00
```bash
$ cd /var/log/kernel
```
2021-04-07 12:56:36 +02:00
Note: We can use an absolute path from any location where as if you want to use relative path we should be present in a directory where we are going to specify relative to that present working directory.
Examples of relative path and absolute path for the same operation.
# Basic Linux Command
## help
2021-04-08 10:30:33 +02:00
### Man: an interface to the on-line reference manuals
```bash
$ man ls
```
2021-04-06 16:21:38 +02:00
2021-04-07 13:08:22 +02:00
## Wildcard
2021-04-08 10:30:33 +02:00
### \* Show all picture (multiple char)
```bash
2021-04-07 13:08:22 +02:00
> $ ls pic*
2021-04-08 10:30:33 +02:00
### **?** show only picture between 50 and 59 (only one char)
2021-04-07 13:08:22 +02:00
> $ ls pic5?.jpg
2021-04-07 12:56:36 +02:00
[] show only if char in [p-P]
2021-04-07 13:08:22 +02:00
2021-04-07 12:56:36 +02:00
> $ ls [pP]ic*
2021-04-07 13:08:22 +02:00
## Files and directory
2021-04-07 12:56:36 +02:00
- ls: list directory contents
> $ ls
> args = -l -a -r -t -h --help
- Pwd: output the current working directory
2021-04-06 16:21:38 +02:00
> $ pwd
2021-04-07 13:08:22 +02:00
- Cd: Change directory
> $ cd
2021-04-07 12:56:36 +02:00
back to previous folder
2021-04-07 13:08:22 +02:00
> $ cd -
2021-04-06 16:21:38 +02:00
2021-04-07 13:08:22 +02:00
- Mkdir: Make directory
2021-04-06 16:21:38 +02:00
> $ mkdir test1 test2 test3
2021-04-07 13:08:22 +02:00
> $ mkdir -p lol/lol/lol
2021-04-06 16:21:38 +02:00
-p to create parent directory if needed
2021-04-08 10:30:33 +02:00
### Rmdir: Remove directory
```bash
$ rmdir filename
2021-04-07 12:56:36 +02:00
2021-04-08 10:30:33 +02:00
$ rm -rf filename
```
2021-04-07 12:56:36 +02:00
2021-04-07 16:18:32 +02:00
Delete all of the files in the diectory including all subdirectories and tier contents
2021-04-08 10:30:33 +02:00
```bash
$ rm -r \* .\*
```
2021-04-07 16:18:32 +02:00
Remove all files with the .doc extension recursively in the current working directory.
2021-04-08 10:30:33 +02:00
```bash
$ rm \*\*/\*.doc
```
2021-04-07 16:18:32 +02:00
2021-04-08 10:30:33 +02:00
### Mv: Move directory (can be used to rename a file)
```bash
$ mv file /opt/movedfile
```
2021-04-07 12:56:36 +02:00
2021-04-08 10:30:33 +02:00
### Cp: Copy file or directory
```bash
$ cp file /opt/newcopiedfile
```
2021-04-07 12:56:36 +02:00
2021-04-08 10:30:33 +02:00
### Touch: change file timestamps but it can also create files
```bash
$ touch nomdefichier.md
2021-04-07 12:56:36 +02:00
2021-04-08 10:30:33 +02:00
$ touch pic{00..99}.jpeg # does not work
```
2021-04-07 13:08:22 +02:00
2021-04-08 10:30:33 +02:00
### Which: Searching the PATH for executable files matching the names of the arguments
```bash
$ which ls
```
### File: file — determine file type
```bash
$ file myfile
```
2021-04-07 12:56:36 +02:00
## file viewer
2021-04-08 10:30:33 +02:00
### More: file perusal filter for crt viewing
```bash
$ more filename
```
### Less: opposite of more but Less is more ;)
```bash
$ less filename
```
### Cat: concatenate files and print on the standard output
```bash
$ cat filename
```
### tail : output the last part of files
2021-04-07 12:56:36 +02:00
2021-04-08 10:30:33 +02:00
```bash
$ tail -n 5 Workspace/SysAdminTraining/LinuxSysAdminsDoc/Linux/basic_cmd.md
- dhclient > get ip
- gnome networkmanager
- wpa_supplicant > encryption @ wifi
2021-04-07 12:56:36 +02:00
2021-04-08 10:30:33 +02:00
![htop ](./img/htop.png )
```
2021-04-07 13:08:22 +02:00
2021-04-08 10:30:33 +02:00
Args -n define the number of line needed
2021-04-07 13:08:22 +02:00
## Users
2021-04-07 12:56:36 +02:00
adduser, addgroup - add a user or group to the system
```bash
2021-04-08 10:30:33 +02:00
$ sudo adduser steve
2021-04-07 12:56:36 +02:00
[sudo] password for r4v3n:
Sorry, try again.
[sudo] password for r4v3n:
Adding user `steve' ...
Adding new group `steve' (1002) ...
Adding new user `steve' (1002) with group ` steve' ...
Creating home directory `/home/steve' ...
Copying files from `/etc/skel' ...
New password:
Retype new password:
passwd: password updated successfully
Changing the user information for steve
Enter the new value, or press ENTER for the default
Full Name []:
Room Number []:
Work Phone []:
Home Phone []:
Other []:
Is the information correct? [Y/n] y
2021-04-07 13:08:22 +02:00
```
2021-04-07 12:56:36 +02:00
user skeleton: skeleton files used for new user configuration
> $ ls /etc/skel/
```bash
─r4v3n at d3bi4n in /etc/skel
╰─» ls -a 0 (0.002s) < 05:21:24
./ ../ .bash_logout .bashrc .profile
```
- Change user
> $ su - marie
2021-04-07 13:08:22 +02:00
## Permissions
2021-04-07 12:56:36 +02:00
- Chmod: change file mode bits
A combination of the letters **ugoa** controls which users' access to the file will be changed:
- the user who owns it (u),
- other users in the file's group (g),
- other users not in the file's group (o),
- or all users (a).
If none of these are given, the effect is as if (a) were given, but bits that are set in the umask are not affected.
2021-04-07 13:08:22 +02:00
![file permissions ](./img/file_permission.png )
2021-04-07 12:56:36 +02:00
```bash
marie@d3bi4n:~$ ls -l
total 4
-rw-r--r-- 1 marie marie 12 Apr 7 05:44 test
```
> $ chmod o-r mysecret
> -rw-r--**-**-- 1 marie marie 12 Apr 7 05:44 test
> $ chmod o+**rw** myscret
> -rw-r--**rw**- 1 marie marie 12 Apr 7 05:44 test
2021-04-07 13:08:22 +02:00
2021-04-07 16:18:32 +02:00
##Using chmod in absolute mode
In the absolute mode, permissions are represented in numeric form (octal system to be precise). In this system, each file permission is represented by a number.
- r (read) = 4
- w (write) = 2
- x (execute) = 1
- – (no permission) = 0
With these numeric values, you can combine them and thus one number can be used to represent the entire permission set.
| Number | Permission |
|--------|------------|
| 0 | — |
| 1 | – x |
| 2 | -w- |
| 3 (i.e. 2+1) | -wx |
| 4 | r– |
| 5 (i.e. 4+1) | r-x |
| 6 (i.e. 4+2) | rw- |
| 7 (i.e. 4+2+1) | rwx |
most commonly used:
755 644 600 640
Can you guess the file permission in numbers on agatha.txt file in our example so far? That’ s right, it’ s 764.
Now that you know what number represents which permission, let’ s see how to change file permission using this knowledge.
Suppose you want to change the file permission on agatha.txt so that everyone can read and write but no one can execute it? In that case, you can use the chmod command like this:
> $ chmod 666 agatha.txt
## Danger : if a folder has not the X (executable) right => you cannot open it.
-R for recursive on folder
If you list agatha.txt now, you’ ll see that the permission has been changed.
> -rw-rw-rw- 1 abhishek abhishek 457 Aug 10 11:55 agatha.txt
- Chown: change file owner and group
> $ sudo chown marie:marie agatha.txt
> -rw-rw-rw- 1 marie marie 457 Aug 10 11:56 agatha.txt
2021-04-07 13:08:22 +02:00
### Groups
2021-04-07 12:56:36 +02:00
Groups: print the groups a user is in
> $ groups
Adds user marie into steve group
> $ adduser marie steve
```bash
r4v3n@d3bi4n:~/Workspace/test$ sudo adduser marie steve
Adding user `marie' to group ` steve' ...
Adding user marie to group steve
Done.
marie@d3bi4n:~$ groups
marie steve
```
## Sysadmin tools
2021-04-08 10:30:33 +02:00
### & vs &&
```bash
$ apt update & & upgrade
# && launch both instance one after the other
$ sleep 10 & htop
& launch in background the sleep 10 process and open htop
```
### exit status
```bash
$ ls thisnotexist
ls: cannot access 'thisnotexist': No such file or directory
$ echo $?
2
$ ls
Desktop Documents Downloads Music Pictures Public Templates Videos Workspace
$ echo $?
0
r4v3n@d3bi4n:~$
```
### Display Environement variables
```bash
$ env
SHELL=/bin/bash
XDG_CURRENT_DESKTOP=GNOME
...
```
### Get users password hases:
```bash
$ cat /etc/shadow | grep bash
# get hashes
$ sudo cat /etc/shadow | cut -d ":" -f2
# get name
$ sudo cat /etc/shadow | cut -d ":" -f1
```
### How to create a symbolic link in Linux
2021-04-07 16:18:32 +02:00
To create a symbolic link to target file from link name,
you can use the ln command with -s option like this:
2021-04-08 10:30:33 +02:00
```bash
$ ln -s target_file link_name
```
### alias:
```bash
$ alias ll="ls -l"
```
2021-04-07 16:18:32 +02:00
The -s option is important here. It determines that the link is soft link. If you don’ t use it, it will create a hard link. I’ ll explain the difference between soft links and hard links in a different article.
2021-04-08 10:30:33 +02:00
### Htop: Interactive processes viewer
2021-04-06 15:49:44 +02:00
> $ htop
2021-04-06 11:56:51 +02:00
2021-04-07 16:18:32 +02:00
- Changer default shell
> $ vim /etc/passwd
```bash
steve:x:1002:1002:,,,:/home/steve:/bin/bash
steve:x:1002:1002:,,,:/home/steve:/bin/fish
```
- |: pipe send result of the first command to the second
> cat /etc/passwd **|** **grep** bash |**cut** -d ":" -f1
- Grep: print lines that match patterns
> $
Search for specific text with grep command
> $ grep -l example document1.txt document2.txt
> $ grep -l example \*.txt
grep as long as you include the -r (recursive) option in the command.
> $ grep -lr example /path/to/directory1/\*.txt /path/to/directory2
Or, to search the current directory and all subdirectories, omit the path at the end of the command.
> $ grep -lr example
- Cut: remove sections from each line of files
> $
- Wc: print newline, word, and byte counts for each file
> $ wc -l
```bash
$ realpath example.txt
/home/username/example.txt
```
2021-04-08 09:41:49 +02:00
## how to navigate in a web page source code
- Wget: The non-interactive network downloader
2021-04-08 10:30:33 +02:00
```bash
2021-04-08 09:41:49 +02:00
$ wget www.tandemlaw.be
```
- search url inside index.html
[Bashoneliners.com ](bashoneliners.com )
```bash
$ cat index.html | grep -o "https.*" |cut -d "\"" -f1 |sort | uniq
```
2021-04-07 12:56:36 +02:00
## text editor
- Nano: Nano's ANOther editor, an enhanced free Pico clone
(simple text editor for noobies)
2021-04-08 10:30:33 +02:00
```bash
$ nano
$ nano filename
```
2021-04-07 12:56:36 +02:00
- VIM: vim - Vi IMproved, a programmer's text editor (PGM)
> $ vim
2021-04-08 09:41:49 +02:00
> $ vim filename
2021-04-07 12:56:36 +02:00
2021-04-06 15:49:44 +02:00
# APT
2021-04-08 10:30:33 +02:00
```bash
$ apt install
$ apt remove
$ apt autoremove
$ apt update
```
2021-04-06 15:49:44 +02:00
## Display & Destop Manager
* Architecture:
2021-04-08 10:30:33 +02:00
```bash
BIOS -> GRUB -> Display Manager -> Desktop Environement
```
2021-04-06 15:49:44 +02:00
* Install Desktop Environement (GUI)
2021-04-08 10:30:33 +02:00
```bash
$ tasksel
$ apt install gnome
$ apt remove gnome
```
2021-04-06 15:49:44 +02:00
* Reconfigurer le display manager
2021-04-08 10:30:33 +02:00
```bash
$ sudo dpkg-reconfigure gdm3
```
2021-04-06 15:49:44 +02:00
* Installer le display manager
2021-04-08 10:30:33 +02:00
```bash
$ sudo apt install lightdm
2021-04-06 15:49:44 +02:00
2021-04-08 10:30:33 +02:00
$ sudo apt install gdm3
```
2021-04-06 15:49:44 +02:00
* remove Desktop environement
2021-04-08 10:30:33 +02:00
```bash
$ sudo apt remove lightdm
```
2021-04-06 15:49:44 +02:00
# Services
- HTOP
- dhclient > get ip
- gnome networkmanager
- wpa_supplicant > encryption @ wifi
2021-04-08 09:41:49 +02:00
![htop ](./img/htop.png )