Linux基础知识

• Linux是什么

– Linux是一套开源的类Unix操作系统,是一个多用户、多任务、支持多线程和多CPU的操

作系统。

• Linux特点

– 一切都是文件

– 模块化程度高、源码公开、安全性高、可靠性好、多任务多用户、相对资源消耗较低

• 发行版

– Red Hat Enterprise Linux

– SUSE Linux Enterprise

– Ubuntu

– CentOS

• 预备知识

• 文件系统目录结构

在这里插入图片描述

– /: 根目录,所有其他目录都由根目录衍生出来

– bin/sbin: 系统命令目录,如cat,chmod,chown,mv

– dev: 设备文件目录,如磁盘,U盘,光驱

– etc : 系统主要的配置文件,如/etc/hosts、/etc/passwd

– home : 用户主文件夹,”/home/用户名”为非root用户登录后的默认主目录

– mnt : 挂载目录,通常在此目录下挂载额外设备

– root: 系统管理员(root)的主文件夹

– tmp : 暂存目录,会定时清理,任何人都能访问

– usr : 操作系统软件资源目录,类似windows下的”C:\Windows\”和”C:\Program files\”

– var : 缓存、日志文件,某些软件运行产生的文件

– lost+found: 此目录的目的在于当文件系统发生错误时,将一些丢失的片段放置到这个目

录下

– proc: 此目录的数据都放置在内存中,不占用任何磁盘空间。存放如系统内核、进程、外

部设备的状态及网络状态

– sys: 跟/proc类似,主要也是记录与内核相关的信息

• 挂载点

– linux、unix这类操作系统将系统中的一切都作为文件来管理。在windows中我们常见的硬

件设备、磁盘分区等,在linux、unix中都被视作文件,对设备、分区的访问就是读写对

应的文件。

– 挂载点实际上就是linux中的磁盘文件系统的入口目录,类似于windows中的用来访问不同

分区的C:、D:、E:等盘符。

‼ 如将磁盘/dev/sda1挂载到根目录/下,磁盘/dev/sdb1挂载到/mnt/disk1目录下

• 磁盘阵列(RAID)

– 磁盘阵列是由很多价格较便宜的磁盘,组合成一个容量巨大的磁盘组,利用个别磁盘ᨀ

供数据所产生加成效果ᨀ升整个磁盘系统效能。

**– RAID 0:**N块同样的硬盘通过磁盘控制器串联在一起。容量ᨀ高N倍,读写速度ᨀ高N倍。

任何一块硬盘出现故障,整个系统将会受到破坏,可靠性仅为1/N

**– RAID 1:**把一个磁盘的数据镜像到另一个磁盘上。具备很好的磁盘冗余能力,但是磁盘

利用率为50%

‼ 现在的很多服务器只能识别RAID,不支持裸盘

• 交换分区SWAP

– Linux中Swap(即:交换分区),类似于Windows的虚拟内存,就是当内存不足的时候,

把一部分硬盘空间虚拟成内存使用,从而解决内存容量不足的情况。

– 它和Windows系统的交换文件作用类似,但是它是一段连续的磁盘空间,并且对用户不

可见

SWAP的大小一般为内存的1~2倍

• 绝对路径与相对路径

– “.”与”…”

. :代表当前的目录,也可以使用./来表示

… :代表上一层目录,也可以…/来表示

**– 绝对路径:**由根目录(/)开始写起的文件名或目录名称。如/home/dmtsai/passwd

**– 相对路径:**相对于目前路径的文件名写法。如./dmtsai/passwd或…/home/dmtsai/passwd

‼ pwd:用于查看用户现在的当前路径

• 基础命令

• 如何了解一个陌生的命令

– which :在默认路径中搜索命令,返回该命令的绝对路

– whereis :在相对比较大的范围搜索命令,返回该命令的绝对路

– whatis:用很简短的一句话来介绍命令

– man :查询一个命令的帮助手册

‼ 各发行版的命令会有些区别,可以通过man手册或者–help选项去查找对应命令的具体使 用方式

• ls – list directory contents

-a :do not ignore entries starting with.

-d :list directory entries instead of contents, and do not

dereference symbolic links

-l :use a long listing format16/11/27

• cd – change working directory

Ø change to the home directory of the user fred:

cd ~fred

Ø change to the directory lib that is a sibling directory of the current

one:

cd …/lib

• mkdir – make directories

Ø -p :no error if existing, make parent directories as needed

Ø -v :print a message for each created directory

• cat – concatenate files and print on the

standard output

