๐ Summary
- Linux Overview
- System Information
- Files & Directory
- Compress & Extract Files
- Process Management
- File Permission
- Network
๐ฅ Linux Overview
username@system_name:~$
The tilde (~) symbol stands for your home directory
Directory | Function |
---|---|
/ | begins the file system, called root |
/home | contains users home directory |
/bin | all the standard commands and utility programs i.e. executable binaries such as cat, cp, ls, mv, ps, rm |
/usr | holds those files and commands used by the system |
/var | files that are expected to change in size and content (var stands for variable), such as mailbox files |
/dev | file interfaces for devices such as the terminals and printers |
/etc | is the home for system configuration files and any other system files |
/boot | contains the few essential files needed to boot the system |
/lib | contains libraries (common code shared by applications and needed for them to run) |
/mnt | it has been used since the early days of UNIX for temporarily mounting filesystems |
/opt | optional application software packages |
/tmp | temporary files; on some distributions erased across a reboot and/or may actually be a ramdisk in memory |
/sys | virtual pseudo-filesystem giving information about the system and the hardware. Can be used to alter system parameters and for debugging purposes |
creating user account
$ sudo useradd -m -c "Rubens Barbosa" -s /bin/bash rubnsbarbosa
$ sudo passwd rubnsbarbosa
look if it was well created
$ grep rubnsbarbosa /etc/passwd /etc/group
connect to the new user
$ shh rubnsbarbosa@localhost
remove the new user
$ sudo userdel -r rubnsbarbosa
to check all users
$ ls -l /home
Keyboard Shortcuts
keyboard shortcut | task |
---|---|
tab | auto-completes files, directories, and binaries |
ctrl + l | clear the screen |
ctrl + a | goes to the beginning of the line |
ctrl + e | goes to the end of the line |
ctrl + d | exits the current shell |
ctrl + z | puts the current process into suspended background |
ctrl + c | kill the current process |
ctrl + h | works the same as backspace |
ctrl + w | deletes the word before the cursor |
ctrl + u | deletes from beginning of line to cursor position |
๐ง System Information
the man pages, which are manuals for linux commands available from the Command Line Interface CLI
$ man ls
$ man mkdir
$ man rm
$ man grep
$ man patch
$ man diff
display one line manual page description of a command
$ whatis top
$ whatis mv
$ whatis nice
display full path of commands where given commands reside
$ which top
$ which grep
$ which nice
locate the binary, source, and manual page files for a command
$ whereis top
$ whereis grep
$ whereis nice
tell us your username
$ whoami
show the current date and time
$ date
show this month's calendar
$ cal
show current uptime
$ uptime
tell us detailed information about the machine name, operating system and kernel
$ uname -a
display CPU information
$ cat /proc/cpuinfo
display memory information
$ cat /proc/meminfo
show the disk usage
$ df
show directory usage space
$ du
display memory and swap usage
$ free
display memory and swap usage in human format
$ free -h
โ๏ธ Files & Directory
clear terminal
$ clear
$ ctrl + l
list of files and/or directories contents
$ ls
list content of current directory (display attributes such as owner, group owner, permissions)
$ ls -l
list hidden files and/or directories (hidden file begins with . dot sign)
$ ls -a
list everything of current directory (attributes such as owner, group owner, permissions) + hidden files
$ ls -la
$ ls -al
to know in which directory you're located (pwd stands for "print working directory")
$ pwd
to make directories e.g. mkdir foo will create a new directory or folder called "foo"
$ mkdir foo
to create any number of directories/folders simultaneously
$ mkdir foo bar foobar
to remove or delete an empty directory/folder
$ rmdir foo
to remove or delete a file in your directory entries
$ rm
to remove or delete a directory and all of its contents recursively [f = force]
$ rm -r foo
$ rm -rf foo
to change directories (moving through the file system)
$ cd
to navigate into the root directory
$ cd /
to navigate to your home directory, use "cd"
$ cd
$ cd ~
$ cd $HOME
$ cd /home_path/
to navigate up one directory level
$ cd ..
to navigate into the documents directory
$ cd Documents/
to navigate into the documents directory and see which files and/or directories exists there
$ cd Documents/{press tab twice}
to navigate into the directory which contains space in their names
$ cd 'best songs ever'
$ cd best\ songs\ ever
to create a new empty file
$ touch file_name.txt
to create multiples empty files
$ touch foo.txt bar.txt foobar.txt
to execute several commands on the same line by separating them with a semicolon ;
$ ls ; date
to see the contents of a file (-n parameter shows the number of lines in file)
$ cat main.py
$ cat passwd.txt
$ cat -n song.txt
displays only the first 10 lines of the file
$ head foo.txt
prints the first -n 'num' lines instead of first 10 lines
$ head -n 5 foo.txt
display only the last 10 lines of the file
$ tail foo.txt
prints the last -n 'num' lines instead of last 10 lines
$ tail -n 3 foo.txt
to move a file to a different location
$ mv [source-file] [destination-file]
$ mv foo.txt /home/ubuntu/script/
to move a file to home directory (the terminal use the ~ shortcut to your home directory)
$ mv foo.txt ~
$ mv foo.txt /home/ubuntu/
to copy a file from the current directory to a different one, the command below will make an exact copy of "foo.txt" file
$ cp [source-file] [destination-file]
$ cp foo.txt /home/ubuntu/script/
to copy a directory, use "cp -r directory name" (-r recursively = to copy the directory and all its files and subdirectories and all their files and so on)
$ cp -r bar /home/ubuntu/script/
$ cp -r 'best songs ever' /home/ubuntu/music/
to copy or move all your C or Python source code files to a given directory
$ cp *.c algorithms
$ mv *.py algorithms
to find a file in a current working directory
$ find . -name hello_world.py
$ find . -name hello_world.py -print
to find all files with the .py extension in the script directory
$ find script -name '*.py' -ls
to locate other directories e.g. the command below will locate the script directory
$ find /home/ubuntu -name script -type d -print
to find text in a file i.e. the command will search through the file to find a piece of text which you are looking for
$ grep 'Hello' hello_world.py
to print the history of a long list of executed commands in the terminal
$ history
reboots the system
$ reboot
shuts down the system
$ shutdown
shuts down the system by powering off
$ poweroff
brings the system down immediately
$ halt
reboots the system by shutting it down completely and then restarting it
$ init 6
powers off the system using predefined scripts to synchronize and clean up the system prior to shutting down
$ init 0
๐ฆ Compress & Extract Files
Tar utility creates archives for files and directories, and was originally designed to create archieves on tapes (the term "tar" stands for tape archive). The tar utility is ideal for making backups of your files, which can then be transferred over the Internet
Archives using tar
Syntax:
tar [options] [archive-name.tar] [directory-or-file-name]
Options | Function |
---|---|
-c | creates a new archive |
-x | extract the archive |
-f | specify an archive filename |
-v | verbosely display the .tar progress in the terminal |
-t | lists files in archived file |
-r | appends files to an archive |
-u | updates an archive with new files |
-w | waits for a confirmation from the user before archiving each file |
-z | creates archived file using gzip |
-j | creates archived file using bzip |
create a tar archive using option -cvf
$ tar -cvf foo-archive.tar foo.txt
extract file from archive using option -xvf
$ tar -xvf foo-archive.tar
create a gzip tar archive using option -cvzf
$ tar -cvzf foo-archive.tar.gz foo.txt
extract a gzip tar archive using option -xvzf
$ tar -xvzf foo-archive.tar.gz
create a gzip tar archive with python files
$ tar -cvzf python-codes.tar.gz *.py
create a gzip tar archive file for a directory
$ tar -cvzf images-august-2021.tar.gz /home/ubuntu/images/
create tar with bzip2 compression
$ tar -cvjf foo-archive.tar.bz2 foo.txt
extract a tar using bzip2
$ tar -xvjf foo-archive.tar.bz2
compress to file.gz
$ gzip foo-archive
decompress file.gz
$ gzip -d foo-archive.gz
๐จโ๐ป Process Management
A process, in simple terms, is an instance of a running program. Whenever we execute a command in Linux it starts, or creates a new process. The Linux kernel tracks processes through an ID number known as PID which means Process ID. The kernel is a part & core of the Operating System OS, it's closer to the hardware i.e. is the lowest level of the OS. The Operating System is the software package which contains applications like the user interface (shell, gui, tools, etc) and communicates directly to the hardware and our application. The kernel is the main part of the Operating System and is responsible for translating the command into something that can be understood by the computer. Basically the Kernel is the layer between hardware (devices which are available in computer) and software (applications like gedit). Only Kernel provides low level services such as:
- memory management
- network management
- device driver
- file management
- process management
Types of Processes
When we create a new process (run a command), there are two types:
- Foreground Processes: They run on the screen and need input from the user. For example Office Programs
- Background Processes: They run in the background and usually do not need user input. For example Antivirus.
to display the currently working processes
$ ps
all the currently running processes
$ ps -A
processes associated with the current terminal session
$ ps -T
to check all the processes associated with a particular User
$ ps -u rubnsbarbosa
to check all the processes running under a user
$ ps ux
to check all the processes associated with a particular user group
$ ps -fG root
to display the processes running with full information
$ ps -f
to display the processes running on the system in the form of a tree
$ pstree
description of all the fields displayed by ps -f command
Column | Description |
---|---|
UID | user id that this process belongs to |
PID | process id |
PPID | parent process id (the id of the process that started it |
C | CPU utilization process |
STIME | process start time |
TIY | terminal type associated with the process |
TIME | CPU time taken by the process |
CMD | The command that started this process |
display all running Linux processes
$ top
interactive process viewer
$ htop
display list of kill
$ kill -l
kill the process with given pid (for example: I want to kill this 217956 PID)
$ kill 217956
if a process ignore a regular kill command, we can use kill -9 followed by the PID
$ kill -9 217956
to find the PID of a process
$ pidof Photoshop.exe
kill all the process named proc
$ killal proc
will kill all processes matching the pattern
pkill pattern
to check the nice value of a process (example: I'd like to find terminal value name)
$ ps -el | grep terminal
run a program with modified scheduling priority i.e. set processes CPU priority. Kernel will allocate more CPU time to that process
$ nice -10 gnome-terminal
changing priority of running processes with PID 77982
$ renice -n 15 -p 77982
change the priority of all programs of a specific group
$ renice -n 10 -g 4
suspend process running in foreground
$ ctrl+z
list jobs table
$ jobs
send stopped process to background
$ bg [job-num]
brings process to foreground
$ fg [job-num]
๐ File Permission
Every file or directory within Linux has a set of permissions that control who may read, write and execute the contents. In Linux Linux, a directory is just a special type of file. File ownership is an important component of Unix which provides a secure method for storing files. Every file in Linux has the following category:
- Owner โ the name of the user that owns the file/directory;
- Group โ the name of the group that has permissions on the file/directory;
- Other โ the name of all other users can perform on the file/directory.
Permissions
Every file and directory in your Linux has following 3 permissions defined for all the 3 category discussed above.
Permission | Abbreviation | File | Directory | Octal Value |
---|---|---|---|---|
read | r | able to view the contents of a file | able to list the files within the directory | 4 |
write | w | able to modify the contents of a file | able to add/delete files to/from directory | 2 |
execute | x | able to run the file as an executable | able to cd into the directory and access files | 1 |
Octal Notation Table
Octal | Decimal | Permission | Representation |
---|---|---|---|
000 | (0+0+0) = 0 | no permission | --- |
001 | (0+0+1) = 1 | execute | --x |
010 | (0+2+0) = 2 | write | -w- |
011 | (0+2+1) = 3 | write + execute | -wx |
100 | (4+0+0) = 4 | read | r-- |
101 | (4+0+1) = 5 | read + execute | r-x |
110 | (4+2+0) = 6 | read + write | rw- |
111 | (4+2+1) = 7 | read + write + execute | rwx |
Whenever using ls -l command, it display informations related to file permission as follows
The first character is called the file type. An ordinary file is represented by a dash (-) and a directory is represented by a d.
Note: A dash (-) anywhere else in the permission set indicates no permission.The 1st set of three characters are the users permissions in green.
The 2nd set of characters are the group permissions in cyan.
The 3rd set of characters are the permissions for all other users in red.
The File permissions that are set depend on the type of file e.g. a text file has different permissions to a shell script because a text file doesnโt need the executable permission but a shell script does.
examples of different types of permissions on files and directories:
-rwx------ this file is read/write/execute for the owner only
dr-xr-x--- this directory is read/execute for the owner and the group
-rwxr-xr-x this file is read/write/execute for the owner, and read/execute for the group and others
Setting Permission
In order to change file permissions we use chmod command (change mode - changes permissions of a given file) followed by the octal values that reflect the permissions we want to set. To decide on the permissions:
- work out what you want each category of user to be able to do and the appropriate octal value for this (see Octal Notation Table);
- take these 3 octal values and put them together to form a set which will be the permissions for that file.
The example below shows that if we want a user to be able to read and write to a file but the group and other to only be able to read that file then the permissions for this file would need to be set to 644
category | u | g | o |
---|---|---|---|
permission | r w | r | r |
value | 4 + 2 | 4 | 4 |
total | 6 | 4 | 4 |
more examples:
-
chmod 755 foo.txt
(results in rwxr-xr-x) -
chmod 666 foo.txt
(results in rw-rw-rw-) -
chmod 664 foo.txt
(results in rw-rw-r--) -
chmod 700 foo.txt
(results in rwx------) -
chmod 711 foo.txt
(results in rwx--x--x) -
chmod 754 foo.txt
(results in rwxr-xr--) -
chmod 755 foo.txt
(results in rwxr-xr-x) -
chmod 000 foo.txt
(results in ---------) -
chmod 777 foo.txt
(results in rwxrwxrwx) -
chmod g+r foo.txt
(adds read to group) -
chmod g-r foo.txt
(removes read to group) -
chmod o+r foo.txt
(adds read to others) -
chmod a-w foo.txt
(removes write from all users)
Using chmod in symbolic mode
Chmod operator | Description |
---|---|
+ | adds a permission to a file/directory |
- | removes the permission from a file/directory |
= | sets the designated permission(s) |
a shell script or any other which needs to be executable should have a permission 711
$ chmod 711 foobar.sh
owner - read, write and execute
group - execute
other - execute
a text file doesn't need to be executable, it should have a permission 644
$ chmod 644 foo.txt
owner - read and write
group - read
other - read
โก๏ธ Network
Letโs start with the most basic question: is our physical interface up? The ip link show command tells us
# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc pfifo_fast state DOWN mode DEFAULT group default qlen 1000
link/ether 52:54:00:82:d6:6e brd ff:ff:ff:ff:ff:ff
your interface might be disable, so before check cables you should bring the interface up
# ip link set eth0 up
this prints output in a much more readable table format
# ip -br link show
lo UNKNOWN 00:00:00:00:00:00 <LOOPBACK,UP,LOWER_UP>
eth0 UP 52:54:00:82:d6:6e <BROADCAST,MULTICAST,UP,LOWER_UP>
we can use the -s flag with the ip command to print additional statistics about an interface
# ip -s link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 52:54:00:82:d6:6e brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
34107919 5808 0 6 0 0
TX: bytes packets errors dropped carrier collsns
434573 4487 0 0 0 0
We can check the entries in our ARP table with the ip neighbor command:
# ip neighbor show
192.168.122.1 dev eth0 lladdr 52:54:00:11:23:84 REACHABLE
Note that the gatewayโs MAC address is populated (weโll talk more about how to find your gateway in the next section). If there was a problem with ARP, then we would see a resolution failure:
# ip neighbor show
192.168.122.1 dev eth0 FAILED
Linux caches the ARP entry for a period of time, so you may not be able to send traffic to your default gateway until the ARP entry for your gateway times out. For highly important systems, this result is undesirable. Luckily, you can manually delete an ARP entry, which will force a new ARP discovery process:
# ip neighbor show
192.168.122.170 dev eth0 lladdr 52:54:00:04:2c:5d REACHABLE
192.168.122.1 dev eth0 lladdr 52:54:00:11:23:84 REACHABLE
# ip neighbor delete 192.168.122.170 dev eth0
# ip neighbor show
192.168.122.1 dev eth0 lladdr 52:54:00:11:23:84 REACHABLE
# ip -br address show
lo UNKNOWN 127.0.0.1/8 ::1/128
eth0 UP 192.168.122.135/24 fe80::184e:a34d:1d37:441a/64 fe80::c52f:d96e:a4a2:743/64
# ping www.google.com
PING www.google.com (172.217.165.4) 56(84) bytes of data.
64 bytes from yyz12s06-in-f4.1e100.net (172.217.165.4): icmp_seq=1 ttl=54 time=12.5 ms
64 bytes from yyz12s06-in-f4.1e100.net (172.217.165.4): icmp_seq=2 ttl=54 time=12.6 ms
64 bytes from yyz12s06-in-f4.1e100.net (172.217.165.4): icmp_seq=3 ttl=54 time=12.5 ms
^C
--- www.google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 12.527/12.567/12.615/0.036 ms
We can print the routing table using the ip route show command:
# ip route show
default via 192.168.122.1 dev eth0 proto dhcp metric 100
192.168.122.0/24 dev eth0 proto kernel scope link src 192.168.122.135 metric 100
SSH
$ ssh admin@192.168.1.113
$ ssh ubuntu@192.168.1.113
ssh key
$ ssh-key
$ ssh-keygen -t rsa
ssh authentication - you store your public key on your server and you have your private key with you, and the private key is the most important is the key that will allow you to authenticate successfully with the server. So you need to keep it secure, if you lose it you will lose access to the server.
copy file from remote server
$ scp foo.txt admin@192.168.1.113:/home/admin/
display the current network interface configuration information
# ifconfig
display IP addresses and property information
# id addr
List all of the route entries in the kernel
# ip route
display neighbour objects; also known as the ARP table for IPv4
# ip neigh
lists all my connections and their DNS servers
# systemd-resolve --status
allows you to test the IP-level connectivity of a given host on the network
# 192.168.2.32
display the route that a packet takes to reach the host; also prints detail about all the hops that it visits
# traceroute google.com
# traceroute 172.217.26.206
Top comments (0)