编程知识 cdmana.com

Linux system own service (Part 2)

System own service ( Next ).md

Own services , Services that do not require users to install software independently , It's a service that can be used directly after the system is installed ( built-in )

Learning goals

1、 Master two ways to set the host name

2、 understand FQDN effect

3、 understand chkconfig The role of

4、 Be able to use chkconfig The command implements the addition and deletion of services

5、 understand ntp service

6、 master ntpdate The principle and implementation of time synchronization

7、 Understand the role of firewalls

8、 Familiar with firewall operation ( start-up 、 stop it )

9、 Learn about firewall rules

10、 understand rpm How to install the package

11、 Master how to mount the CD and how to hang it

12、 master rpm Unloading of packages 、 Installation and update operations

13、 Understand the role of planning tasks

14、 Master the editing of planned tasks

15、 Learn about the extended capabilities of scheduling tasks ( jurisdiction 、 File save path 、 Log view )

One 、 Host name settings

1、 review hostname command

grammar :# hostname View the full host name

grammar :# hostname  -f FQDN( Fully qualified domain name 、 domain name 、 Host name )

ntp.itcast.cn

web01.itcast.cn

web02.itcast.cn

wps1

2、 Temporarily set the host name

effect ( immediate ), But you need to switch users to make it work .

grammar :# hostname Set the host name of

for example : Set the host name of the current host to yunwei.itcast.cn

wps2

3、 Set the host name permanently

First step : Find the file first /etc/sysconfig/network file 【 The configuration file for the host name 】

wps3

The second step : Modify the HOSTNAME Set the permanent host name for yourself

wps4

The third step : Modify the server's hosts file , take yunwei Point to the local ( Set up FQDN) domain name IP DNS Application

File path :/etc/hosts

wps5

After changing the above documents , Please restart the computer by yourself .

4、 Expand :FQDN

problem : Not set up FQDN What will happen? ?

answer :

① A lot of open source server software ( for example Apache) Unable to start or error reported

② Convenient memory , Seeing the host name has a preliminary judgment on its function

③ If it is not set, it will affect the resolution of the local domain name ( Local access )

Two 、chkconfig

1、chkconfig summary

effect : amount to Windows Next " Safety guard "、" The computer butler " Security aids like this provide " Startup item " A management service for .

2、 Why chkconfig

stay Linux Not all of the following software installation has boot service , Some may need to be added by themselves . In addition, you can view and delete .

notes :【 stay CentOS6 Is used in chkconfig, stay CentOS7 Is used in systemctl】

3、 Boot up service query

command :# chkconfig  --list

wps6

wps7

among 0-6 Represents each startup level
for example : With httpd For example
Its 3 Level is off (off), said : stay 3 In the startup mode, it will not start by default , stay 5 It's also closed , It means that it will not start when it is turned on in the desktop environment .
Another example is :haldaemon service , stay 3,4,5 Under the level of default boot , Other levels of the default boot does not start


4、 Add boot service

grammar :# chkconfig --add service name

notes : The added boot service must be in /etc/init.d, Can be used chkconfig --add service name

Sample code : take apache Of httpd Services added to chkconfig Go to... In the list , Give Way httpd By chkconfig management

① proofreading /etc/init.d Directory services apache The service name :httpd

wps8

② hold httpd Service added to boot service

wps9

③ Set the service to boot at a certain level / Do not start 【 Key command 】

grammar :# chkconfig  --level  Linked startup levels   service name   on open /off Turn off

Sample code : Set up httpd The service is 3,5 Under the default boot level

wps10

If you just want to see httpd Startup information of the service , You can also use the pipeline command + grep Realization :

wps11

Sample code : Set up httpd The service is 5 Under the level of the default boot does not start (off)

wps12

5、 Remove boot service

# chkconfig   --del   service name

for example : Delete httpd service 【 Delete service and boot not start is 2 A concept , Deleting a service only means that the startup entry of the corresponding service is no longer made by chkconfig Conduct management 】

Sample code : Delete during startup httpd service

wps13

6、 Expand :CentOS7 Boot up in