Ø -E :display $ at end of each line

Ø -n :number all output lines

Ø with no FILE, or when FILE is -, read standard input

• mv – move (rename) files

Ø -f :do not prompt before overwriting

Ø -i :prompt before overwrite

Ø -u :move only when the SOURCE file is newer than the destination

file or when the destination file is missing

• cp – copy files and directories

Ø -f : if an existing destination file cannot be opened, remove it and

try again

Ø -i :prompt before overwrite

Ø -n :do not overwrite an existing file

• rm – remove files or directories

Ø -f :ignore nonexistent files, never prompt

Ø -i :prompt before every removal

Ø -r :remove directories and their contents recursively

‼ 删除文件时,务必使用此格式

‼ rm /etc/Hadoop/conf –rf

参数放在最后避免误操作

• echo – display a line of text

Ø -n :do not output the trailing newline

Ø -e :enable interpretation of backslash escapes

• 其他命令

– date

– reboot

– shutdown

– touch

– file

– du

– wc

– ln

– ……

• 服务器信息收集

Ø lsb_release -a :prints certain linux standard base and distribution information

Ø uname –a :print system information

Ø hostname :show or set the system’s host name

Ø cat /proc/cpuinfo

Ø cat /proc/meminfo

Ø cat /proc/swaps

Ø free –g :display amount of free and used memory in the system

Ø df –hT :report file system disk space usage

Ø ifconfig :configure a network interface

• 用户管理命令

– useradd :create a new user account

– groupadd :create a new group entry

– passwd :change user password

– userdel :delete an user account

• useradd

Ø -d :This option specifies the users home directory

Ø -G :With this option a list of supplementary groups can be specified, which the

user should become a member of.

Ø -g :The group name or number of the user’s main group.

Ø -u :Force the new userid to be the given number. This value must be positive

and unique.

Ø -m :Create home directory for new user account.

Ø -p :Encrypted password as returned by crypt for the new account. The default

is to disable the account.

• groupadd

Ø -g :Force the new group ID to be the given number. This value must

be positive and unique.

Ø -p :Encrypted password as returned by crypt for the new account.

The default is to disable the account.

• 用户信息路径

– /etc/passwd :用户信息

• 注册名:口令:用户标识号:组标识号:用户注释:用户主目录:命令解释程序

• root❌0:0:root:/root:/bin/bash

– /etc/shadow :存放用户密码

• /etc/shadow 由pwconv命令根据/etc/passwd中的数据自动产生的

– /etc/group :组信息

• 组名:口令:组标识号:组内用户列表

• root❌0:

• 文件权限

– linux中每个文件有所有者、所在组、其他组三个类别

– 每个类别有读®、写(w)、执行(x)三个权限,权值分别为4,2,1

– 如:-rw-r–r-- 1 root root 2009 Mar 10 14:51 /etc/passwd

rw- :文件所有者root有读写权限

r– :文件所在组只读权限

r– :其他用户也是只读权限

x:代表此文件可以作为shell被执行,文件目录必须有x权限才可以访问

• chown – change file owner and group

Ø -R :operate on files and directories recursively

Ø 如 chown –R root:root /etc/passwd

• chmod – change file mode bits

Ø -R :operate on files and directories recursively

Ø 如 chmod –R 755 /etc/passwd

• tar – The GNU version of the tar archiving

utility

Ø -c :create a new archive

Ø -x :extract files from an archive

Ø -f :use archive file or device ARCHIVE

Ø -j :filter the archive through bzip2

Ø -z :filter the archive through gzip

Ø -v :verbosely list files processed

Ø 如tar –xzvf file, tar –xjvf file

• 高级命令

• 磁盘分区、格式化、挂载命令

– fdisk :partition table manipulator for linux (<2TB)

– parted :a partition manipulation program (>=2TB)

– mkfs :build a linux file system

– mount :mount a filesystem

• fdisk

– fdisk –l :lists partition layout on all block devices

– fdisk /dev/sda :对sda磁盘做分区操作

• m :for help

• n : add a new partition

– primary / extended partition

– partition number

– first cylinder

– last cylinder

• w :write table to disk and exit

• parted

– parted –l :lists partition layout on all block devices

– parted /dev/sda :对sda磁盘做分区操作

• help :for help

• mkpart PART-TYPE [FS-TYPE] START END :make a partition

• mkpartfs PART-TYPE FS-TYPE START END :make a partition with a file

system

• quit :exit program

• mkfs

– ext4兼容ext3

– ext3支持最大16TB文件系统和最大2TB文件,ext4支持1EB的文件系统和

