theme | background | fonts | ||||||
---|---|---|---|---|---|---|---|---|
seriph |
|
siriuskoan
cd
- Change directoryrm
- Removemkdir
- Create directorytouch
- Create filecp
- Copycat
- Get the content of a file and more thingsmv
- Move or renamepwd
- Get current (working) directoryless
- A powerful file viewerman
- Show mannual pagewhich
- Get executable file path
ls
ln
chmod
grep
ssh
su
/sudo
scp
/rsync
ps
/top
/htop
du
/df
- Introduction to Shell Script
A package management tool.
apt update
apt search <keyword>
apt install <package name>
apt upgrade [package name]
apt remove <package name>
apt autoremove
The best editor
Two modes that are usually used:
- normal
- insert
When in normal mode, press i
to enter insert mode.
When in insert mode, press Esc
to enter normal mode.
::left::
Some commands that can be used in normal mode
dd
- cut the current lineyy
- copy the current linep
- pastex
- cut the current chargg
- go to the head of the fileG
- go the the end of the filegg=G
- reindent the whole fileu
- undoCtrl + R
- redo
::right::
When in normal mode, type
:q
to quit:q!
to quit without saving:w
to write:x
to write and quit!{shell command}
to execute shell command
::end::
Tmux is a terminal multiplexer, it enables a number of terminals to be created, accessed, and controlled from a single screen.
Linux mannual page
The tool can help you manage multiple terminal.
You can detach the session so that the session will keep running even if your SSH connection is closed.
It also has many great features such as screen spliting, screen syncing, customization and so on.
layout: iframe url: https://www.youtube.com/embed/3kxAfNDQLQw
::left::
- Session
- You get a new session when you execute tmux every time.
- Window
- You can create multiple windows in a session.
- Pane
- You can get a pane by spliting a window.
::right::
Prefix: Ctrl + b
c
- create new windowd
- detach current sessionn
- switch to next windowp
- switch to previous window{number}
- switch to speficied windoww
- show a list of the window to switch to&
- close current window"
- split current window horizontally%
- split current window vertically{arrow key}
- switch among panes
>
,>>
<
,<<
|
ls > ls.txt
cat < input.txt
ls > ls.txt 2> err.txt
ls > ls.txt 2>&1
ls | grep *.log
cat test.txt | tail -n 20 | sort | uniq | nl
cat test.txt |& my_script.sh
::left::
cat << EOF | grep ls | uniq > test 2>&1
ls
grep
ls
ps
EOF
The result is
ls
::right::
cat << EOF > out.txt
test
test2
test3
EOF
It will generate a file containing the three lines.
First thing to know: everything you see in Linux is a file, i.e.,
- Directory is file
- Link is file
- Your screen is file
- Your keyboard is file
- ...
Two types of path:
- absolute path (start with
/
):/var/log/auth.log
- relative path (start from current directory):
test/meow.txt
Some special path
.
- current directory..
- parent directory~
- home directory/
- root directory
ls -l
(or ll
)
-rw-rw---- 1 siriuskoan wheel 217 Apr 8 14:08 test
File types:
-
: Regular fileb
: Block device filec
: Character device filed
: Directoryl
: Symbolic links
: Unix domain socketp
: Named pipe
Use file
command to determine its file type.
If you are interested in how to make a character device or block device, check this.
ln
- make links between filesLinux mannual page
There are two types of links
- hard: another entrypoint of file
- symbolic (soft): just like "shortcut" in Windows
To create hard link, use ln [original_file] [hard_link]
To create soft link, use ln -s [original_file] [soft_link]
Every file has its own inode. Use ls -i
to view the inode of file.
A hard link means the same inode as original file, but a soft link has different one.
The data block and inode will be taken back by OS once all hard links are deleted.
When the system says "no more space", it may mean you don't have space for another new inode.
Recall the information shown in ll
.
-rw-rw---- 1 siriuskoan wheel 217 Apr 8 14:08 test
r
: readw
: writex
: execute
For directory, rwx
represent "can ls
in it", "can write a file in it" and "can cd
into it".
Every set of three characters shows permissions of a certain class of users, including the owner, the users in the group,and the other users, respectively.
Sometimes, the permissions not only contain r
, w
and x
but also contain s
, which means setuid or setgid (depending on where the s
is).
They allow users to execute a file as its owner or as people in its group.
-
A file with permissions
rwsr-xr-x
.A user who is classified as "others" can execute the file, and due to the
s
in owner permissions, when the user execute the file, he or she will become its owner. -
A file with permissions
rwxrwsr-x
.A user who is classified as "others" can execute the file, and due to the
s
in group permissions, when the user execute the file, he or she will become a member in its group.
A real example is /user/bin/passwd
: -rwsr-xr-x
.
- uid - who runs the script
- gid - the group the runner belongs to
- euid - who actually runs the script and acquires the resources
- egid - the group which actually runs the script and acquires the resources
In normal situation, uid == euid
, gid == egid
.
However, if we use setuid
and setgid
, euid
becomes the owner's id and egid
becomes the group's id.
We can use this simple program to check the uid, gid, euid and egid.
#include <stdio.h>
#include <unistd.h>
int main() {
uid_t real_uid = getuid();
uid_t effect_uid = geteuid();
gid_t real_gid = getgid();
gid_t effect_gid = getegid();
printf("ruid=%d, euid=%d\n", real_uid, effect_uid);
printf("rgid=%d, egid=%d\n", real_gid, effect_gid);
return 0;
}
Sometimes, there is t
in permissions. It means sticky bit.
Sticky bit means only the owner of a file (and superuser) can remove or move it.
A real example is /tmp
: drwxrwxrwt
.
There are two ways to change permission.
-
The first way is
chmod [0-4][0-7]{3} test
.In the first set of number,
4
,2
,1
represents setuid, setgid, sticky bit, respectively. This part can be omitted.In the second set of number,
4
,2
,1
representsr
,w
,x
, respectively.For example,
chmod 1777 test
.
-
The second way is
chmod [ugoa][+-=][srwx] test
u
,g
,o
,a
represents user (owner), group, others, all, respectively.+
,-
,=
represents grant, deprive, set, respectivelyIf there are multiple settings you want to change, you can use comma to separate them.
For example,
chmod uo+rx,ug+w test
To add or remove sticky bit is a little bit different, it only requires
+-
andt
. For example,chmod +t test
.
In most of the cases, we use -r
option to recursively apply something.
However, we should use -R
option to accomplish this.
chmod -R 777 d/
will change the permission of all the files and directories under d/
to 777
.
It is the same when we use chown
and chgrp
.
grep - print lines that match patterns
Linux manual page
It is a very useful tool when checking log.
patterns: regular expression
::left::
Some common options
-i
- ignore case different-r
- recursively-n
- show line number-f
- read from file-e
- regex patterns-v
- logical NOT-A
- print some lines after the matched lines-B
- print some lines before the matched lines-C
- print some lines before and after the matched lines
::right::
Some common usage
cat test.txt | grep test
grep "test" test.txt
grep -inr "test" .
grep -A 2 -B 2 "test" test.txt
grep -C 2 "test" test.txt
(the same output as the above one)
SSH, standing for Secure Shell, is a protocol that provides secure channel for transferring.
Two ways to connect
-
Use
ssh [username]@[hostname or IP address]
to connect to remote server and start your work. -
Use
ssh-keygen
to generate public key and private key, and usessh-copy-id
to copy your key to remote server.
In this way, no password is required.
Both of them make you have higher privileges, but there are still some differences between them.
su
gives you full permissions of root. When using it, you should use root password.sudo
gives you temporary higher permissions for executing certain commands. When using it, you should use your own password.
Using sudo
is much better than using su
when a team maintains a system. The reasons are
- It can give different team members different permissions, i.e., your system can have better access control.
- Every command executed with
sudo
will be recorded in/var/log/secure
, so if there is something wrong, it is easy to find out who did it.
We should not do anything like bash
with sudo
since others cannot know what you do.
That is, we should not do anything that (can) creates a shell with sudo
. For example, su
, sh
, vim
, less
and so on.
To run command as root with su
su
- switch to rootsu -
- login as root
To switch to another user, we can use su - [user]
.
To run command as others with sudo
sudo [command]
- usingsudo
to run a command as rootsudo !!
- usingsudo
to run last command as rootsudo -u [user] [command]
- usingsudo
to run command as specified usersudo -g [group] [command]
- usingsudo
to run command as specified group
We can use visudo
to edit sudo config file (/etc/sudoers
).
User Host=(RunAsUser:Group) Commands
For example,
root ALL = (ALL:ALL) ALL
%sudo ALL = (ALL:ALL) ALL
siriuskoan ALL = (root) /bin/cat,/bin/ls
siriuskoan ALL = (root) NOPASSWD:ALL
Linux Fundamentals: A to Z of a Sudoers File.
Some common errors
[username] is not in the Sudoers file. This incident will be reported.
Sorry, user [username] is not allowed to execute [command] as [user] on [hostname]
scp
- secure copy (remote file copy program)Linux manual page
scp
uses SSH protocol to copy file from one host to another.
scp
is a good friend when we need to copy file from one host to another host. However, scp
has been deprecated, we should use rsync
instead.
Usage
scp [path] [user]@[remote host]:[path]
- copy file to remote hostscp [user]@[remote host]:[path] [path]
- copy file from remote host
Examples
scp ~/test.txt siriuskoan@my-host:~
scp [email protected]:~/test.txt ~
Options
-r
- recursively-p
- preserves modification times, access times, and modes from the original file.-C
- enable compression
rsync
- a fast, versatile, remote (and local) file-copying toolLinux manual page
rsync
can do what cp
and scp
do; moreover, it is more efficient.
Advantages
- Speed
- Better security
- Delta transfer algorithm
Examples
rsync ~/test.txt siriuskoan@my-host:~
rsync [email protected]:~/test.txt ~
Options
-r
- recursively-a
- archive-z
- enable compression--delete
- delete receiving side file if it does not exist in the sending files--progress
- show progress
They are process and system monitoring tools.
ps - report a snapshot of the current processes
Linux manual page
top - display Linux processes
Linux manual page
htop - interactive process viewer
Linux manual page
ps aux
is a command we often use.
Program is dead, and when you execute it, it becomes a process.
Attributes of the processes
- PID, PPID - Process ID and Parent PID
- UID, EUID - User ID and Effective UID
- GID, EGID - Group ID and Effective GID
- Niceness - Priority
top
and htop
are both process viewers, we can use them to check out some system and processes information.
htop
is a better process viewer than top
.
Let's see how to use them.
du - estimate file space usage
Linux manual page
df - report file system disk space usage
Linux manual page
They are some tools that help us check disk usage or file / directory size.
Usage
du -ah ~
- show the size of all files under home directorydu -ah -d 1 /
- show the size of all directories under root directory (because of-d 1
)du -hs ~
- show how large the home directory isdf -Th
- show all filesystems disk usage along with their filesystem types
Shell is a bridge between user and system kernel.
There are many shells such as Bourne shell (sh), bash, zsh and so on.
By writting shell script, several things like analyzing log and user management can be done automatically.
We can program in shell language, which has variables, flow control, input, output and so on, just like all the other languages, but it has its own syntax.
Shell language is a interpreted language, it can be run directly in your shell.
Actually, shell script consists of the commands we use, and with some important components such as special variables, exit code and so on.
After finishing your script, remember to chmod
it to allow users to execute it.
Shell script file starts with "Shebang" (#!
), the sign specifies which program is used to run it.
For example, if we want bash
to run the script, the Shebang is #!/bin/bash
.
Also, we can use python
to run it, and the Shebang is #!/usr/bin/python
.
Then, you can run it by executing command ./your_script_name.sh
(be sure you have make it executable).
Let's see an example of shell script.
#!/bin/bash
my_name="$1";
if [[ ${my_name} == "siriuskoan" ]]; then
echo "You are admin!";
else
echo "You are not admin, the incident will be reported.";
echo "${my_name} attempted to run this script" > /root/my_script.log;
fi
If you want to know more about the syntax of shell script, you can check out the following two websites.