KANIKIG

KANIKIG

just for fun | 兴趣使然 Ph.D. in Engineering|❤️ #NFT $ETH| [Twitter](https://twitter.com/kanikig2)|[Github](https://github.com/KANIKIG)|[Telegram channel](https://t.me/kanikigtech)

Linux Basics Notes

image

Notes on the basic Linux course included in last year's Python fundamentals class

Remember it was very brief, basically only understandable to myself.

I. Operating Systems and Linux#

1. Operating System#

  1. Operate hardware
  2. Encapsulate methods of operating hardware as system calls

1.1 Classification of Operating Systems#

  • Desktop Operating Systems
  • Server Operating Systems
  • Embedded Operating Systems
  • Mobile Device Operating Systems

2. History of Operating Systems#

2.1 Unix#

Star Trek
BCPL->B Language->C Language
Unix
Multi-user

2.2 Linux#

Based on Minix
Kernel

2.3 Linux Kernel and Distributions#

Added terminal commands
The kernel is encapsulated system calls
Distributions are a set of applications developed based on the kernel

2.4 Linux File System#

Windows drive letters
Because before XP, it was all single-user

Linux
User folders are placed under home

etc system configuration files
bin executable binary files
home user directory

II. Linux Terminal Commands#

Master a dozen commonly used ones, look up the less common ones

ctrl +/- to zoom in on terminal font

1. The 7 Most Common Commands:#

  1. ls list view the contents of the current folder
  2. pwd print working directory view the current folder
  3. cd change directory switch folders
  4. touch touch if the file does not exist, create a new file
  5. mkdir make directory create a directory
  6. rm remove delete a file
  7. clear clear screen

2. Terminal Command Format#

command [-option][parameter]

rm -r folder

3. Consult Command Help Information#

Two ways

command --help

man command
Space to page down
Enter for the next line
b to page back
f to page forward
q to quit

(command is the corresponding command)

4. Common File and Directory Commands#

ls cd touch rm mkdir

Tab for auto-completion of file names; if there are other choices, press again, and the system will prompt possible commands

You can switch up and down to previous commands
ctrl c to go directly to the bottom

Copy and move
tree cp mv

View file contents
cat more grep

1) ls#

Files starting with . are hidden; use ls -a (all)
To create a hidden file, just add a .

. current directory
.. parent directory

ls -a display all files
ls -l display all file information in list format
ls -h human-readable file size with -l

drwxr-xr-x 4 kanikig kanikig 4096 May 19 18:02 AppWebCache
drwxr-xr-x 3 kanikig kanikig 4096 Jun 16 22:55 Audio
drwxr-xr-x 2 kanikig kanikig 4096 May 18 18:13 CloudRes
-rw-r--r-- 1 kanikig kanikig 33280 May 26 15:05 CustomFace.db
-rw-r--r-- 1 kanikig kanikig 494509 Jul 9 14:01 CustomFace.dbc
-rw-r--r-- 1 kanikig kanikig 132096 Jul 9 14:01 DingDongStorage.db
-rw-r--r-- 1 kanikig kanikig 0 May 18 18:08 DingDongStorage.db-journal
-rw-r--r-- 1 kanikig kanikig 25600 Jul 9 14:01 DocImport.db

d represents a directory

ls -l -h
File sizes are more intuitive
drwxr-xr-x 4 kanikig kanikig 4.0K May 19 18:02 AppWebCache
drwxr-xr-x 3 kanikig kanikig 4.0K Jun 16 22:55 Audio
drwxr-xr-x 2 kanikig kanikig 4.0K May 18 18:13 CloudRes
-rw-r--r-- 1 kanikig kanikig 33K May 26 15:05 CustomFace.db
-rw-r--r-- 1 kanikig kanikig 483K Jul 9 14:01 CustomFace.dbc

Options can be combined, such as
ls -alh

ls and wildcards

  • any string
    ? any character
    [] match any character in the group [1-9] [abc]

For example, ls m* finds those starting with m

2) cd#

Common parameters

cd directly home
or add a tilde ~ to return directly to the home directory
cd - to switch to the last two directories

Relative path
Absolute path starts with / or ~

3) touch#

If it already exists, update the date

4) mkdir#

-p to create directories in succession