16TB的文件

– ext3支持32000个子目录,而ext4支持无限数量的子目录

– ext4支持快速fsck

– mkfs.ext3 :create an ext3 filesystem

– mkfs.ext4 :create an ext4 filesystem

• mount

– mount device dir :将分区文件系统挂载到指定文件夹下

– /etc/fstab :该文件包含了你的电脑上的存储设备及其文件系统的信息

• device mount point filesystem

parameters

dump

check

• /dev/sda1 swap swap

defaults 0 0

• /dev/sdb1 /mnt/disk1 ext4 defaults 0 0

– mount –a :mount all stuff from /etc/fstab

• fsck – check and repair a linux system

Ø -A :walk through the /etc/fstab file and try to check all file systems in one run

Ø -p :automatic repair (no questions)

Ø -f :force checking even if filesystem is marked clean

• sudo – execute a command as another user

Ø sudo allow a permitted user to execute a command as the superuser or

another user, as specified in the sudoers file

Ø -u :the –u option causes sudo to run the specified command as a user ither

than root

• su – run a shell with substitute user and group IDs

Ø - :make the shell a login shell

• find – search for files in a directory hierarchy

Ø -depth :process each directory’s contents before the directory itself

Ø maxdepth、mindepth

Ø -name :指定搜索符合名称规范的文件

Ø -type :指定搜索符合类型要求的文件

Ø -perm :指定搜索符合权限的文件

Ø find –name *.jar /usr

• iconv – perform character set conversion

Ø -f :encoding of original text

Ø -t :encoding for output

Ø -o :output file

Ø -l :list all know coded character sets

Ø find –name *.jar /usr

• 数据流重定向

在这里插入图片描述

– 我们执行一个命令的时候,这个命令可能会由文件读入数据,经过处理之

后,再将数据输出到屏幕上。standard output与standard error分别代表”标

准输出”与”标准错误”,这两个命令默认都是输出到屏幕上

– 标准输入:使用<或<<

– 标准输出:使用>或>>

– 标准错误输出:使用2>或2>>

– ls /root > /home/dir

– ls /root >> /home/dir

– wc –l < /home/dir

– >每一次操作会覆盖文件,>>为追加写入

• 管道命令

在这里插入图片描述

– 管道”|”把上一个命令的标准输出,作为下一个命令的标准输入传入

– 管道命令仅会处理standard output,对于standard error output会予以忽略

– ps –ef |grep “java”

*– find –name .jar / | jar -tf

• vim – Vi IMproved, aprogrammerstext editor

– 一般模式:默认模式,可以进行移动、删除、复制、粘贴

– 编辑模式:按下”i,I,o,O,a,A,r,R”等任一个字母进入,可以对文档

进行编辑

– 命令行模式:输入”:,/,?”3个中任一个进入,ᨀ供数据从的查找、

读取、保存、替换等操作

– ESC键从其他模式回到一般模式

• vim – 一般模式

– 上下左右箭头移动光标

– G :移动到这个文件的最后一行

– nG :n为数字,移动到这个文件的第n行

– gg :移动到这个文件的第一行

– dd :删除光标所在的那一整行

– ndd :删除光标所在的向下n行

– yy :复制光标所在的那一行

– nyy :复制光标所在的向下n行

– p :将已复制的数据在光标下一行粘贴

– u :复原前一个操作

• vim – 进入编辑模式

– i,I :i为从目前光标所在处插入,I为在目前所在行的第一个非空格符处插入

– a,A :a为从目前光标所在的下一个字符处开始插入,A为从光标所在行的最后一

个字符处插入

– o,O :o为在目前光标所在的下一行插入新的一行,O为在目前光标所在处的上

一行插入新的一行

– r,R :r只会替换光标所在的那一个字符一次,R会一直替换光标所在的文字,直

到按下ESC为止

• vim – 命令模式

– :w :将编辑的数据写入硬盘文件中

– :q :离开vi

– ! :强制执行命令

– ? :从文档当前行向前查找关键字(n,N进行切换)

– / :从文档当前行向后查找关键字(n,N进行切换)

– %s/{old}/{new}/g :全文替换指定字符串,{old}代表要被替换的字符串,

{new}代表要替换成的字符串

• 诊断命令

• 进程管理

– ps :report a snapshot of the current processes

– top :display linux tasks

– kill :send signals to processes, or list signals

‼ grep :print lines matching a pattern

• ps

Ø ps –ef :see every process on the system using standard syntax

