Dark horse programmer linux study notes introductory part collection

ubuntu installation

This course uses the ubuntu system.

ubuntu official website - download.

It will show that there are two versions. Ubuntu releases two versions every year. LTS is a long-term maintenance version, so it will be relatively stable.

introduce

Linux distribution

Regardless of the version, the kernel is the same.

  • RPM based,如 red hat,centos7.
  • dpkg based,如 ubuntu,debian。
  • gentoo, compiled from source code.

The linux from scratch website can help us write our own operating system in source code.

live CD

Operating system on disc. For example, the computer operating system has crashed, and we want to copy the data and reinstall it, but the operating system has crashed, so how can we use it? At this time, you can use this kind of CD, insert it and copy it.

study method

Ditch the GUI, use the CLI.

Query resources:

  • Linux comes with: /usr/share/doc directory.
  • TLDP
  • CLDP, the project has been terminated.

Get ready

software preparation

apt-get

apt-get update //先查看一下服务器上软件的最新版本

image-20230312174202607

Unlike windows, the user created at the beginning is not the admin, and the account created by linux only gives you the necessary permissions. More permissions need to apply.

sudo apt-get update # sudo: 执行此语句的时候,临时把用户设置为 root

sudo apt-get upgrade # 根据之前获取的最新版本,更新软件

A solution to the error: [Solved] Could not get lock /var/lib/dpkg/lock-frontend_could not get lock /var/lib/dpkg/lock-frontend. it_Bungehurst's blog - CSDN blog .

But the premise is definitely the same: your current user can have Sudo privileges. (super user do)

mirror source

We can see from the previous apt-get that it is the latest software obtained through http://cn.archive.ubuntu.com/ubuntu website. Maybe this website is not suitable for your system, we can replace the source.

nano /etc/apt/sources.list, nano is the editor, and the sources are in the sources.list file.

image-20230314152538082

jammy is a code name for different ubuntu versions.

You can search for some mirror websites on Baidu to replace them. Generally, such websites support most versions.

This file is read-only and requires sudo privileges. Write out after modification.

Be sure to re-update after the change!

Software installation package removal

Stored under /var/cache/apt.

There are many deb files visible under the archives folder. du -sh .You can check the size.

sudo apt-get cleanClear all downloaded packages.

installation file

sudo apt-get install 软件名

View the name of the package to be installed: apt-cache search 软件名.

The search results can be limited by regular expressions, for search "^mysql"example, only the packages with mysql appearing at the beginning of the package name or package description line.

Compared with apt-get, dpkg only downloads the deb package, does not install software, and does not download associated dependent software.

dpkg -lSee which packages are currently installed.

dpkg -L 软件包名View installation information for a particular package.

uninstall file

sudo apt-get remove [--purge] 软件名, purge is to delete its configuration files.

Of course, you can manually delete all the content in dpkg -L rm, but this will not delete the database information in apt-get, which means that the software is still recorded in apt-get but has actually been deleted. not on. So it is not recommended to use it like this.

WSL

Compared with VMWare creating an entire virtual machine, windows supports WSL software to simulate our win system into a linux system.

Open the linux item in the windows function, install ubuntu in the application store, and open ubuntu after restarting, which is a new page.

We can also find ubuntu in windows terminal. In the settings-startup configuration file, it can be set to open ubuntu by default when it is opened.

image-20230315214845754

Compared with VMWare, there are still advantages, for example, snapshots can be taken for emergency recovery.

Log in

Unix is ​​actually a multi-user operating system, and each user uses ID PWD to log in.

Log in

First confirm that your machine is connected to a Unix (ssh), or your machine itself is Unix, and log in through the console. Servers that allow others to log in remotely need to be implemented openssh server. In the past, telnet and telnetd were used, but this method of transmitting data is complement code, which is not safe.

In order to ensure security, after the account password is wrong, it will not be notified whether the account number or the password is wrong, and there is a certain delay in checking the account password.