stay centos7 The boot entry is no longer used chkconfig 了 , Although still compatible with , But use systemctl command .

systemctl  enable   service name

systemctl  disable  service name

Check boot entry , have access to systemctl list-unit-files

7、 practice

① Set up sshd service , stay 12345 Start up at level 2 , Other levels don't start ;

② Set up kdump service , stay 1235 Start up at level 2 , Other levels don't start ;

Expand :grep When filtering, query multiple keywords at the same time ,-E:extension( Expand ),“|” Express or , That is, only one line matches one of the keywords .

3、 ... and 、ntp service

1、ntp summary

effect :ntp It is mainly used for the time synchronization management operation of the computer .

Time is important to the server , Generally, many websites need to read the server time to record relevant information , If time is not right , It could have a big impact .

for example : In the current virtual machine linux Time is just not accurate .

wps14

2、 Synchronous server principle

There are ways to synchronize server time 2 individual : One time synchronization ( Manual sync )、 Automatic synchronization through services .

The concept of upstream :

wps15

① # ntpdate  The domain name of the time server or IP Address

IP Address view can access :http://www.ntp.org.cn/pool.php

wps16

Use ntpdate Synchronize server time :

wps17

② Set up the time synchronization service ntpd

service name :ntpd

start-up ntpd service :service ntpd start perhaps /etc/init.d/ntpd start

wps18

Set up ntpd Service startup :

wps19

Four 、 A firewall ( a key )

1、 What is a firewall

A firewall : Guard against some networks ***. There's a software firewall 、 Hardware firewall .

wps20

The firewall chooses to let normal requests go through , So as to ensure the network security .

Windows A firewall :

wps21

2、Linux A firewall

In the current CentOS6.9 There is a name for the firewall in :iptables 【7.x The default is firewalled】

① see iptables Start up or not

wps22

Be careful :iptables After startup, it can't pass through ps View the progress of , Its processing is the firewall access rules handled by the kernel , So there's nothing in the process .

The solution to permanently shut down the firewall :chkconfig --level 2345 iptables off

② iptables Service startup / restart / close

# service   iptables    start/restart/stop

/etc/init.d/iptables   start/restart/stop

wps23

③ see iptables The state of ( The rules )

#  service  iptables  status

wps24

④ Command to view rules

# iptables  -L  -n

Option description :

-L:(list) It means to list rules

-n: To change the expression of a word into a number

wps25

And in iptables In the startup state, the contents are almost the same .

In particular :-n Options and -L Options can actually be merged together .iptables -nL

Option description :

--line-number: Display the line number of each rule

wps26

3、 Firewall rule settings

for example : allow 80 port ( The default port number of the website ) Through firewall , The rule can be set with the following command

First step : install httpd Software (Apache Software ) And start the Apache Software

[root@localhost ~] # rpm  -ivh   httpd-2.2.15...
[root@localhost ~] # service  httpd  start

The second step : Set firewall rules

#iptables -I INPUT -p tcp --dport 80 -j ACCEPT # allow access to 80 port

wps27

iptables: Master order
-I: Put the rule first ( Capitalization i),INSERT abbreviation , For insertion
-A: To add a rule to a rule ( Last ),APPEND abbreviation , For append insert
INPUT: Stop in request 【 Departure OUTPUT】
-p:protocol, Designated agreement (icmp/tcp/udp)
--dport: Specify port number
-j: Specify the behavior result , allow (ACCEPT)/ prohibit (REJECT)/ discarded (DROP)





After adding, you need to save the operation :/etc/init.d/iptables save

wps28

wps29

The third step : Modify the firewall rules (-R)

wps30

Step four : Delete firewall rules (-D)

wps31

Step five : Save and restart the firewall

[root@localhost ~] # /etc/init.d/iptables save
[root@localhost ~] # service iptables restart

4、 Firewall test case

① start-up apache service ( service name :httpd)

wps32