Ø ps aux :see every process on the system using BSD syntax

Ø 用户

进程ID

上级父进程ID

cpu使用

cpu使用时间

终端机

具体指令

Ø UID PID PPID C STIME TTY TIME CMD

Ø root 1 0 0 Feb16 ? 00:00:29 init[5]

• top

在这里插入图片描述

• kill

Ø -s :specify the name or number of the signal to be sent

Ø -l :list signal names, or convert signal names to/from numbers
在这里插入图片描述

• netstat

– print network connections, routing tables, interface statistics, masquerade connections,

and ,ulticast memberships

Ø -a :show both listening and non-listening sockets

Ø -l :show only listening sockets

Ø -n :show numerical addresses instead of trying to determine symbolic hosts, port or user

names

Ø -p :show the PID and name of the program to which each socket belongs

Ø -t :tcp

Ø -u :udp

• iostat

– report Central Processing Unit (CPU) statistics and input/output statistics for devices,

partitions and network filesystems
在这里插入图片描述

• vmstat

– Report virtual memory statistics

在这里插入图片描述

r表示运行队列,b表示阻塞进程

swpd虚拟内存使用大小,free物理内存剩余大小

• sar

– Collect, report, or save system activity information

– -b :report I/O and transfer rate statistics

– -n {keyword} :report net work statistics. keywords are DEV,TCP,UDP,IP,IP6…

– -r :report memory utilization statistics

– -u report CPU utilization

– RHEL iso自带,yum install sar即可安装

• 其他命令

perf/top/netstat/pidstat/mpstat/dstat/free/sar/blktrace/iostat.strace/tcpdump/nicstat

• Shell

• 什么是Shell

– shell是用户和Linux操作系统之间的接口。Linux中有多种shell,其中缺省使

用的是Bash。

– Linux系统的shell作为操作系统的外壳,为用户ᨀ供使用操作系统的接口。

它是命令语言、命令解释程序及程序设计语言的统称。

• 注意事项

– 命令的执行是从上而下、从左到右地分析与执行

– 命令、参数间的多个空白都会被忽略掉

– 空白行也将被忽略

– 如果读到一个Enter符号,就尝试开始执行该行命令

– 至于如果一行的内容太多,则可以使用”\”来扩展至下一行

– “#”可作为批注。任何加在#后面的数据将全部被视为批注文字而被忽略

– shell.sh文件必须具备可读与可执行(rx)权限

– 第一行#!/bin/bash声明这个脚本使用的shell名称

• 示例

#!/bin/bash

#Program:

#

This program shows “Hello World!” in your screen.

PATH=/bin:/sbin:/user/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin

export PATH

echo –e “Hello World! \a \n”

exit0

• test

在这里插入图片描述

• if … then

if [条件判断式一]; then

当条件判断式一成立时,可以进行的命令工作内容

elif [条件判断式二]; then

当条件判断式二成立时,可以进行的命令工作内容

else

当条件判断式都不成立时,可以进行的命令工作内容

fi

• while do done

while [ condition ] <==中括号内为判断式

do <==循环的开始

程序段落

done <==循环的结束

• for … do … done

for var in con1 con2 con3 …

do <==循环的开始

程序段落

done <==循环的结束

• for … do … done

for ((初始值;限制值;执行步长))

do <==循环的开始

程序段落

done <==循环的结束

• 常用服务

• 网络服务

– /etc/init.d/network start 启动服务

– /etc/init.d/network stop 停止服务

– /etc/init.d/network restart

重启服务

– /etc/init.d/network status

查看服务状态

– /etc/sysconfig/network

hostname配置文件

– /etc/hosts

主机名及其ip

– /etc/resolv.conf

DNS解析文件

– /etc/sysconfig/network-scripts/ifcfg-eth0

网卡配置文件

– /etc/sysconfig/network

hostname配置文件

– NETWORKING=yes 设置linux网络是否运行

– HOSTNAME=WY

主机的名称

– GATEWAY=192.168.1.2

网关的IP

– /etc/hosts

主机名及其ip

– 一般情况下HOSTS文件的每行每一个主机,每行由三部分组成,每个部分由空格隔开

– 第一部分:网络IP地址

– 第二部分:主机名或域名

– 第三部分:主机别名

– 每行也可以是两部分,即主机IP地址和主机名

– 192.168.1.1

myhost

– /etc/resolv.conf

DNS解析文件

– 每行以一个关键字开头,后接配置参数

– nameserver 定义DNS服务器的IP地址

– domain

定义本地域名

– search

定义域名的搜索列表

