Linux如何进行网络管理、软件安装、进程管理操作

60次阅读
没有评论

共计 10386 个字符,预计需要花费 26 分钟才能阅读完成。

这篇文章给大家分享的是有关 Linux 如何进行网络管理、软件安装、进程管理操作的内容。丸趣 TV 小编觉得挺实用的,因此分享给大家做个参考,一起跟随丸趣 TV 小编过来看看吧。

 1、网络管理

1.1 网络状态查看

在 Linux 中经常使用 ifconfig,route 和 netstat 查看网络状态,它们就是.  net-tools 工具,下面我来使用下。

我就说下 ifconfig 和 route

在我们的 linux 中有很多网卡接口,比如 eth0 第一块网卡网络接口,eno1 板载⽹网卡,ens33 PCI- E ⽹网卡  。CentOS 7 使⽤用了⼀致性⽹络设备命名,以上都不匹配,则使⽤ 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 网络配置文件

vim /etc/hosts 网络配置文件

在配置 hadoop,elasticsearch 集群的时候需要在 /etc/hosts 配置集群 IP 和主机名,有时候你 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,域名解析服务  [root@node01 ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0 DNS1=202.106.0.20 DNS2=8.8.8.8

1.3 网络故障排除命令

第一,ping 百度:查看目标机器的网络是否可通

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 工具就可以对域名解析, 格式是 nslookup 空格 + 域名

比如 nslookup www.baidu.com。Server 即为域名对应的 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 是一个用于截取网络分组,并输出分组内容的工具。凭借强大的功能和灵活的截取策略,使其成为类 UNIX 系统下用于网络分析和问题排查的首选工具

比如 tcpdump -i any -n port 80 抓取所有网卡 (any)80 端口数据包,并且以 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   抓取所有网卡的 80 端口和 10.0.0.1 之间的数据包,并且以 ip 形式显示

netstat 命令用于显示与 IP、TCP、UDP 和 ICMP 协议相关的统计数据,一般用于检验本机各端口的网络连接情况。netstat 是在内核中访问网络及相关信息的程序,它能提供 TCP 连接,TCP 和 UDP 监听,进程内存管理的相关报告。

netstat 查看服务监听端口状态是否正确

-n 显示 ip 地址 -t tcp 协议 -p 显示端口对应的进程 -l tcp 的监听状态 (listen)-ntpl 查看端口开放情况

maoli@ubuntu:~$ sudo netstat -ntlp  激活 Internet 连接  (仅服务器) 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 网络服务管理

网络服务管理程序分为两种,分别为 SysV 和 systemd。现在 Systemd 已经基本取代了 SysV 的 Init。

1.5 设置静态 ip

在搭建任何集群,都是要设置静态 ip 的。

[root@node01]# vim /etc/sysconfig/network-scripts/ifcfg-ens33 ################ BOOTPROTO=static ONBOOT= yes  #  网关地址根据系统的网络而定  GATEWAY=192.168.92.2 #  设置的静态 ip IPADDR=192.168.92.92 NETMASK=255.255.255.0 #  配置 DNS 服务器  DNS1=8.8.8.8 DNS2=8.8.4.4

2. 软件安装

2.1 rpm 安装

在 Linux 操作系统下,几乎所有的软件均通过 RPM 进行安装、卸载及管理等操作。RPM 的全称为 Redhat  Package Manager,是由 Redhat 公司提出的,用于管理 Linux   下软件包的软件,主要用于 CentOS、RedHat 等 linux 系统,软件安装包格式为 rpm。

比如一个 vim 的 rpm 叫:vim-common-7.4.10-5.el7.x86_64.rpm  。vim-common 是软件名称,7.4.10- 5 软件版本,el7 是 Red Hat Enterprise Linux   指的是 centos7 系统版本,x86_64 指的是系统平台 x86

rpm 命令常⽤参数,-q 查询软件包,-i 安装软件包和 -e 卸载软件包

2.2 yum 包管理器

yum(全称 Yellow dog Updater,  Modified) 是一个前端软件包管理器,基于 RPM 包管理,能够从指定的服务器自动下载 RPM 包并且安装,可以自动处理依赖性关系。

下载阿里云的 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 #  运行 yum makecache 生成缓存  yum clean all yum makecache

这时候可以查看 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 速度是非常慢的。对此,我可以下载 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 升级软件包

2.3 apt 安装

Ubuntu 的高级打包工具 (APT,Advanced Packaging Tool),Debian、Ubuntu 使⽤ apt   包管理器,软件安装包格式为 deb。

apt 安装一个 nginx

maoli@ubuntu:~$ sudo apt-get install nginx /usr/sbin/nginx:主程序  /etc/nginx:存放配置文件  /usr/share/nginx:存放静态文件  /var/log/nginx:存放日志 

2.4 make install 编译源码安装源码的安装

一般由 3 个步骤组成:配置 (configure)、编译 (make)、安装 (make  install)。

configure 文件是一个可执行的脚本文件,它有很多选项,在待安装的源码目录下使用命令./configure ndash;help 可以输出详细的选项列表。

其中 –prefix 选项是配置安装目录,如果不配置该选项,安装后可执行文件默认放在 /usr  /local/bin,库文件默认放在 /usr/local/lib,配置文件默认放在 /usr/local/etc,其它的资源文件放在 /usr  /local/share,比较凌乱。

如果配置了 –prefix,如:./configure  –prefix=/usr/local/python3 安装后的所有资源文件都会被放在 /usr/local/python3 目录中,不会分散到其他目录。如果删除直接删除这个文件就可以了。

比如 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. 进程管理

进程 是 Unix 和 Linux 系统中对正在运行中的应用程序的抽象,通过它可以管理和监视程序对内存、处理器时间和 I / O   资源的使用。

3.1 杀进程

很多时候需要杀进程,ps -ef 可以查看所有的进程,ps -ef|grep 查看具体的任务的进程,

比如查看 Mysql 进程

[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

查找与指定条件匹配的进程 –「pgrep」,,就是 ps -ef|grep 缩写

[root ~]$ pgrep mysqld 3584

查看端口的进程,比如 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

杀进程使用 kill - 9 命令,比如 kill -9 1726,就是杀 Mysql 进程

3.2 守护进程

守护进程就是通常说的 daemon 进程,是 linux 后台执行的一种进程,不会随着终端的关闭而停止运行,开 Linux 系统的会自动打开。

不挂断地运行命令。no hangup 的缩写,意即“不挂断”。和 联用

[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  关闭上面的 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  关闭上面的 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 查看进程

ps -ef 查看进程

[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

感谢各位的阅读!关于“Linux 如何进行网络管理、软件安装、进程管理操作”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-08-25发表,共计10386字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)