The dark horse course uses finalshell to connect to the linux system in VMware, and it will be much more convenient to use across vmware.

Check the ens33 inet item in the ifconfig of the linux system, and use this ip address to realize the remote connection. You need to install ssh related content in linux first.

image-20230315195521818

quit

logout or exit or ^d.

shutdown

Shutdown and restart requires permission.

shutdown -h nowor haltor init 0.

reboot

rebootor init 6.

change Password

passwd

Shell

An interface to the linux operating system, which is convenient for us to run programs. The operating system kernel is the center of a sphere, and the Shell is the outer layer of spherical shell (nb). We send commands through the shell.

The shell is configured under /etc/passwd, the last field of each record. Use more to view the file to see its information:

用户名:密码(x 代表有密码):userId:groupId:desc:家目录(该用户初次登录时处于哪个目录):该用户使用的 shell

Different shells support different commands, whether they support command line editing (such as up and down keys, shortcut keys), whether they support history records ( , , history) !编号, !!configuration file settings, environment variables, command line prompt (prompt string), programming methods, etc. All are different. Now commonly used is bash.

There are also text-based graphical shells, and graphical shells.

getting Started

structure

Tree structure, without many drive letters, all files are in the root directory /.

The windows hierarchy is represented by \, and the linux hierarchy is still the / symbol.

command base

Command general syntax: command [-option] [parameter], options indicates parameter details, and parameter indicates specific execution target.

For example ls -l /home, it is to list all files under home in the form of a list.

ls: List all files in the current folder in flat form.

  • options -a: List all files including hidden files.
  • options -l: Each file is displayed in one line and one list, and detailed information (such as creation date) of each file will also be displayed.
  • options -a -l or -al or -la: apply both -a -l.
  • options -h: must be used together with -l. The file size abbreviation (KMG) of each file, given the list of all files.
  • parameter: The directory path to list, the default is the current working directory.

When the system is turned on, the default user directory is the current working directory. /home/usrname in linux, c:\user\usrname in windows

pwd : View the current directory.

cd : Enter a specific directory.

  • parameter specific directory address: enter the path. The beginning of / is an absolute path, and nothing is a relative path.
  • parameter .: Indicates the current path. eg cd ./home.
  • parameter ... : Upward path.
  • parameter ~: Enter the home path of the current user.

mkdir : Create a directory. A parameter must be added to indicate the path.

  • options -p: Whether to create a new parent path if the parent path does not exist. -p means new.
  • parameter specific directory address: the directory to be created and its address.

Creating folders requires permissions. Therefore, it must be created inside the home, and it cannot be successfully created outside. The authority control will be described in detail later.

touch : Create a file. Parameters are required, no options.

ls -l starts with a - sign is a file, without a - sign is a folder.

cat : View files. The required parameter is the path and filename of the file to view; no options.

more : Same as cat, but query in paging mode. Space to turn the page, more to exit.

cp : copy files, parameters 1 and 2 are required.

  • options [-r]: If the copied folder is used, it means recursive.
  • parameters Path 1: The file or folder to be copied.
  • parameters Path 2: The destination folder to copy to.

mv : Move files.

  • parameters Path1: The file or folder to move.

  • parameters Path 2: The destination folder to move to. If only the path is written, it means to move to this path; if adding a file name, it means to move to this path and rename it.

rm : Delete a file.

  • options -r: for folder deletion.
  • options -f: Forcibly delete, no prompt message will pop up. The root user will pop up.
  • parameters file1 file2 filen: the file to delete.

* is a wildcard, like *testfor anything that ends with test.

I learned a little trick here. My root password is not the default 12334556, but I successfully logged in through sudo suthe method.

which : Find instructions.

The instructions learned above are all stored in linux in the form of binary files, similar to .exe files. We can look up these instructions with which.

  • parameters directive name. likewhich ls

find : Find files.

find dirName -name fileName

*Fuzzy queries can be based on wildcards .

