關(guān)于Linux網(wǎng)絡(luò)管理,軟件安裝,進(jìn)程管理總結(jié)
1、網(wǎng)絡(luò)管理
1.1 網(wǎng)絡(luò)狀態(tài)查看
在Linux中經(jīng)常使用ifconfig,route和netstat查看網(wǎng)絡(luò)狀態(tài),它們就是. net-tools工具,下面我來(lái)使用下。
我就說(shuō)下ifconfig和route
在我們的linux中有很多網(wǎng)卡接口,比如eth0第一塊網(wǎng)卡網(wǎng)絡(luò)接口,eno1板載⽹網(wǎng)卡, ens33 PCI-E⽹網(wǎng)卡 。CentOS 7 使⽤用了⼀致性⽹絡(luò)設(shè)備命名,以上都不匹配,則使⽤ eth0
- [root@node01 ~]# ifconfig eth0
- eth0: error fetching interface information: Device not found
- [root@node01 ~]# ifconfig ens33
- ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
- inet 192.168.92.90 netmask 255.255.255.0 broadcast 192.168.92.255
- inet6 fe80::b889:1772:c306:ef8f prefixlen 64 scopeid 0x20<link>
- ether 00:0c:29:07:43:5A txqueuelen 1000 (Ethernet)
- RX packets 910 bytes 954985 (932.6 KiB)
- RX errors 0 dropped 0 overruns 0 frame 0
- TX packets 450 bytes 38942 (38.0 KiB)
- TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
1.2 網(wǎng)絡(luò)配置文件
vim /etc/hosts網(wǎng)絡(luò)配置文件
在配置hadoop,elasticsearch集群的時(shí)候需要在/etc/hosts配置集群IP和主機(jī)名,有時(shí)候你ping不了百度,可能域名解析不了,需要在/etc/sysconfig/network-scripts/ifcfg-eth0配置
- [root@node01 ~]# vim /etc/sysconfig/network
- #########
- HOSTNAME=node01
- [root@node01 ~]# vim /etc/hosts
- #########
- 192.168.92.90 node01
- 192.168.92.91 node02
- 192.168.92.92 node03
- [root@node01 ~]# 配置DNS,域名解析服務(wù)
- [root@node01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
- DNS1=202.106.0.20
- DNS2=8.8.8.8
1.3 網(wǎng)絡(luò)故障排除命令
第一,ping百度:查看目標(biāo)機(jī)器的網(wǎng)絡(luò)是否可通
- maoli@ubuntu:~$ ping baidu.com
- PING baidu.com (220.181.38.148) 56(84) bytes of data.
- 64 bytes from 220.181.38.148: icmp_seq=1 ttl=128 time=49.6 ms
- 64 bytes from 220.181.38.148: icmp_seq=2 ttl=128 time=48.2 ms
- ^C
- --- baidu.com ping statistics ---
- 2 packets transmitted, 2 received, 0% packet loss, time 1001ms
- rtt min/avg/max/mdev = 48.223/48.948/49.673/0.725 ms
nslookup 工具就可以對(duì)域名解析, 格式是nslookup空格+域名
比如nslookup www.baidu.com 。Server即為域名對(duì)應(yīng)的ip
- maoli@ubuntu:~$ nslookup www.baidu.com
- Server: 127.0.1.1
- Address: 127.0.1.1#53
- Non-authoritative answer:
- www.baidu.com canonical name = www.a.shifen.com.
- Name: www.a.shifen.com
- Address: 182.61.200.6
- Name: www.a.shifen.com
- Address: 182.61.200.7
tcpdump是一個(gè)用于截取網(wǎng)絡(luò)分組,并輸出分組內(nèi)容的工具。憑借強(qiáng)大的功能和靈活的截取策略,使其成為類UNIX系統(tǒng)下用于網(wǎng)絡(luò)分析和問題排查的首選工具
比如tcpdump -i any -n port 80 抓取所有網(wǎng)卡(any)80端口數(shù)據(jù)包,并且以ip形式顯示(-n)
- maoli@ubuntu:~$ sudo tcpdump -i any -n port 80 -n
- tcpdump: verbose output suppressed, use -v or -vv for full protocol decode
- listening on any, link-type LINUX_SLL (Linux cooked), capture size 262144 bytes
- 10:28:11.003675 IP 192.168.92.1.53951 > 192.168.92.135.80: Flags [S], seq 185886164, win 64240, options [mss 1460,nop,wscale 8,nop,nop,sackOK], length 0
- 10:28:11.003875 IP 192.168.92.135.80 > 192.168.92.1.53951: Flags [S.], seq 2863640054, ack 185886165, win 64240, options [mss 1460,nop,nop,sackOK,nop,wscale 7], length 0
- 10:28:11.004114 IP 192.168.92.1.53951 > 192.168.92.135.80: Flags [.], ack 1, win 4106, length 0
- 10:28:11.010472 IP 192.168.92.1.53951 > 192.168.92.135.80: Flags [P.], seq 1:476, ack 1, win 4106, length 475: HTTP: GET /sqli-labs/ HTTP/1.1
比如tcpdump -i any -n host 10.0.0.1 and port 80 抓取所有網(wǎng)卡的80端口和10.0.0.1之間的數(shù)據(jù)包,并且以ip形式顯示
netstat命令用于顯示與IP、TCP、UDP和ICMP協(xié)議相關(guān)的統(tǒng)計(jì)數(shù)據(jù),一般用于檢驗(yàn)本機(jī)各端口的網(wǎng)絡(luò)連接情況。netstat是在內(nèi)核中訪問網(wǎng)絡(luò)及相關(guān)信息的程序,它能提供TCP連接,TCP和UDP監(jiān)聽,進(jìn)程內(nèi)存管理的相關(guān)報(bào)告。
netstat 查看服務(wù)監(jiān)聽端口狀態(tài)是否正確
-n 顯示ip地址-t tcp協(xié)議-p 顯示端口對(duì)應(yīng)的進(jìn)程-l tcp的監(jiān)聽狀態(tài)(listen)-ntpl 查看端口開放情況
- maoli@ubuntu:~$ sudo netstat -ntlp
- 激活I(lǐng)nternet連接 (僅服務(wù)器)
- Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
- tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1111/sshd
- tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN 14200/cupsd
- tcp 0 0 127.0.0.1:3306 0.0.0.0:* LISTEN 1148/mysqld
- tcp 0 0 127.0.0.1:6379 0.0.0.0:* LISTEN 1165/redis-server 1
- tcp 0 0 127.0.1.1:53 0.0.0.0:* LISTEN 1285/dnsmasq
- tcp6 0 0 :::22 :::* LISTEN 1111/sshd
- tcp6 0 0 ::1:631 :::* LISTEN 14200/cupsd
- tcp6 0 0 :::9000 :::* LISTEN 2234/docker-proxy
- tcp6 0 0 :::80 :::* LISTEN 1842/apache2
1.4 網(wǎng)絡(luò)服務(wù)管理
網(wǎng)絡(luò)服務(wù)管理程序分為兩種,分別為SysV和systemd。現(xiàn)在Systemd已經(jīng)基本取代了SysV的Init。
1.5 設(shè)置靜態(tài)ip
在搭建任何集群,都是要設(shè)置靜態(tài)ip的。
- [root@node01]# vim /etc/sysconfig/network-scripts/ifcfg-ens33
- ################
- BOOTPROTO=static
- ONBOOT="yes"
- # 網(wǎng)關(guān)地址根據(jù)系統(tǒng)的網(wǎng)絡(luò)而定
- GATEWAY=192.168.92.2
- # 設(shè)置的靜態(tài)ip
- IPADDR=192.168.92.92
- NETMASK=255.255.255.0
- # 配置DNS服務(wù)器
- DNS1=8.8.8.8
- DNS2=8.8.4.4
2. 軟件安裝
2.1 rpm安裝
在 Linux 操作系統(tǒng)下,幾乎所有的軟件均通過RPM 進(jìn)行安裝、卸載及管理等操作。RPM 的全稱為Redhat Package Manager ,是由Redhat 公司提出的,用于管理Linux 下軟件包的軟件,主要用于CentOS、RedHat等linux系統(tǒng),軟件安裝包格式為 rpm。
比如一個(gè)vim的rpm叫:vim-common-7.4.10-5.el7.x86_64.rpm 。vim-common是軟件名稱,7.4.10-5軟件版本,el7是Red Hat Enterprise Linux 指的是centos7系統(tǒng)版本,x86_64指的是系統(tǒng)平臺(tái)x86
rpm 命令常⽤參數(shù),-q 查詢軟件包,-i 安裝軟件包和-e 卸載軟件包
2.2 yum 包管理器
yum(全稱 Yellow dog Updater, Modified)是一個(gè)前端軟件包管理器,基于RPM包管理,能夠從指定的服務(wù)器自動(dòng)下載RPM包并且安裝,可以自動(dòng)處理依賴性關(guān)系。
下載阿里云的yum源到了/etc/yum.repos.d中
備份yum源
- mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
下載新的CentOS-Base.repo 到/etc/yum.repos.d/,這里指的是centos7
- wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
- # 運(yùn)行yum makecache生成緩存
- yum clean all
- yum makecache
這時(shí)候可以查看yum的base
- [root@node01 ~]# vim /etc/yum.repos.d/CentOS-Base.repo
- [base]
- name=CentOS-$releasever - Base - mirrors.aliyun.com
- failovermethod=priority
- baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
- http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
- http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
- gpgcheck=1
- gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
由于yum中有的mirror速度是非常慢的。對(duì)此,我可以下載fastestmirror插件。
- [root@node01 ~]# yum install yum-fastestmirror -y
- [root@node01 ~]# cat /etc/yum/pluginconf.d/fastestmirror.conf
- [main]
- enabled=1
- verbose=0
- always_print_best_host = true
- socket_timeout=3
- # Relative paths are relative to the cachedir (and so works for users as well
- # as root).
- hostfilepath=timedhosts.txt
- maxhostfileage=10
- maxthreads=15
- #exclude=.gov, facebook
- #include_only=.nl,.de,.uk,.ie
yum常用命令install 安裝軟件包,remove 卸載軟件包,list| grouplist 查看軟件包,update 升級(jí)軟件包
2.3 apt安裝
Ubuntu的高級(jí)打包工具(APT,Advanced Packaging Tool ),Debian、Ubuntu 使⽤ apt 包管理器,軟件安裝包格式為 deb。
apt安裝一個(gè)nginx
- maoli@ubuntu:~$ sudo apt-get install nginx
- /usr/sbin/nginx:主程序
- /etc/nginx:存放配置文件
- /usr/share/nginx:存放靜態(tài)文件
- /var/log/nginx:存放日志
2.4 make install編譯源碼安裝源碼的安裝
一般由3個(gè)步驟組成:配置(configure)、編譯(make)、安裝(make install)。
configure文件是一個(gè)可執(zhí)行的腳本文件,它有很多選項(xiàng),在待安裝的源碼目錄下使用命令./configure –help可以輸出詳細(xì)的選項(xiàng)列表。
其中--prefix選項(xiàng)是配置安裝目錄,如果不配置該選項(xiàng),安裝后可執(zhí)行文件默認(rèn)放在/usr /local/bin,庫(kù)文件默認(rèn)放在/usr/local/lib,配置文件默認(rèn)放在/usr/local/etc,其它的資源文件放在/usr /local/share,比較凌亂。
如果配置了--prefix,如:./configure --prefix=/usr/local/python3安裝后的所有資源文件都會(huì)被放在/usr/local/python3目錄中,不會(huì)分散到其他目錄。如果刪除直接刪除這個(gè)文件就可以了。
比如centos7安裝Python3.6
- [root@node01 ~]# yum install yum-utils
- [root@node01 ~]# yum install openssl-devel -y
- [root@node01 ~]# mkdir -p /usr/local/python3
- [root@node01 ~]# cd /usr/local/python3/
- [root@node01 python3]# wget https://www.python.org/ftp/python/3.6.7/Python-3.6.7.tgz
- [root@node01 python3]# tar -zxvf Python-3.6.7.tgz
- [root@node01 python3]# cd Python-3.6.7
- [root@node01 python3.6.7]# ./configure --prefix=/usr/local/python3 --with-ssl
- [root@node01 python3.6.7]# make && make install
- Installing collected packages: setuptools, pip
- Successfully installed pip-10.0.1 setuptools-39.0.
- [root@node01 python3.6.7]# cd ..
- [root@node01 Python3]# ln -s /usr/local/python3/bin/python3 /usr/bin/python3
- [root@node01 Python3]# ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
- [root@node01 Python3]# python3 -V
- Python 3.6.7
- [root@node01 Python3]# python3
- Python 3.6.7 (default, Mar 5 2020, 11:00:15)
- [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
- Type "help", "copyright", "credits" or "license" for more information.
- >>> exit()
3. 進(jìn)程管理
進(jìn)程 是 Unix 和 Linux 系統(tǒng)中對(duì)正在運(yùn)行中的應(yīng)用程序的抽象,通過它可以管理和監(jiān)視程序?qū)?nèi)存、處理器時(shí)間和 I / O 資源的使用。
3.1 殺進(jìn)程
很多時(shí)候需要?dú)⑦M(jìn)程,ps -ef可以查看所有的進(jìn)程,ps -ef|grep 查看具體的任務(wù)的進(jìn)程,
比如查看Mysql進(jìn)程
- [root@node01 ~]# ps -ef|grep mysql
- clouder+ 1726 1 38 15:16 ? 00:04:34 /usr/java/jdk1.8.0_241/bin/java -cp .:/usr/share/java/mysql-connector-java.jar:/usr/share/java/oracle-connector-java.jar:/usr/share/java/postgresql-connector-java.jar:lib/* -server -Dlog4j.configuration=file:/etc/cloudera-scm-server/log4j.properties -Dfile.encoding=UTF-8 -Dcmf.root.logger=INFO,LOGFILE -Dcmf.log.dir=/var/log/cloudera-scm-server -Dcmf.log.file=cloudera-scm-server.log -Dcmf.jetty.threshhold=WARN -Dcmf.schema.dir=/opt/cloudera/cm/schema -Djava.awt.headless=true -Djava.net.preferIPv4Stack=true -Dpython.home=/opt/cloudera/cm/python -XX:+UseConcMarkSweepGC -XX:+UseParNewGC -XX:+HeapDumpOnOutOfMemoryError -Xmx2G -XX:MaxPermSize=256m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/tmp -XX:OnOutOfMemoryError=kill -9 %p com.cloudera.server.cmf.Main
- mysql 2745 1 0 15:16 ? 00:00:04 /usr/sbin/mysqld --daemonize --pid-file=/var/run/mysqld/mysqld.pid
- root 9864 4959 0 15:28 pts/0 00:00:00 grep --color=auto mysql
查找與指定條件匹配的進(jìn)程 - 「pgrep」,,就是ps -ef|grep縮寫
- [root ~]$ pgrep mysqld
- 3584
查看端口的進(jìn)程,比如mysql的端口是3306
- [root@node01 ~]# lsof -i:3306
- COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
- java 1726 cloudera-scm 285u IPv4 50625 0t0 TCP localhost:58292->localhost:mysql (ESTABLISHED)
- mysqld 2745 mysql 27u IPv6 47164 0t0 TCP *:mysql (LISTEN)
- mysqld 2745 mysql 40u IPv6 54060 0t0 TCP localhost:mysql->localhost:58296 (E
殺進(jìn)程使用kill -9命令,比如 kill -9 1726,就是殺Mysql進(jìn)程
3.2 守護(hù)進(jìn)程
守護(hù)進(jìn)程就是通常說(shuō)的daemon進(jìn)程,是linux后臺(tái)執(zhí)行的一種進(jìn)程,不會(huì)隨著終端的關(guān)閉而停止運(yùn)行,開Linux系統(tǒng)的會(huì)自動(dòng)打開。
不掛斷地運(yùn)行命令。no hangup的縮寫,意即“不掛斷”。和&聯(lián)用
- [root@node01 ~]# tail -f /var/log/messages
- May 1 16:01:10 node01 kubelet: I0501 16:01:10.344757 26130 server.go:837] Client rotation is on, will bootstrap in background
- [root@node01 ~]# ps -ef|grep tail
- root 26210 25353 0 16:01 pts/1 00:00:00 tail -f /var/log/messages
- root 26555 25310 0 16:01 pts/0 00:00:00 grep --color=auto tail
- 關(guān)閉上面的tail -f /var/log/messages
- [root@node01 ~]# ps -ef|grep tail
- [root@node01 ~]# ps -ef|grep tail
- root 27353 25310 0 16:03 pts/0 00:00:00 grep --color=auto tail
一般需要nohup和 &連用
- [root@node01 ~]# nohup tail -f /var/log/messages &
- [1] 27718
- nohup: 忽略輸入并把輸出追加到"nohup.out"
- [root@node01 ~]# ps -ef|grep tail
- root 27718 25353 0 16:04 pts/1 00:00:00 tail -f /var/log/messages
- root 29444 25310 0 16:07 pts/0 00:00:00 grep --color=auto tail
- 關(guān)閉上面的nohup tail -f /var/log/messages &
- [root@node01 ~]# ps -ef|grep tail
- root 27718 1 0 16:04 ? 00:00:00 tail -f /var/log/messages
- root 29946 25310 0 16:08 pts/0 00:00:00 grep --color=auto tail
3.3 查看進(jìn)程
ps -ef查看進(jìn)程
- [root ~]# 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 ~]# 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
本文已收錄 GitHub,傳送門~[1] ,里面更有大廠面試完整考點(diǎn),歡迎 Star。
Reference
[1]傳送門~:https://github.com/MaoliRUNsen/runsenlearnpy100