② open 172.16.46.129:80(172.16.46.129 by Linux Server's IP Address )

wps33

③ prohibit 80 Port through firewall

wps34

The effect is shown below :

wps35

5、 Expand : Security group

Expand : Cloud security group issues

Security group is also a kind of firewall , This security group is not built on our server , It's built on gateway devices . Operating security groups in the cloud console is mainly through web Site page to carry out the operation of .

Access path based on cloud server :

Inbound direction :

Extranet → Security group → The firewall of the server (ipatables etc. ) → Project code

Not in the form of security groups :

Extranet → The firewall of the server (iptables etc. ) → Project code

6、 ... and 、rpm Package management ( a key )

1、rpm Package Overview

effect :rpm Is similar to Windows In the computer Housekeeper on " Software management "、 Inside the security guard " Software Manager " Products such as , The main function is to linux The software package on the server performs corresponding management operations , Management is divided into : Inquire about 、 uninstall 、 install / to update .

2、rpm How to install the package

To install software , and windows Next is the same. , First find the installation package :xxx.rpm

How to get the software package :

a. Go to the official website to download (http://rpm.pbone.net);

b. If you don't mind the old version , You can get it from CD-ROM ( Or mirror files ) Read from ;

3、 Load the drive

① Load disc CentOS*.DVD1.iso

wps36

② Use # lsblk(list block devices) Check the block device information

wps37

Name: name
Size: Device size
Type: type
MountPoint: Mount point ( similar Windows Lower drive )
This interface is similar to Windows Under the disk management interface :



wps38

4、 Expand : Mounting and undocking the CD ( a key )

① Uncoupling operation

command :umount

grammar :# umount The mount point of the current device ( route )

wps39

② Mount the cd-rom

command :mount

grammar :# mount The original address of the device file (/dev Catalog ) Location path to mount ( Disk character )

The original address of the device : Address unification is in /dev Next , And then, according to the size of the specific name value , Put together to form the original

Starting address , For example :"/dev/sr0"

wps40

Location path to mount : The mount directory is usually in mnt Next , It can also be in mnt Create a directory , Here to "/mnt/dvd" For example .

wps41

wps42

Use cd Command to open /mnt/dvd As shown in the figure below :

wps43

5、 Check the installation of a software

grammar :# rpm -qa | grep key word

Options :

-q: Inquire about ,query

-a: All ,all

Sample code : Inquire about firefox Browser installation

wps44

Sample code : Inquire about qq The installation of the software

wps45

6、 Uninstall a piece of software

Uninstall a piece of software

grammar :# rpm -e The name of the software ( It is suggested to write a complete name , adopt -qa Inquire about )

Case study : Uninstall Firefox

wps46

When the uninstall is complete , single click firefox Browser icon , The results are shown in the following figure :

wps47

WAMP(Windows + Apache + MySQL + PHP) You must install before you install VC++ 2014 x86_64, This is called dependency .

There is no dependency relationship when Firefox is unloaded , So you can uninstall .

But unloading Apache( service name httpd) When the prompt cannot be unloaded :

wps48

Particular attention : When there are dependencies and you don't want to solve the problem, you can # rpm  -e  Package name   --nodeps

7、 Install some software

grammar :# rpm -ivh Package full path name

Options :

-i:install, install

-v: Show progress bar

-h: Said to "#" The form shows the progress bar

Sample code : install firefox Firefox ( stay DVD Compact disc 1 in )

① Get into /mnt/dvd/Packages Catalog , Because we put the CD 1 Mounted in this directory

wps49

② Use rpm Command to install software

wps50

③ Use rpm Inquire about firefox Installation

wps51

④ After installation , open firefox browser

wps52

8、 Update some software

grammar :# rpm -Uvh Complete installation package path

Options :

-U:upgrade, upgrade

-v: Show progress bar

-h: Said to # The form shows the progress bar

9、rpm Expand

① Check the package name to which the file belongs ( practical )

grammar :# rpm -qf File path to query

Options :

-f: Query the package to which the file belongs

Sample code : Inquire about /etc/ntp.conf Which package does it belong to ?

wps53

② After the installation of the query software , What files have been generated

grammar :# rpm  -ql   The name of the package to be queried

wps54

In particular : After the software is installed , The following types of files have been generated
Profile class :/etc Catalog
The program file itself , Binary file command :/usr/sbin Catalog
Documentation manual :/usr/share/doc or man Catalog


7、 ... and 、cron/crontab Planning tasks ( a key )

1、 The role of planning tasks

effect : The operating system is impossible 24 There are people operating it every hour , Sometimes I want to perform tasks at a designated time point ( for example : Every morning 2 Click to restart Apache), It's impossible for someone at this time of the day to 2 Click to execute the order , This can be handed over to the scheduler to perform the operation .

2、 View scheduled tasks

grammar :# crontab   Options

Common options :

-l:list, Lists the scheduled tasks for the specified user

-e:edit, Edits the scheduled task list for the specified user , Simply speaking , Planning a task is a document

-u:user, Specified user name , If you don't specify , Represents the current user

-r:remove, Deletes the scheduled task list for the specified user

Sample code : List the current user's scheduled tasks

wps55

The above tips , representative root Users don't plan tasks to do .

3、 Edit scheduled task ( a key )

Enter the planning task and edit the file

wps56

Open the scheduled task and edit the file , We can write our custom scheduled tasks in this file :

Regular syntax format for scheduling tasks , Behavior unit , A line is a plan :

branch when Japan month Zhou Commands that need to be executed

for example :0 0 * * * reboot, For every day 0 when 0 Points to perform reboot Instructions .

Value range ( common sense ):
branch :0~59
when :0~23
Japan :1~31
month :1~12
Zhou :0~7,0 and 7 Sunday
Four symbols :
*: Represents each number in the range
-: I'm doing continuous interval expressions , If you want to say 1~7, So we can write this as :1-7
/: That's how many , for example : Want to every 10 Minutes at a time , I can write it in the sub position :*/10
,: Multiple values , Such as in 1 spot ,2 spot 6 Point to perform , I can write it at time :1,2,6









In particular : stay Linux In the system , Some commands cannot be used directly in a scheduled task , After using , It won't work either . Such as useradd command . If you want it to work, you have to use the full path .

problem : How to query the location of the real path of a command ?

answer : Use whereis Command name

wps57

4、 A few small cases

problem 1: monthly 1、10、22 Japanese 4:45 restart network service

problem 2: Every Saturday 、 Sunday's 1:10 restart network service

problem 3: Every day 18:00 to 23:00 Between every 30 Minutes to restart network service

problem 4: Every other morning 8 Point to 11 Point of the first 3 And the 15 Perform a reboot every minute

Time sharing day month week Command or service

problem 1:45 4 1,10,22 * * service network restart

problem 2:10 1 * * 6,7 service network restart

problem 3:*/30 18-23 * * * service network restart

problem 4:3,15 8-11 */2 * * reboot

Case study : Every time 1 Minutes to root Home directory readme.txt The lost one 1, Append output to see the effect 【 Output usage echo command , grammar :# echo Output content 】

wps58

Enter... In last line mode :wq Save and exit , Use tail -f see readme.txt Dynamic change

wps59

Be careful : In the planning task , The default minimum unit is minutes , It can't be any smaller . If you need to achieve the second level, you need to cooperate with the development of relevant code .

5、 Expand

① crontab Permission problems

crontab It's a scheduled task that any user can create , However, super administrators can configure some users to not allow to set scheduled tasks .

The blacklist is located in :/etc/cron.deny The user name is written in it , You can only write one in a line

wps60

wps61

Save and exit , Switch to cndws Ordinary users , perform crontab command , As shown in the figure below :

wps62

The whitelist also has a configuration file

/etc/cron.allow ( There is no such thing as , Create yourself )

Be careful : White list has higher priority than blacklist , If a user exists in two list files at the same time , Will be allowed to create scheduled tasks by default .

② View the path to save the scheduled task file

problem : Where is the plan task file saved ?

answer :/var/spool/cron/ In the user name file , If you use root Users edit scheduled tasks , Then the user file name is root

wps63

③ View scheduled task log information

problem : in application , How do we view scheduled tasks running ?

answer : By scheduling task logs , The log file is located in /var/log/cron

wps64

版权声明
本文为[osc_ t4kk3au7]所创,转载请带上原文链接,感谢

Tags linux service
Scroll to Top