– sortlist

对返回的域名进行排序

/etc/sysconfig/network-scripts/ifcfg-eth0

网卡配置文件

– DEVICE=eth0

网卡设备名称

– ONBOOT=yes

启动是否激活

– BOOTPROTO=static

协议类型:DHCP static none

– IPADDR=192.168.1.90

网络IP

– NETMASK=255.255.255.0

网络子网地址

– GATEWAY=192.168.1.1

网关地址

– TYPE=Ethernet

网卡类型为以太网

– ethtool device_name

• 检查网络设备的带宽情况

– ifconfig –a

• 查看网络接口信息

• ethtool

在这里插入图片描述

Speed:1000Mb/s

目前工作在千兆网状态下

Duplex:Full

工作模式是否是全双工

Link detected: yes 网卡是否处于激活状态

• tcpdump

– dump traffic on a network

– -i :Listen on interface. If unspecified, tcpdump searches the system interface

list for the lowest numbered, configured up interface.

– -w :Write the raw packets to file rather than parsing and printing them out.

– examples:

– tcpdump host sundown

– tcpdump tcp port 23 hosts 172.16.2.1

– tcpdump udp port 123

• telnet

– user interface to the TELNET protocol

– The telnet command is used to communicate with another ost using the

TELNET protocol

– examples:

– telnet [host] [port]

– telnet 172.16.2.1 22

• iptables防火墙

– administration tool for IPv4 packet filtering and NAT

– iptables –I input –p tcp --dport –j accept 80

– 80端口允许任何ip访问

– service iptablesstop

– 关闭防火墙

– iptables –f

清楚所有规则

• RedHat Package Manager

– rpm -ivh:安装显示安装进度

– rpm - Uvh:升级软件包

– rpm - qpl:列出RPM软件包内的文件信息

– rpm - qpi:列出RPM软件包的᧿述信息

– rpm - e:删除包

– 默认安装路径:/usr/bin,/usr/lib/usr/share/doc,/usr/share/man

– 安装包存在依赖关系,而rpm命令不能自动解决依赖

• cpio

– copy files to and from archives

– GNU cpio is a tool for creating and extracting archives, or copying files from

one place to another.

• rpm2cpio

– extract cpio archive from RPM Package Manager package

– 组合使用rpm2cpio {rpm包名} | cpio –ivd

– 将rpm包转换成

• Yellow dog Updater,Modified

– 基于rpm包管理,能够从指定的服务器自动下载包并且安装,可以自动处

理依赖关系

– yum install 安装

– yum update 更新

– yum list 显示安装包

– yum remove 删除程序

– yum search 查找软件包

– 源配置文件/etc/yum.repos.d/CentOS-Base.repo

[base] : 独一无二的名称

name=baserepo :对repo的描述

baseurl=ftp://172.16.1.1/pub :repo源的获取地址

gpgcheck=0 :是否进行校验

enabled=1 :是否ᨀ供源服务

• Create repomd repository

– -o:optional output directory

– -d:Generate sqlite databases for use with yum

– -p:Output xml files in pretty format

– createrepo –p –d –o yum/centos/6/x86_64/new yum/centos/6/x86_64/old

– rpm - qpi:列出RPM软件包的᧿述信息

– rpm - e:删除包

– 默认安装路径:/usr/bin,/usr/lib/usr/share/doc,/usr/share/man

– 安装包存在依赖关系,而rpm命令不能自动解决依赖

• cpio

– copy files to and from archives

– GNU cpio is a tool for creating and extracting archives, or copying files from

one place to another.

• rpm2cpio

– extract cpio archive from RPM Package Manager package

– 组合使用rpm2cpio {rpm包名} | cpio –ivd

– 将rpm包转换成

• Yellow dog Updater,Modified

– 基于rpm包管理,能够从指定的服务器自动下载包并且安装,可以自动处

理依赖关系

– yum install 安装

– yum update 更新

– yum list 显示安装包

– yum remove 删除程序

– yum search 查找软件包

– 源配置文件/etc/yum.repos.d/CentOS-Base.repo

[base] : 独一无二的名称

name=baserepo :对repo的描述

baseurl=ftp://172.16.1.1/pub :repo源的获取地址

gpgcheck=0 :是否进行校验

enabled=1 :是否ᨀ供源服务

• Create repomd repository

– -o:optional output directory

– -d:Generate sqlite databases for use with yum

– -p:Output xml files in pretty format

– createrepo –p –d –o yum/centos/6/x86_64/new yum/centos/6/x86_64/old

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值