mkdir -p a/b/c/d

(in Linux, a folder cannot have the same name as a file in the same directory)

5) rm#

-f force delete
-r recursive delete

Wildcards can also be used

6) tree#

Displays the file system in a tree structure

-d show only directories

7) cp#

cp source_file target_file

Text editor ctrl s for quick save

You can specify the file name afterward
If not changed, just write the directory

-i prompt before overwriting files
-r recursively copy entire folders

8) mv#

mv source_file target_file
-i prompt before overwriting

9) cat#

concatenate
View file contents

-b number non-empty output lines
-n number all output lines

10) more#

Page display
Used to view large files

Operation shortcuts are the same as man

11) grep#

Powerful text search tool

-n show line numbers
-v lines that do not contain the text (negation)
-i ignore case

Pattern matching
^asdasd matches the beginning of a line
asdasd$ matches the end of a line

12) echo#

Output in the terminal

Redirection: output content that should be displayed in the terminal to a file

'>' output to file (overwrite)

'>>' append

\ is for new line
It can be used with echo
ls can also be redirected

ls -lh >>a

13) |#

Pipe
The output of one command is used as the input for another command

ls -lha | more for paginated display
ls -lha | grep ds to filter output

III. Remote Management#

1. shutdown#

shutdown options time

The default is to shut down in one minute
now to restart immediately

shutdown 20:25
shutdown +10 shut down in ten minutes

-r restart

2. View Network Card Configuration#

Network card
Hardware device responsible for network communication
IP address is the address information set on the network card

ifconfig view/configure network card information
ping test if the connection is normal

ifconfig | grep inet quickly view IP
One physical network card and multiple virtual network cards
Physical network card ens..

ping
ctrl c to stop

3. Remote Login and Copying#

1) ssh#

Secure Shell

Domain name IP address alias
Port number can find the application running on the computer
The default ssh port is 22 and can be omitted
ftp 21
web server 80

ssh [-p port] user@remote
remote is the remote IP
port defaults to 22

exit to log out

2) scp#

Remote copy files

From remote to local scp -P(uppercase!) 22 source_file user@remote
From local to remote scp user@remote target_file
-r to transfer folders
-P specify port

3) Advanced ssh#

Passwordless login
Configure aliases

ssh configuration information is all under .ssh

Passwordless login:
Execute ssh-keygen in the local .ssh directory, press enter through all prompts to generate two files (public key, private key)
Then ssh-copy-id [-p] user@remote will transfer an authorization file to the server's .ssh directory

Asymmetric encryption algorithm
Data encrypted with the public key needs to be decrypted with the private key
Data encrypted with the private key can be decrypted with the public key

Encrypt data with the private key locally, and the server's public key decrypts the data

Configure aliases:
Create a config file under .ssh
Write in it

Host h1
HostName 202.182.115.91
User root
Port 22

IV. Users and Permissions#

Concepts of users and permissions
User management
Group management
Modify permissions

1. Users and Permissions#

Read r 4
Write w 2
Execute x 1

User groups

1)#

​ Permissions User Group
drwxr-xr-x 3 kanikig kanikig 4.0K May 18 16:45 Music
drwxr-xr-x 2 kanikig kanikig 4.0K May 18 22:17 Pictures
drwxr-xr-x 3 kanikig kanikig 4.0K Jul 8 22:58 PycharmProjects
-rw-r--r-- 1 kanikig kanikig 485 Jul 11 15:39 shadowsocks.json
-rw-r--r-- 1 kanikig kanikig 485 Jul 11 15:25 shadowsocs.json
-rw-r--r-- 1 kanikig kanikig 0 May 18 16:44 stale_outputs_checked
drwxr-xr-x 2 kanikig kanikig 4.0K May 18 14:25 Videos

Hard link count indicates how many ways a file or directory can be accessed
Files generally have 1, which is the absolute path
Folders have 2 or more because you can cd. or cd.. from a subdirectory (this can be numerous)

So the hard link count can indicate the number of subdirectories

The first column indicates file or folder
Then every three characters form a group
The first group is the owner of the file or directory
The second group is the permissions of the user group
The third group is other users

2) chmod#

Modify file or directory permissions
changemod

chmod +/-rwx file or directory

