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 Basic Notes

image

Notes on the Linux basics course included in last year's Python foundational course

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

I. Operating System and Linux#

1. Operating System#

  1. Operate hardware
  2. Encapsulate methods for 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
Home contains folders for each user

etc system configuration files
bin executable binary files
home user directory

II. Linux Terminal Commands#

Master a dozen commonly used ones; for less common ones, refer to a table.

ctrl +/- to zoom in on terminal font

1. The 7 Most Common Commands:#

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

2. Terminal Command Format#

command [-option][parameter]

rm -r folder

3. Referencing 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.

Copying and moving
tree cp mv

Viewing file contents
cat more grep

1) ls#

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

. current directory
.. parent directory

ls -a to display all files
ls -l to display all file information in a list format
ls -h to display file sizes in a human-readable format 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 to go home directly
or add a tilde ~ to return to the home directory directly
cd - to switch between the last two directories

Relative path
Absolute path starts with / or ~

3) touch#

If it already exists, it updates the date.

4) mkdir#

-p to create directories in succession

mkdir -p a/b/c/d

(In Linux, a folder and a file cannot have the same name in the same directory.)

5) rm#

-f to force deletion
-r to delete recursively

Wildcards can also be used.

6) tree#

Displays the file system in a tree structure.

-d to show only directories.

7) cp#

cd source_file target_file

Text editor ctrl s for quick save.

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

-i prompts before overwriting files.
-r to copy entire folders recursively.

8) mv#

mv source_file target_file
-i prompts before overwriting.

9) cat#

concatenate
View file contents.

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

10) more#

Displays in pages.
Used to view large files.

Operation shortcuts are the same as man.

11) grep#

Powerful text search tool.

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

Pattern matching
^asdasd matches the start of the line.
asdasd$ matches the end of the line.

12) echo#

Outputs to the terminal.

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

'>' outputs to a file (overwrites).

'>>' appends.

\ is for new line.
It can be used not only 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 to display in pages.
ls -lha | grep ds to filter output.

III. Remote Management#

1. shutdown#

shutdown option time

Defaults to shutting down in one minute.
now to restart immediately.

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

-r to restart.

2. View Network Card Configuration#

Network Card
A hardware device responsible for network communication.
The IP address is the address information set on the network card.

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

ifconfig | grep inet to 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 of files.

To copy from remote to local: scp -P(uppercase!) 22 source_file user@remote
To transfer from local to remote: scp user@remote target_file
-r to transfer folders.
-P to specify the 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 and 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

The number of hard links 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 infinite).

So the number of hard links can indicate the number of subdirectories.

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

2) chmod#

Modify file or directory permissions.
changemod.

chmod +/-rwx file or directory

For directories, readable means reading the directory contents, writable means modifying the directory contents, and 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.
chagrp -R group_name file/directory to modify the group ownership of the file or directory.

3. User Management#

useradd -m -g group username
-m to automatically create a user directory.
-g to specify the user's group; otherwise, a group with the same name will be automatically created.

passwd to set the user password.
userdel -r username
-r to delete the user's 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 groups, and login shell.

Primary group is the group specified when creating the user.
Supplementary groups are specified additional groups.

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

usermod -G sudo username to add the user to sudo.

Shell is the software for entering terminal commands.

dash
bash

which
View the location of the executed command.

The /etc/passwd file stores user information.
The /usr/bin/passwd program modifies passwords.

su username to switch users.
exit to log out.

4. Modify File Permissions#

chown to change the owner.
chgrp to change the group.
chmod to 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 for a year's calendar
date current system time.

2. Disk Information#

df -h disk free to display remaining disk space.
du -h [directory_name] to view the space occupied by the directory.

3. Process Information#

Process: a program currently being executed.

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

ps by default can only see programs started from the terminal.
a for all users' processes.
u to display detailed process status.
x to display processes without a controlling terminal.

It is best not to kill root processes.

4. Other Commands#

  • find to search for files.
  • ln soft link (equivalent to shortcut).
  • tar to package and compress.
  • apt-get to install software.

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 links:
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, akin to an alias.
A soft link accesses the source file's file name through the soft link file data, indirectly accessing the data.
Hard links are rarely created in daily use.
Data of a file will only be deleted when the number of hard links is 0.

5. Packaging and Compression#

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

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

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

gzip
Compresses the packaged file.

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

bzip2 (two)

tar.bz2

Add -j at the beginning of the options.

6. apt#

Advanced Packaging Tool.

sudo apt install package_name
sudo apt remove package_name
sudo apt upgrade to update the package.

sl small train.
htop upgraded version of top.
linuxlogo.
screenfetch.

Make the terminal interesting.
In the terminal, type 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 say nonsense.

cmatrix hacker matrix.
rev reverse output generally used after a pipe.
lolcat color output.
cowsay a cow says the output result.
aptitude moo also a cow, but for fun.
fortune randomly outputs a passage -zh outputs Chinese poetry.
toilet prints large letters.
figlet 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 of prime factors.
espeak to read aloud.

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

bb a somewhat different media player.
ASCIIquarium aquarium.

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