In the previous series of articles, we introduced the vi editor and 46 basic commands. This article will demonstrate the use of RPM and YUM to install and manage software under centos7.
1. RPM package manager
1.1 RPM Background Introduction
RPM (RedHat Package Manager), similar to the control panel under windows, and RPM package is similar to the setup.exe installation file. RPM will establish a unified database file to record software information in detail and automatically analyze dependencies. It was originally a program used by the Red Hat Linux distribution to manage various Linux packages. Because it follows the GPL rules and is powerful and convenient, it is gradually accepted by other distributions. The use of the version is not limited to Red Hat systems , such as CentOS, fedora, and suse.
The software packages of different Linux distributions have different formats. For example, ubuntu and debian use packages in the suffix deb format; while redhat, centos, fedora, and suse use rpm format packages.
The two are not applicable. For example, the SecureCRT remote tool mentioned above, the official provides software packages for different linux distributions. The package for ubuntu18 is: scrt-8.5.4-1942.ubuntu18-64.x86 64.deb, and the package for redhat (centos) is: scrt-8.5.4-1942.rhel7-64.x86 64.rpm.
Check the official website: https://www.vandyke.com/cgi-bin/releases.php?product=securecrt&dl=1
Before the release of RPM, the only way to install software on a Linux system was to use a source package. In the early days, it was very difficult to install programs in linux. Operation and maintenance personnel needed to compile the code and solve many dependencies by themselves. When upgrading and uninstalling service programs, they also had to consider the dependencies between other programs and libraries, which was very difficult.
So in order to solve these problems, the RPM mechanism was born to make Linux easy to install and upgrade.
1.2 rpm package format
Let's take the above SecureCRT package as an example.
scrt-8.5.4-1942.rhel7-64.x86_64.rpm
-
scrt: software name
-
8.5.4-1942: Software version number, followed by major version and minor version number
-
rhel7: Operating system, here Red Hat Enterprise 7. Some packages do not have this part
-
x86_64: The hardware platform on which the software runs
-
rpm: package extension
1.3 Common commands
1.3.1 Querying installed packages
Syntax: rpm -q packagename
Case:
[root@heimatengyun ~]# rpm -q firefox
firefox-24.5.0-1.el7.centos.x86_64
Since the installation of gui in our first article has already installed firefox by default, it is found here as installed.
1.3.2 Install the software
Syntax: rpm -ivh full package name
Options:
-i: install install
-v: verbose show detailed information
-h: hash progress bar
--nodeps: do not detect dependencies
illustrate:
The system image contains many rpm packages, and you can directly mount the CD for installation when needed. If it is not included in the CD, you can go to the Internet to download the corresponding rpm package, switch to the directory where the rpm package is located, and execute the installation command.
1.3.3 Uninstall the software
Syntax: rpm -e package
ps: It is generally not recommended to uninstall software through this method, which can easily lead to many problems.
2. YUM warehouse
2.1 Introduction to YUM
Although RPM can help query software dependencies, it is still necessary for operation and maintenance personnel to install and solve the dependency problem by themselves. There are many large-scale software dependencies, and it is very troublesome to install. To further reduce the difficulty and complexity of software installation, the YUM software repository is designed.
YUM (Yellow dog Updater, Modified) is a shell front-end package manager in RedHat, Centos and Fedora. It can be considered that the YUM repository is used to manage RPM packages. The packages in the repository can be officially released by Red Hat or written by themselves. The YUM software repository can sub-license the required software packages and related dependencies according to user needs, and automatically download the software packages from the server and install them into the system.
Therefore , in daily work, the software is usually installed by yum.
2.2 YUM related commands
Order | Function description |
---|---|
yum install package name | Install the service, you can add the -y option to avoid asking each time |
yum list | List all available packages and package groups |
yum clean all | Clear all cached data |
yum makecache | Generate cache |
yum deplist package name | List all dependencies of a package |
yum remove package name | delete service |
yum repolist | List available yum sources |
Case:
Install apache service through yum
[root@heimatengyun ~]# yum install httpd -y
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.cn99.com
* extras: mirrors.aliyun.com
* updates: mirrors.cn99.com
Resolving Dependencies
--> Running transaction check
...省略中间部分内容
Installed:
httpd.x86_64 0:2.4.6-90.el7.centos
Dependency Installed:
apr.x86_64 0:1.4.8-5.el7
apr-util.x86_64 0:1.5.2-6.el7
httpd-tools.x86_64 0:2.4.6-90.el7.centos
mailcap.noarch 0:2.1.41-2.el7
Complete!
[root@heimatengyun ~]# systemctl start httpd
It prompts that the installation is successful, and it is not started by default. Start the httpd service manually. At this time, log in to the system directly through the virtual machine, access 127.0.0.1 through the browser, and find that the appache service has been successfully installed.
However, it cannot be accessed through the virtual machine ip on the physical host. A big reason may be because of the firewall. In the early learning period, the firewall can be directly closed to avoid learning interference.
If you are unable to access, please check in the following order:
Check whether the process is started, view the port where the process starts, view the firewall status, and close the firewall if it is enabled. After the operation is completed, it can theoretically be accessed on the host machine.
[root@heimatengyun ~]# ps -ef |grep httpd
root 44948 1 0 15:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 44949 44948 0 15:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 44950 44948 0 15:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 44951 44948 0 15:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 44952 44948 0 15:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
apache 44953 44948 0 15:06 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND
root 44963 42126 0 15:07 pts/0 00:00:00 grep --color=auto httpd
[root@heimatengyun ~]# netstat -lntup |grep httpd
tcp6 0 0 :::80 :::* LISTEN 44948/httpd
[root@heimatengyun ~]# firewall-cmd --state
running
[root@heimatengyun ~]# systemctl stop firewalld.service
[root@heimatengyun ~]# firewall-cmd --state
not running
At this point, enter the IP address of the virtual machine in the browser of the physical machine, and find that the apache service can be accessed normally.
You can also check if the httpd service is running with the following command
[root@heimatengyun ~]# systemctl status httpd.service
httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled)
Active: active (running) since Wed 2019-11-27 15:06:56 CST; 30min ago
...
Note: When using yum source installation, the version of httpd is generally not the latest, and the latest version can only be downloaded from the Apache official website.
- uninstall apache service
[root@heimatengyun ~]# yum remove httpd Loaded plugins: fastestmirror, langpacks Resolving Dependencies --> Running transaction check ---> Package httpd.x86_64 0:2.4.6-90.el7.centos will be erased --> Finished Dependency Resolution ... Is this ok [y/N]: y ... Removed: httpd.x86_64 0:2.4.6-90.el7.centos Complete!
Check the status again at this time, and it has been prompted that the service cannot be found.
[root@heimatengyun ~]# systemctl status httpd.service
httpd.service
Loaded: not-found (Reason: No such file or directory)
Active: inactive (dead)
...
2.3.1 Why do you need to replace YUM
The default yum source of CentOS is not necessarily a domestic mirror, which makes the online installation and update speed of yum not very ideal. At this time, you need to set the yum source as a domestic mirror site. The main open source mirror sites in China include NetEase and Alibaba Cloud.
2.3.2 Replace the default YUM with Alibaba Cloud Source
Prerequisite: centos must be able to connect to the external network.
The main idea is to download the YUM configuration file of Alibaba Cloud from the Alibaba Cloud mirror site, and then overwrite the default YUM configuration file of the system. The configuration file can be downloaded and uploaded locally, or downloaded directly on centos. Remember to back up a default YUM when overwriting, just in case.
Execute the following commands one by one
[root@heimatengyun yum.repos.d]# mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
[root@heimatengyun yum.repos.d]# wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
--2019-11-27 16:13:24-- http://mirrors.aliyun.com/repo/Centos-7.repo
Resolving mirrors.aliyun.com (mirrors.aliyun.com)... 118.112.15.1, 182.140.218.238, 182.140.218.243, ...
...省略部分内容
[root@heimatengyun yum.repos.d]# ls
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Vault.repo
CentOS-Base.repo.backup CentOS-Sources.repo
[root@heimatengyun yum.repos.d]# yum clean all
Loaded plugins: fastestmirror, langpacks
Cleaning repos: base extras updates
Cleaning up everything
Cleaning up list of fastest mirrors
[root@heimatengyun yum.repos.d]# yum makecache
Loaded plugins: fastestmirror, langpacks
...省略部分内容
Determining fastest mirrors
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Metadata Cache Created
After executing the above command, the replacement is successful. If you install the apache service again, you can see that yum has been replaced with the source of Alibaba Cloud.
[root@heimatengyun yum.repos.d]# yum install httpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
...
This is just to verify that the following sources are replaced successfully, press Ctrl+C to interrupt the installation
2.4 Local YUM
2.4.1 Why do you need to make a local YUM source
The YUM source can simplify the software installation process on Linux, but in some special cases, the production environment cannot access the external network and cannot connect to the external network's YUM source, so the software cannot be installed directly using the YUM command. In order to use YUM to install software on the intranet, you need to configure the local YUM source.
The essence of YUM source is a web server that saves multiple RPM packages, and can retrieve, download and install related RPM packages through http.
2.4.2 Making a local YUM source
(1) Prepare another virtual machine
Reinstall a centos virtual machine or clone a virtual machine as per article 1 of the series for the yum source server.
ps: Make a snapshot after the initial installation of the system, and use this snapshot to clone the virtual machine in the future to avoid time-consuming repeated installations
The cloned virtual machine is exactly the same as the original system, so it cannot be started at the same time, and the IP address and host name need to be modified.
Modify the ip address: login interface, modify the ip address to 192.168.78.101 according to the method in the previous article
Modify the host name: the terminal can execute the hostnamectl set-hostname yum-local-server command
(2) Mount the iso image to the directory
Execute the following commands in sequence to create a directory and mount it. After mounting, use the df command to check whether the mount is successful
[root@yum-local-server /]# mkdir /mnt/cdrom
[root@yum-local-server /]# mount /dev/cdrom /mnt/cdrom/
mount: /dev/sr0 is write-protected, mounting read-only
[root@yum-local-server /]# df -HT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/centos-root xfs 19G 4.8G 15G 26% /
devtmpfs devtmpfs 949M 0 949M 0% /dev
tmpfs tmpfs 958M 87k 958M 1% /dev/shm
tmpfs tmpfs 958M 9.4M 949M 1% /run
tmpfs tmpfs 958M 0 958M 0% /sys/fs/cgroup
/dev/sda1 xfs 521M 125M 397M 24% /boot
/dev/sr0 iso9660 4.2G 4.2G 0 100% /mnt/cdrom
(3) Modify the local YUM source configuration file
Back up the original YUM source configuration file and point the source to yourself.
[root@yum-local-server /]# cd /etc/yum.repos.d/
[root@yum-local-server yum.repos.d]# ls
CentOS-Base.repo CentOS-Debuginfo.repo CentOS-Sources.repo CentOS-Vault.repo
[root@yum-local-server yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak
[root@yum-local-server yum.repos.d]# ls
CentOS-Base.repo.bak CentOS-Sources.repo
CentOS-Debuginfo.repo CentOS-Vault.repo
[root@yum-local-server yum.repos.d]# vi CentOS-Base.repo
[base]
name=CentOS-Local
baseurl=file:///mnt/cdrom/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
save and quit
ps: The writing method is in a fixed format, name is the name of the custom warehouse, baseurl is the base address pointing to the mirror directory it is hung on, gpgcheck is a secret key function (can not be enabled, if not enabled, do not specify gpgkey), enabled is to start It means that if the value is 1, it means open, if it is 0, it means close
(4) Clear the cache
[root@yum-local-server yum.repos.d]# yum clean all
Loaded plugins: fastestmirror, langpacks
Cleaning repos: base
Cleaning up everything
Cleaning up list of fastest mirrors
(5) List available sources
[root@yum-local-server yum.repos.d]# yum repolist
Loaded plugins: fastestmirror, langpacks
base | 3.6 kB 00:00
(1/2): base/group_gz | 157 kB 00:00
(2/2): base/primary_db | 2.7 MB 00:00
Determining fastest mirrors
repo id repo name status
base CentOS-Local 3,538
repolist: 3,538
(6) Install apache service
Steps: install apache, open the httpd service (the default is closed), close the firewall
The method is the same as before, and the code will not be pasted here.
(7) Configure the YUM source to httpd
Copy the yum source to the web directory of httpd, so that other servers in the intranet can access the yum source in the intranet.
[root@yum-local-server yum.repos.d]# cp -r /mnt/cdrom/ /var/www/html/CentOS
(8) Browser access test
On the host directly by visiting http://192.168.78.101/CentOS/
Accessible indicates that the yum server configuration is successful
2.4.3 Using a local YUM source
Each centos server can be made into a local source without connecting to other yums, but if there are multiple servers, it can be made into a yum source server alone, and then each server modifies the yum configuration file and points the address to it. Can. Below we prepare a new virtual machine 192.168.78.102 (if the original 100 machine did not perform the yum remove httpd operation, it can also be used directly) to demonstrate the use of local yum.
Important: The reason why it is strongly recommended to install a new virtual machine here is that if you followed the steps above, it is likely that you have already uninstalled the httpd service in 2.2. If the uninstall operation has been performed, use the virtual machine to execute the following command again to replace the local yum to install httpd again, and the installation will continue to fail. I encountered such a problem and was very confused, and finally installed it successfully through a new virtual machine.
- Configure the server to use a local YUM source
[root@heimatengyun yum.repos.d]# mv CentOS-Base.repo CentOS-Base.repo.bak [root@heimatengyun yum.repos.d]# vi CentOS-Base.repo [base] name=CentOS-101 baseurl=http://192.168.78.101/CentOS/ gpgcheck=1 enabled=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
After saving and exiting, clear the cache and list the available yum sources
[root@heimatengyun yum.repos.d]# yum clean all
Loaded plugins: fastestmirror, langpacks
Cleaning repos: base
Cleaning up everything
Cleaning up list of fastest mirrors
[root@heimatengyun yum.repos.d]# yum repolist
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
repo id repo name status
base CentOS-101 3,538
repolist: 3,538
- Install the software using the local yun source
[root@heimatengyun yum.repos.d]# yum install httpd Loaded plugins: fastestmirror, langpacks ...省略部分内容 Installed: httpd.x86_64 0:2.4.6-17.el7.centos.1 Dependency Installed: apr.x86_64 0:1.4.8-3.el7 apr-util.x86_64 0:1.5.2-6.el7 httpd-tools.x86_64 0:2.4.6-17.el7.centos.1 mailcap.noarch 0:2.1.41-2.el7 Complete!
Successful installation.
After the installation and uninstallation of the software under centos7 and the configuration of the yum source in this article, the next section begins to explain the knowledge of pipe characters, redirection, environment variables, etc., to prepare for the subsequent shell programming
Original article, please indicate the source for reprinting:http://127.0.0.1:8000/system-administration-linux-tutorial/rpm-and-yum-repositories-for-software-management/