For directories, readable means reading the contents of the directory, writable means modifying the contents of the directory, executable means executing terminal commands in the directory

3) root#

sudo
substitute user do
5-minute validity period

2. Group Management#

groupadd group_name
groupdel group_name
cat /etc/group to confirm group information
chgrp -R group_name file/directory to change the group ownership of the file directory

3. User Management#

useradd -m -g group username
-m automatically create user directory
-g specify the user's group, otherwise automatically create a group with the same name

passwd set user password
userdel -r username
-r delete user home directory
cat /etc/passwd | grep username to confirm user information
passwd file
1 username
2 password x
3 uid
4 gid
5 full name
6 home directory
7 shell used for login

View user information
id username to view user id and group id
who to view the list of currently logged-in users
whoami to view the currently logged-in username

usermod
Set user primary group, supplementary group, and login shell

Primary group is the group specified when creating the user
Supplementary group is the specified supplementary group

usermod -g group username to change the user's primary group
usermod -G group username to change the supplementary group
usermod -s /bin/bash to change the shell

usermod -G sudo username to add the user to sudo

Shell is the software that inputs terminal commands

dash
bash

which
View the location of the executed command

/etc/passwd stores user information
/usr/bin/passwd is the program to change passwords

su username to switch users
exit to log out

4. Modify File Permissions#

chown change owner
chgrp change group
chmod change permissions

chown username file or directory
chgrp -R group_name file or directory
chmod -R 755 file or directory

7 owner permissions
5 group member permissions
5 other user permissions

r=4
w=2
x=1

So it can be inferred

V. System Information#

1. Date and Time#

cal this month's calendar -y a year's calendar
date current system time

2. Disk Information#

df -h disk free displays remaining disk space
du -h [directory name] view the space occupied by the directory

3. Process Information#

Process: a program currently being executed

ps aux process status view detailed process information
top dynamically displays processes and sorts q to quit
kill [-9] process ID -9 force stop

ps by default can only see terminal-started programs
a all users' processes
u displays detailed process status
x displays processes without a controlling terminal

It is best not to kill root processes

4. Other Commands#

  • find find files
  • ln soft link (equivalent to shortcut)
  • tar package and compress
  • apt-get software installation

find [path] -name “*.py”
ln -s source_file link_file
The source file must use an absolute path
If -s is not used, a hard link will be created

For soft links, if the source file is moved or deleted, it will become invalid; hard links will not

Hard link:
In Linux, file names and file data are stored separately
Access data through the file name
A hard link is equivalent to the file name, directly accessing the data, equivalent to an alias
A soft link accesses the source file's file name through the soft link file data, indirectly accessing the data
In daily use, hard links are rarely created
File data will only be deleted when the hard link count is 0

5. Package Compression#

Commonly used in Linux is tar.gz
tar only packages, does not compress

tar -cvf package_file.tar files_to_package/path package
tar -xvf package_file.tar unpack

-v lists the detailed process of packaging
-f specifies the file name (f must be placed last)

gzip
Compress the packaged file

tar -zcvf package_file.tar.gz compressed_file
tar -zxvf package_file.tar.gz decompress
tar -zxvf package_file.tar.gz -C target_path decompress to specified path

bzip2(two)

tar.bz2

Add -j at the beginning of the options

6. apt#

advanced packaging tool

sudo apt install package
sudo apt remove package
sudo apt upgrade update package

sl small train
htop upgraded version of top
linuxlogo
screenfetch

Make the terminal interesting
In the terminal, enter sudo visudo
At the end of "Defaults" (the first half of the file), add a line "Defaults insults".
Save it
After that, when you use sudo, if you enter the password incorrectly, the terminal will start to talk nonsense

cmatrix The Matrix
rev reverse output generally used after a pipe
lolcat colored output
cowsay a cow says the output result
aptitude moo also a cow, but for fun
fortune randomly outputs a sentence -zh outputs Chinese poetry
toilet prints out large letters
figlet the same as above
aafire this will display a burning flame on the screen sudo apt install libaa-bin
oneko a little cat chases the mouse
factor factorization
espeak read aloud

fork bomb.. exponential self-multiplication until it occupies all system resources

bb a somewhat different player
ASCIIquarium aquarium

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.