find dirName -size +|-n[K|M|G]

Find files larger|less than n K|M|GB.

grep : Find lines in a file that match a string or regex condition.

grep [-n] 关键字 文件路径

-n indicates whether to display line numbers.

Because keywords may have special characters, it is recommended to wrap them in double quotes.

wc : Statistical information such as the number of lines in the file, the number of words, etc.

wc [-c -m -l -w] fileName

-c: Count the number of bytes.

-m: Count the number of characters.

-l: Count the number of lines.

-w: Count the number of words.

Pipe character |: The result on the left side of the pipe character is used as the input on the right side. Either the options input or the parameters input will work.

cat abc.txt | grep abc1 .

The file path of grep can be input by pipe character. For example, the above example is: Find the content abc1 in abc.txt.

Or, for example ls -l | grep abc, view only the details of files with abc in the name.

ls -l | wc -lCount the number of files.

echo : output. For example echo 'hello world', then hello world will be output.

If backticks ` are added to the output, the content will be executed in the form of commands instead of ordinary characters. Such as echo `pwd`.

Redirector >: Overwrite the content on the left and write it to the right. For example echo 'hello world' > abc.txt, the content of abc.txt is overwritten with this line: hello world.

>> : Append writes at the end.

tail : View the content at the end of the file.

  • options [-f]: indicates continuous tracking and will be updated in real time.
  • options [-num]: Indicates the number of lines at the end of the view, the default is 10 lines
  • parameters file path.

vi editor

The most classic text editor in linux.

vim is compatible with vi, and can also edit shell programs, and can distinguish grammatical correctness by color.

There are three modes:

Command mode command mode : the keys you press are all commands. Cannot edit text.

Input mode insert mode : The mode in which the text can be edited.

Bottom line command mode last line mode : Start with:, usually used to save and exit files.

image-20230317230337618

vi 文件路径If the .file already exists, it will be opened; if it does not exist, it will be created. The bottom line shows whether it is a new file or an open file.

Press i to enter input mode. Press: Enter the bottom thread command mode. In both modes, press esc to return to the command mode.

image-20230318154828047

Command mode has some shortcut commands, such as yy to copy the current line, p to paste, dd to delete, u to undo.

image-20230318155043706

Underline mode w saves writing, q quits current file. q! Force quit.

set nu: display line number.

set paste: Set the paste mode. When enabled, the content pasted in input mode will be pasted intact.

user

root

The most privileged user in Linux. The account we registered is not a root user and has insufficient permissions, such as not being able to create new files in the root directory.

Each user only has the maximum authority under his own home, and other places only have read-only, execute authority, and no modification authority.

You can log in through su [-] root(it seems that the default password is 1234556, but mine is not, I don’t know if it’s a problem with the new version or I didn’t pay attention when setting up the virtual machine) or , sudo su [-] [root]and switch to the root user by default, and you can change the user name. We already know that we can temporarily gain permission through the sudo command.

exitOr ctrl+d to return to the previous user. Ordinary users need passwords to switch users, but root switches do not.

sudo

Root is not safe, or sudo is safer to execute a single command. Of course, users need to have sudo permission to use it.

sudoers file editor

Every once in a while using the sudo command requires re-entering the password. Can it be configured so that no manual input is required?

There is a sudoers file, which is stored under etc/sudoers. Can be sudo visudoedited .

After opening, it is still edited through the nano editor. But the difference is that the modified content will not be directly modified in the original file, but will be saved in the tmp file first.

Then the system will detect the syntax of the sudoers file, and if there is a syntax error, it will ask you whether to save or re-modify. Be sure not to save the wrong edits! ! ! Because if the sudoers file is wrong, the sudo command cannot be used in the future, and if you want to modify the sudoers file, you need to use the sudo command, and you can't change it.

Add a sentence:

%adm ALL=(ALL)NOPASSWD:ALL, where adm is the username. It means that all users in the adm group do not need a password when using sudo, and this is true for any command.

Users and Usergroups

Users and user groups can be configured in linux, and each user can be in multiple groups.

Permissions can be set for a user or for a user group.

user

useradd [-g -d] userName

-g: Specify the group you are in. If not specified, a group with the same name will be automatically created and joined. If a group with the same name already exists, -g must be added.

-d: Specify the user's home path, if not specified, it will automatically be under /home/userName.

userdel [-r] userName

-r: Delete the home directory.

id [userName]View users, and view yourself by default.

user group

groupadd groupName

groupdel groupName

usermod -aG userName groupName

Use getent passwdor view the contents of /etc/passwd, line by line:

用户名:密码(x 代表有密码):userId:groupId:desc:家目录(该用户初次登录时处于哪个目录):该用户使用的 shell

getent groupView group information.

permissions

ls -lThe file list can also view permissions.

image-20230319003910043

The first column: the permission control information of the file.

The third column: belongs to the user.

The fourth column: belongs to the user group.

There are 10 bits of file permission control information, and their respective meanings are:

image-20230319004223122

Let’s expand after the soft link~

r: read. w: Write (add, delete, rename folders, create files in folders). x: Execute (cd this folder).

chmod change permissions

chmod [-R] 权限 文件或文件夹

-R means to apply this permission to all subcontents in the folder.

Permissions look like u=rwx, g=rx, o=x.

When a file permission is rwxrwxrwx, this file will be displayed in red ls -lin .

Quickly modify permissions method:

image-20230319140827152

For example, we want to set a file to rwx rx x, that is, binary 7 5 1, chmod 715 test.txt.

chown Change permission control

Change the owner of a file or folder directly.

chown [-R] [user]:[userGroup] 文件或文件夹, -R also means iteration.

chown usr test.txt

chown :usrGroup test.txt

However, ordinary users do not have permission to execute this command, only the root user can execute it.

Practical operation

hot key

If you enter a wrong command, or the program wants to stop early, you can press ctrl+c to stop it forcibly.

ctrl+d can log out of the current account, or exit the exclusive page of the current program.

historyView the commands entered in history.

! can automatically match the content of the last input, matching the prefix. If entered !p, the last entered command beginning with p is pythonwill be executed python.

But this bottom-up matching is not so convenient. If you want to find a historical record farther away, you can use ctrl+r, similar to the ctrl+f we usually use.

Cursor movement: ctrl+a is home, ctrl+e is end, ctrl+← is the cursor jumps a word to the left, ctrl+→ is a word to the right.

ctrl+l or clear to clear the screen.

Software Installation

The installation package, such as the .exe file or .msi file commonly downloaded by windows, click to install. Mac commonly used dmg pkg files.

Linux is rpm, deb files, centos is installed through yum, and ubuntu is installed through apt.

yum

yum [-y] [install | remove |search] fileName

-y: confirm without order, install directly.

yum requires networking and needs root or sudo privileges.

apt

apt [-y] [install | remove |search] fileName

systemctl

Many software in linux use systemctl to start, stop, and start automatically.

systemctl start | stop| status | enable | disable 服务名

enable disable is used to adjust whether to boot automatically.

Services such as:

  • NetworkManager main network service

  • network Secondary network service

  • firewalld firewall

  • sshd, ssh service (finalshell is used to remotely control linux service)

status has active and inactive.

Many systems or third-party software are automatically inherited from systemctl. But there are many not, and I will talk about how to deal with this kind of software later.

soft link

Link files and folders to other locations, similar to windows shortcuts.

ln -s 被链接的文件或文件夹 要链接到的目的地

date and time zone

date [-d] [+格式化字符串]

image-20230319160307898

likedate "+%H-%M-%S"

-d can perform date calculations, such as:

date -d "-1hour" "+%H-%M-%S", based on the current time -1.

image-20230319160715459

modify time zone

Delete the current time zone file first.

rm -f /etc/localtime

Then pick Shanghai from the time zone file and create a soft link.

ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

ntp automatically calibrates system time

First the ntp program needs to be installed.

Then systemctl start ntpd system enable ntpdntpd will automatically connect to the Internet to help us calibrate the time.

It seems that the ubuntu system needs to enter ntp to start the ntp service? Or is this the setting of the new version of ntp? Anyway the code I execute issystemctl start ntp

Manual update time: ntpdate -u ntp.aliyun.com(ntpdate seems to be updated too)

network transmission

IP

The IP address is unique to each networked computer to identify itself and communicate with other computers.

There are two versions, V4 and V6, and V6 is not commonly used.

An IP address is a combination of four numbers from 0 to 255. It can be viewed through ifconfig (you need to install net-tools if you can’t view it), the main network card: the inet option in ens33 is the IPV4 address. The lo network card is the local loopback network card, and the virbr0 is the dedicated network card for the virtual machine.

127.0.0.1: refers to the local machine.

0.0.0.0: Refers to the local machine; or determines the binding relationship in port binding; or indicates that all IP addresses are released in some IP restrictions.

the host

In addition to the IP address, each computer has its own name called a hostname. linux input hostnameview .

image-20230320103048195

You can modify the current host name hostnamectl set-hostname newNamethrough , and you need to log in to finalshell again to view it.

DNS

We know that generally we visit websites through domain names that are easier to remember (such as www.baidu.com).

Domain names and IPs are mapped through the DNS system. First check whether the local DNS library has a record of the domain name to be accessed, and if not, go to the DNS public server to find the address.

The windows local domain name storage file is in: C:\Windows\System32\drivers\etc\hosts, and the linux is in/etc/hosts

When we set up the mapping between linux IP address and domain name locally in windows, we can input the host name instead of the IP address when connecting through finalshell.

Configure a fixed IP address

The IP address of linux is dynamically obtained through DHCP, so the domain name mapping we configured cannot be applied as it is updated, and needs to be changed frequently.

VMWare WorkStation - Edit - Virtual Network Adapter - VMnet8, the subnet is set to 192.168.88.0, the subnet mask is 255.255.255.0, and the NAT setting is 192.168.88.2

image-20230320105512706

The ubuntu system is different here, it is configured /etc/network/interfacesin .

And if we are using the desktop version of ubuntu, it is not configured here. Refer to this article: Ubuntu Server20.04 static ip configuration (netplan)_Brady-wdh's Blog-CSDN Blog

ethernets: ens33: # 配置的网卡名称,可以使用ifconfig -a查看本机的网卡 dhcp4: no # 关闭动态IP设置,因为要设置固定IP addresses: [192.168.88.130/24] # 要设置为的固定IP,后面的24为子网掩码的位数 gateway4: 192.168.88.2 # 要设置的网关地址 nameservers: addresses: [192.168.88.2,8.8.8.8] # 要设置的DNS地址 ———————————————— 版权声明:本文为CSDN博主「Brady-wdh」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:https://blog.csdn.net/qq_45949008/article/details/118862854

In the end systemctl restart network, netplan is: netplan generategenerate the required configuration for the renderer, netplan applyapply the configuration for the renderer.

After that, ifconfig has been fixed at 192.168.88.130 and will not change.

ping

Test whether the server is reachable by pinging.

ping [-c num] IPAddress, -c is to limit the number of pings, if not limited, it will check infinitely.

Here we can first add vim /etc/resolv.confin nameserver 8.8.8.8, which is the IP address of a free DNS server provided by Google, which should be able to resolve the mapping of domain names such as baidu.

If unable to ping: display unreachable.

wget

Non-interactive download tool.

wget [-b] url, -b is to download in the background, and save the download information to the wget-log file in the current working directory. You can keep track with tail -f.

curl

Send http requests to get information or download files.

curl [-O] url

-O: Download the file.

For example curl cip.cc, the website will tell us the public IP address of the machine.

port

Physical port: Visible, such as a USB port.

Virtual port: The internal port of the computer, invisible, and used by the operating system to interact with the outside world.

image-20230320181540486

The IP address is the address of a community, the program is the resident, and the process is the house number used to find each user.

image-20230320183846899

Linux uses the nmap command to view the exposed ports of the specified host.

nmap 被查看的 IP

22: ssh port. finalshell remote connection will be used.

netstat: View port usage.

netstat -anp | grep portNo

process management

In order to facilitate management, the operating system assigns process IDs to running programs.

ps [-e -f]View process information.

-e: View all processes.

-f: View all information.

  • PPID: Parent process ID.

  • C: CPU usage.

  • STIME: start time.

  • TTY: The serial number of the terminal that started this process, ? Indicates that it was not started by the terminal.

  • TIME: Accumulated CPU usage time.

  • CMD: Launch command or path.

shutdown process: kill [-9] PID, -9 force shutdown. If it is not forced to close, it just sends a close request, not necessarily closed. For example, tail will be terminated automatically after receiving the kill command, but if it is forced to close, it will be killed.

Host status monitoring

View system resource usage

top command, refresh every 5s, similar to task manager.

image-20230323184326044

image-20230323232340079

image-20230323233928213

image-20230323234504022

View disk usage

df [-h], -h is displayed in KMG units.

View disk speed

iostat [-x] [num1] [num2], -x displays more information, num1 refresh interval, num2 refresh times.

Check network status

sar, the command is more complicated. Therefore, to deal with network statistics, we only remember a fixed syntax:

sar -n DEV num1 num2, -n means to view the network, and DEV only views the network interface. num1 refresh interval, num2 refresh times.

environment variable

We know that instructions are executable programs one by one, which can be found.

But why can the instruction be executed without jumping to a specific directory? Like windows, thanks to environment variables.

Environment variable is a key-value pair structure, which records a series of key information needed for system operation. You can use envthe command to view.

Among them, PATH stores a series of instruction search paths.

Add the $ symbol at the beginning of the environment variable to get the value of the environment variable. Can be used in conjunction with echo. Such as: echo ${PATH}ABC.

In this way, we can add our own program to the environment variable, or put the program under the PATH path.

Setting environment variables takes effect temporarily:export virableName virableValue

Permanently effective for the current user: in the ~/bashrc file.

Permanently valid for all users: in /etc/profile.

You can pass source 配置文件or restart finalshell to take effect immediately.

Configure PATH: echo PATH, export PATH=$PATH:路径, or add the following sentence to the user or system environment variable.

Write $PATH: here first, in order to splice with the previous PATH value, this step must not be ignored.

finalshell transfer files

Right-click the file to download the file from the virtual machine to the local. Which account is used to log in, what you see is the content that can be seen by that account.

Upload: Drag and drop local files directly into finalshell.

Or download the lrzsz command. Download: sz 文件; Upload: rz, a file box will automatically pop up randomly for selection.

However, the drag and drop efficiency of uploading large files is > rz.

compression decompression

image-20230324012526297

tar: Simple assembly of files, there is no big difference in size before and after compression.

tar.gz: The compression algorithm is used, which is greatly reduced.

tar [-c -v -x -z -C -f]Can be decompressed or compressed.

-c: compression.

-v: Show progress.

-v: decompress.

-z: Added is gzip mode, not added is tarball mode.

-C: decompress destination.

-f: refers to the file to be created/decompressed.

Directives can be combined, but -z must be at the beginning and -f must be at the end. liketar -zcvf 1.tar.gz 1.txt 2.txt 3.txt

-C should be written separately.tar -zxvf test.tar.gz -C /home/jingqing

zip is compressed, and -r can be added to represent recursive processing of folders.zip -r test.zip /test 1.txt

unzip decompresses the zip file, -d specifies the address similar to -C.unzip test.zip -d /home/jingqing

Guess you like

Origin blog.csdn.net/jtwqwq/article/details/129822450