Linux操作系统 - 软件安装与配置
一.使用包管理工具
- yum - Yellowdog Updater Modified。
yum search
:搜索软件包,例如yum search nginx
。yum list installed
:列出已经安装的软件包,例如yum list installed | grep zlib
。yum install
:安装软件包,例如yum install nginx
。yum remove
:删除软件包,例如yum remove nginx
。yum update
:更新软件包,例如yum update
可以更新所有软件包,而yum update tar
只会更新tar。yum check-update
:检查有哪些可以更新的软件包。yum info
:显示软件包的相关信息,例如yum info nginx
。
- rpm - Redhat Package Manager。
- 安装软件包:
rpm -ivh <packagename>.rpm
。 - 移除软件包:
rpm -e <packagename>
。 - 查询软件包:
rpm -qa
,例如可以用rpm -qa | grep mysql
来检查是否安装了MySQL相关的软件包。
- 安装软件包:
(1)安装Nginx为例,演示如何使用yum安装软件。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# yum -y install nginx
...
Installed:
nginx.x86_64 1:1.12.2-2.el7
Dependency Installed:
nginx-all-modules.noarch 1:1.12.2-2.el7
nginx-mod-http-geoip.x86_64 1:1.12.2-2.el7
nginx-mod-http-image-filter.x86_64 1:1.12.2-2.el7
nginx-mod-http-perl.x86_64 1:1.12.2-2.el7
nginx-mod-http-xslt-filter.x86_64 1:1.12.2-2.el7
nginx-mod-mail.x86_64 1:1.12.2-2.el7
nginx-mod-stream.x86_64 1:1.12.2-2.el7
Complete!
[root@iZwz97tbgo9lkabnat2lo8Z ~]# yum info nginx
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Installed Packages
Name : nginx
Arch : x86_64
Epoch : 1
Version : 1.12.2
Release : 2.el7
Size : 1.5 M
Repo : installed
From repo : epel
Summary : A high performance web server and reverse proxy server
URL : http://nginx.org/
License : BSD
Description : Nginx is a web server and a reverse proxy server for HTTP, SMTP, POP3 and
: IMAP protocols, with a strong focus on high concurrency, performance and low
: memory usage.
[root@iZwz97tbgo9lkabnat2lo8Z ~]# nginx -v
nginx version: nginx/1.12.2
移除Nginx。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# nginx -s stop
[root@iZwz97tbgo9lkabnat2lo8Z ~]# yum -y remove nginx
(2)安装MySQL为例,演示如何使用rpm安装软件。
要安装MySQL需要先到MySQL官方网站下载对应的RPM文件,当然要选择和你使用的Linux系统对应的版本。MySQL现在是Oracle公司旗下的产品,在MySQL被收购后,MySQL的作者重新制作了一个MySQL的分支MariaDB,可以通过yum进行安装。如果要安装MySQL需要先通过yum删除mariadb-libs
这个可能会跟MySQL底层库冲突的库,然后还需要安装一个名为libaio
的依赖库。
[root@iZwz97tbgo9lkabnat2lo8Z mysql]# ls
mysql-community-client-5.7.22-1.el7.x86_64.rpm
mysql-community-common-5.7.22-1.el7.x86_64.rpm
mysql-community-libs-5.7.22-1.el7.x86_64.rpm
mysql-community-server-5.7.22-1.el7.x86_64.rpm
[root@iZwz97tbgo9lkabnat2lo8Z mysql]# yum -y remove mariadb-libs
[root@iZwz97tbgo9lkabnat2lo8Z mysql]# yum -y install libaio
[root@iZwz97tbgo9lkabnat2lo8Z mysql]# ls | xargs rpm -ivh (也可以使用rpm -ivh 包名, 依次安装)
warning: mysql-community-client-5.7.22-1.el7.x86_64.rpm: Header V3 DSA/SHA1 Signature, key ID 5072e1f5: NOKEY
Preparing... ################################# [100%]
...
(windows版本启动用:net start mysql57或者mysql -h localhost -u root -p)
使用systemctl start mysqld
登陆是需要密码的,当不知道密码时,可采用修改配置文件设置免验证登陆
vim /etc/my.conf : 打开mysql配置文件
skip-grant-tables : 在最后一行添加,然后保存退出
bind-address=ip地址 : ip是本机eth0的ip地址,在现实mysqld那行下面一行添加
mysql -u root -p : 登陆到输密码的地方直接回车然后就进数据库了
进去之后首先清除免验证登陆 (清除权限)
flush privileges;
确认版本
select version();
设置用户密码
alter user 'root'@'localhost' identified by '123456';
此时已经可以本机正常登陆了,但是需要远程连接还是不行的,
需要进入mysql库中user表中 root的那条数据的host的值填成‘%’
use mysql;
update user set host='%' where user='root';
退出mysql后需要把配置文件/etc/my.conf中添加的skip-grant-tables行注释,
重启mysql服务器之后就能正常远程连接了,如果不能请重启服务和检查防火墙
mysql服务启停控制命令:
systemctl start mysqld : 开启
systemctl stop mysqld : 停止
systemctl restart mysqld : 重启
systemctl status mysqld : 查看状态
systemctl enable mysqld : 开机自启动
systemctl disable mysqld : 关闭开机自启动
netstat -nap | grep 3306 查看3306端口是否开通
说明:由于MySQL和MariaDB的底层依赖库是有冲突的,所以上面我们首先用
yum
移除了名为mariadb-libs的依赖库并安装了名为libaio的依赖库。由于我们将安装MySQL所需的rpm文件放在一个独立的目录中,所以可以通过ls
命令查看到安装文件并用xargs
将ls
的输出作为参数交给rpm -ivh
来进行安装。关于MySQL和MariaDB之间的关系,可以阅读维基百科上关于MariaDB的介绍。
移除安装的MySQL。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# rpm -qa | grep mysql | xargs rpm -e
二.下载解压配置环境变量
(1)安装MongoDB为例,演示这类软件应该如何安装。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.5.tgz
--2018-06-21 18:32:53-- https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-3.6.5.tgz
Resolving fastdl.mongodb.org (fastdl.mongodb.org)... 52.85.83.16, 52.85.83.228, 52.85.83.186, ...
Connecting to fastdl.mongodb.org (fastdl.mongodb.org)|52.85.83.16|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 100564462 (96M) [application/x-gzip]
Saving to: ‘mongodb-linux-x86_64-rhel70-3.6.5.tgz’
100%[==================================================>] 100,564,462 630KB/s in 2m 9s
2018-06-21 18:35:04 (760 KB/s) - ‘mongodb-linux-x86_64-rhel70-3.6.5.tgz’ saved [100564462/100564462]
[root@iZwz97tbgo9lkabnat2lo8Z ~]# gunzip mongodb-linux-x86_64-rhel70-3.6.5.tgz
[root@iZwz97tbgo9lkabnat2lo8Z ~]# tar -xvf mongodb-linux-x86_64-rhel70-3.6.5.tar
mongodb-linux-x86_64-rhel70-3.6.5/README
mongodb-linux-x86_64-rhel70-3.6.5/THIRD-PARTY-NOTICES
mongodb-linux-x86_64-rhel70-3.6.5/MPL-2
mongodb-linux-x86_64-rhel70-3.6.5/GNU-AGPL-3.0
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongodump
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongorestore
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongoexport
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongoimport
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongostat
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongotop
mongodb-linux-x86_64-rhel70-3.6.5/bin/bsondump
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongofiles
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongoreplay
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongoperf
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongod
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongos
mongodb-linux-x86_64-rhel70-3.6.5/bin/mongo
mongodb-linux-x86_64-rhel70-3.6.5/bin/install_compass
[root@iZwz97tbgo9lkabnat2lo8Z ~]# vim .bash_profile
...
PATH=$PATH:$HOME/bin:$HOME/mongodb-linux-x86_64-rhel70-3.6.5/bin
export PATH
...
[root@iZwz97tbgo9lkabnat2lo8Z ~]# source .bash_profile
[root@iZwz97tbgo9lkabnat2lo8Z ~]# mongod --version
db version v3.6.5
git version: a20ecd3e3a174162052ff99913bc2ca9a839d618
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64
[root@iZwz97tbgo9lkabnat2lo8Z ~]# mongo --version
MongoDB shell version v3.6.5
git version: a20ecd3e3a174162052ff99913bc2ca9a839d618
OpenSSL version: OpenSSL 1.0.1e-fips 11 Feb 2013
allocator: tcmalloc
modules: none
build environment:
distmod: rhel70
distarch: x86_64
target_arch: x86_64
说明:当然也可以通过yum来安装MongoDB,具体可以参照官方网站上给出的说明。
三.源代码构建安装
(1)安装Python 3.6
[root@iZwz97tbgo9lkabnat2lo8Z ~]# yum install gcc
[root@iZwz97tbgo9lkabnat2lo8Z ~]# wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tgz
[root@iZwz97tbgo9lkabnat2lo8Z ~]# gunzip Python-3.6.5.tgz
[root@iZwz97tbgo9lkabnat2lo8Z ~]# tar -xvf Python-3.6.5.tar
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cd Python-3.6.5
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ./configure --prefix=/usr/local/python36 --enable-optimizations
[root@iZwz97tbgo9lkabnat2lo8Z ~]# yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
[root@iZwz97tbgo9lkabnat2lo8Z ~]# make && make install
... 配置环境变量 ...
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ln -s /usr/local/python36/bin/python3.6 /usr/bin/python3
[root@iZwz97tbgo9lkabnat2lo8Z ~]# python3 --version
Python 3.6.5
[root@iZwz97tbgo9lkabnat2lo8Z ~]# python3 -m pip install -U pip
[root@iZwz97tbgo9lkabnat2lo8Z ~]# pip3 --version
(2)安装Redis-3.2.12
[root@iZwz97tbgo9lkabnat2lo8Z ~]# wget http://download.redis.io/releases/redis-3.2.12.tar.gz
[root@iZwz97tbgo9lkabnat2lo8Z ~]# gunzip redis-3.2.12.tar.gz
[root@iZwz97tbgo9lkabnat2lo8Z ~]# tar -xvf redis-3.2.12.tar
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cd redis-3.2.12
[root@iZwz97tbgo9lkabnat2lo8Z ~]# make && make install
[root@iZwz97tbgo9lkabnat2lo8Z ~]# redis-server --version
Redis server v=3.2.12 sha=00000000:0 malloc=jemalloc-4.0.3 bits=64 build=5bc5cd3c03d6ceb6
[root@iZwz97tbgo9lkabnat2lo8Z ~]# redis-cli --version
redis-cli 3.2.12
四.配置服务
启动服务。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# systemctl start firewalld
终止服务。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# systemctl stop firewalld
重启服务。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# systemctl restart firewalld
查看服务。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# systemctl status firewalld
设置是否开机自启。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# systemctl enable firewalld Created symlink from /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service to /usr/lib/systemd/system/firewalld.service. Created symlink from /etc/systemd/system/multi-user.target.wants/firewalld.service to /usr/lib/systemd/system/firewalld.service. [root@iZwz97tbgo9lkabnat2lo8Z ~]# systemctl disable firewalld Removed symlink /etc/systemd/system/multi-user.target.wants/firewalld.service. Removed symlink /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
五.计划任务
crontab命令。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# crontab -e * * * * * echo "hello, world!" >> /root/hello.txt 59 23 * * * rm -f /root/*.log
说明:输入
crontab -e
命令会打开vim来编辑Cron表达式并指定触发的任务,上面我们定制了两个计划任务,一个是每分钟向/root目录下的hello.txt中追加输出hello, world!
;另一个是每天23时59分执行删除/root目录下以log为后缀名的文件。如果不知道Cron表达式如何书写,可以参照/etc/crontab文件中的提示(下面会讲到)或者用谷歌或百度搜索一下,也可以使用Cron表达式在线生成器来生成Cron表达式。crontab相关文件。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# cd /etc [root@iZwz97tbgo9lkabnat2lo8Z etc]# ls -l | grep cron -rw-------. 1 root root 541 Aug 3 2017 anacrontab drwxr-xr-x. 2 root root 4096 Mar 27 11:56 cron.d drwxr-xr-x. 2 root root 4096 Mar 27 11:51 cron.daily -rw-------. 1 root root 0 Aug 3 2017 cron.deny drwxr-xr-x. 2 root root 4096 Mar 27 11:50 cron.hourly drwxr-xr-x. 2 root root 4096 Jun 10 2014 cron.monthly -rw-r--r-- 1 root root 493 Jun 23 15:09 crontab drwxr-xr-x. 2 root root 4096 Jun 10 2014 cron.weekly [root@iZwz97tbgo9lkabnat2lo8Z etc]# vim crontab 1 SHELL=/bin/bash 2 PATH=/sbin:/bin:/usr/sbin:/usr/bin 3 MAILTO=root 4 5 # For details see man 4 crontabs 6 7 # Example of job definition: 8 # .---------------- minute (0 - 59) 9 # | .------------- hour (0 - 23) 10 # | | .---------- day of month (1 - 31) 11 # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... 12 # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat 13 # | | | | | 14 # * * * * * user-name command to be executed
通过修改
/etc
目录下的crontab文件也能够定制计划任务。
六.网络访问和管理
通过网络获取资源 - wget。
- -b 后台下载模式
- -O 下载到指定的目录
- -r 递归下载
显示/操作网络配置(旧) - ifconfig。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ifconfig eth0 eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 172.18.61.250 netmask 255.255.240.0 broadcast 172.18.63.255 ether 00:16:3e:02:b6:46 txqueuelen 1000 (Ethernet) RX packets 1067841 bytes 1296732947 (1.2 GiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 409912 bytes 43569163 (41.5 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions
显示/操作网络配置(新) - ip。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ip address 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever 2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:16:3e:02:b6:46 brd ff:ff:ff:ff:ff:ff inet 172.18.61.250/20 brd 172.18.63.255 scope global eth0 valid_lft forever preferred_lft forever
网络可达性检查 - ping。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ping www.baidu.com -c 3 PING www.a.shifen.com (220.181.111.188) 56(84) bytes of data. 64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=1 ttl=51 time=36.3 ms 64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=2 ttl=51 time=36.4 ms 64 bytes from 220.181.111.188 (220.181.111.188): icmp_seq=3 ttl=51 time=36.4 ms --- www.a.shifen.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2002ms rtt min/avg/max/mdev = 36.392/36.406/36.427/0.156 ms
查看网络服务和端口 - netstat。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# netstat -nap | grep nginx
安全文件拷贝 - scp。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# scp root@1.2.3.4:/root/guido.jpg hellokitty@4.3.2.1:/home/hellokitty/pic.jpg
安全文件传输 - sftp。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# sftp root@120.77.222.217 root@120.77.222.217's password: Connected to 120.77.222.217. sftp>
help
:显示帮助信息。ls
/lls
:显示远端/本地目录列表。cd
/lcd
:切换远端/本地路径。mkdir
/lmkdir
:创建远端/本地目录。pwd
/lpwd
:显示远端/本地当前工作目录。get
:下载文件。put
:上传文件。rm
:删除远端文件。bye
/exit
/quit
:退出sftp。
七.进程管理
ps - 查询进程。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ps -ef UID PID PPID C STIME TTY TIME CMD root 1 0 0 Jun23 ? 00:00:05 /usr/lib/systemd/systemd --switched-root --system --deserialize 21 root 2 0 0 Jun23 ? 00:00:00 [kthreadd] ... [root@iZwz97tbgo9lkabnat2lo8Z ~]# ps -ef | grep mysqld root 4943 4581 0 22:45 pts/0 00:00:00 grep --color=auto mysqld mysql 25257 1 0 Jun25 ? 00:00:39 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
kill - 终止进程。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# kill 1234 [root@iZwz97tbgo9lkabnat2lo8Z ~]# kill -9 1234
例子:用一条命令强制终止正在运行的Redis进程。
ps -ef | grep redis | grep -v grep | awk '{print $2}' | xargs kill
将进程置于后台运行。
Ctrl+Z
&
[root@iZwz97tbgo9lkabnat2lo8Z ~]# mongod & [root@iZwz97tbgo9lkabnat2lo8Z ~]# redis-server ... ^Z [4]+ Stopped redis-server
jobs - 查询后台进程。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# jobs [2] Running mongod & [3]- Stopped cat [4]+ Stopped redis-server
bg - 让进程在后台继续运行。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# bg %4 [4]+ redis-server & [root@iZwz97tbgo9lkabnat2lo8Z ~]# jobs [2] Running mongod & [3]+ Stopped cat [4]- Running redis-server &
fg - 将后台进程置于前台。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# fg %4 redis-server ^C5554:signal-handler (1530025281) Received SIGINT scheduling shutdown... 5554:M 26 Jun 23:01:21.413 # User requested shutdown... 5554:M 26 Jun 23:01:21.413 * Saving the final RDB snapshot before exiting. 5554:M 26 Jun 23:01:21.415 * DB saved on disk 5554:M 26 Jun 23:01:21.415 # Redis is now ready to exit, bye bye...
说明:置于前台的进程可以使用
Ctrl+C
来终止它。top - 进程监控。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# top top - 23:04:23 up 3 days, 14:10, 1 user, load average: 0.00, 0.01, 0.05 Tasks: 65 total, 1 running, 64 sleeping, 0 stopped, 0 zombie %Cpu(s): 0.3 us, 0.3 sy, 0.0 ni, 99.3 id, 0.0 wa, 0.0 hi, 0.0 si, 0.0 st KiB Mem : 1016168 total, 191060 free, 324700 used, 500408 buff/cache KiB Swap: 0 total, 0 free, 0 used. 530944 avail Mem ...
八.系统性能
查看系统活动信息 - sar。
查看内存使用情况 - free。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# free total used free shared buff/cache available Mem: 1016168 323924 190452 356 501792 531800 Swap: 0 0 0
查看进程使用内存状况 - pmap。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# ps PID TTY TIME CMD 4581 pts/0 00:00:00 bash 5664 pts/0 00:00:00 ps [root@iZwz97tbgo9lkabnat2lo8Z ~]# pmap 4581 4581: -bash 0000000000400000 884K r-x-- bash 00000000006dc000 4K r---- bash 00000000006dd000 36K rw--- bash 00000000006e6000 24K rw--- [ anon ] 0000000001de0000 400K rw--- [ anon ] 00007f82fe805000 48K r-x-- libnss_files-2.17.so 00007f82fe811000 2044K ----- libnss_files-2.17.so ...
报告设备CPU和I/O统计信息 - iostat。
[root@iZwz97tbgo9lkabnat2lo8Z ~]# iostat Linux 3.10.0-693.11.1.el7.x86_64 (iZwz97tbgo9lkabnat2lo8Z) 06/26/2018 _x86_64_ (1 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 0.79 0.00 0.20 0.04 0.00 98.97 Device: tps kB_read/s kB_wrtn/s kB_read kB_wrtn vda 0.85 6.78 21.32 2106565 6623024 vdb 0.00 0.01 0.00 2088 0