学习笔记一: linux 基础知识
2016-08-24 14:37
489 查看
目录大纲一 文本编辑器
二 常见命令
2.1 history
2.2 bash
2.3 help pinfo
2.4 tar
2.5 yum
2.6 rpm
2.7 mount
2.8 lsof
2.9 find
2.10 grep
三 文件系统
3.1 磁盘划分fdisk
3.2 GPT gdisk
3.3 创建文件系统
3.4 挂载文件系统
3.5 uuid挂载文件系统
3.6 交换分区swap
四 正则表达式
五 输入输出
六 进程管理
七 网络
1.显示IP
2. 显示ip路由
3. 显示sockets统计
4.netstat
5.nmcli
6.修改网络文件
7.配置主机名和名称解析
8. 案例: 配置网络
9.NetworkManager
10.Network Teaming
11. sotfware bridges
八 定时任务管理
1.at
2.crontab
九 日志分析
一 文本编辑器
1.可以查看vim编辑30分钟指南 ,输入:vim -- > help tutor
VIM USER MANUAL - by Bram Moolenaar
查看手册: help usr_24.txt
2.查看接口 help interfaces
3.自定义菜单 $home/menu.vim
4.安装python自动扩展插件
需要下载插件安装包: https://github.com/rkulla/pydiction
另外可以根据自己从事的工作,配置适当的vim插件或环境变量,以便提高工作效率。
二 常见命令
2.1 history
-c 清除历史命令
-w 写入命令到文件,默认位置为.bash_history
2.2 bash
使用好下面的快捷键,可以加快输入命令行的速度,提高工作效率。
2.3 help pinfo
查看帮助信息:
pinfo cat
帮助文件目录:
/usr/share/doc
2.4 tar
a.打包文件
b.打包并压缩文件
c.解压文件
d.bzip压缩文件
2.5 yum
显示yum安装包
显示httpd安装包的详细信息
根据文件找包
2.6 rpm
显示包的详细信息:
显示包里面的文件:
显示包里面的配置文件:
根据文件找包:
2.7 mount
blkid 显示设备的UUID
mount UUID="XXXX" /mnt/
查看设备状态:
通过UUID挂载:
2.8 lsof
lsof /mnt 显示挂载目录正在访问的文件
可以查看到是有vi文件正在写文件到目录里面去:
2.9 find
查找sshd文件:
查找log结尾的文件:
查找用户为cw的文件
2.10 grep
以r开头的文件:
以sync结尾的文件:
v 不是以login$结尾的所有文件
-A显示匹配字符和后面一行
-B 显示匹配字符和匹配字符前面1行
[root@localhost ~]# more /etc/passwd | grep -B 1 'shutdown$'
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@localhost ~
-C 显示匹配字符和匹配字符的前后一行
三 文件系统
ext4
xfs
btrfs
gfs2
3.1 磁盘划分fdisk
#划分磁盘:
3.3 创建文件系统
[root@localhost ~]# mount /dev/sdb1 /data
3.5 uuid挂载文件系统
3.6 交换分区swap
激活:
swapon -a
检查:
free
四 正则表达式
五 输入输出
>> file 2>&1
find /etc -name aaa > /tmp/output 2> /tmp/error
六 进程管理
1.状态: R,S,D,K
2.查看进程状态
ps aux
3.后台运行和前台运行
ctrl+z
jobs
bg
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# bg
[1]+ vmstat 1 100 &
4.kill进程
kill -l 查看信号
七 网络
显示IP
ip addr show eth0
2. 显示ip路由
ip route
3. 显示sockets统计
ss -ta
5.nmcli
manage network settings and devices
使用nmcli创建网络连接
添加新的网卡,配置ip地址如下:
检查网卡设备:
配置网卡eth1地址
查看 ip地址
修改网络接口
删除网络接口信息:
nmcli con show 显示网络
nmcli dev show eth0 显示网卡详细信息
实验:创建网络
6.修改网络文件
7.配置主机名和名称解析
8. 案例: 配置网络
创建一个网络接口,配置下面IP地址:
network: lab
ip address: 10.1.1.1/24
gateway: 10.1.1.254
dns: 10.1.1.254
配置静态IP,并且自动启动
修改网络ip为 x.x.x.x
配置host文件为private
验证配置 lab network grade
9.NetworkManager
10.Network Teaming
nmcli con add type team con-name TEAM0 ifname TEAM0 config '{"runner":{"name":"loadbalance"}}
nmcli con mod team0 ipv4.addresses 1.1.1.1/24
nmcli con mod team0 ipv4.method manual
eg:
nmcli con add type team-slave ifname eth1 master team0
nmcli con add type team-slave ifname eth2 master team0 con-name team0-eth2
teamdctl team0 state
nmcli con add type team con-name team0 ifname team0 config '{"runner":{"name":"activebackup"}}'
ifcfg-team0
DEVICE=team0
DEVICETYPE=Team
TEAM_CONFIG="{\"runner\":{\"name\":\"broadcast\"}}"
BOOTPROTO=none
IPADDR0=1.1.1.1
PREFIX0=24
NAME=team0
ONBOOT=yes
ifcfg-team0-eth1
DEVICE=eth1
DEVICETYPE=TeamPort
TEAM_MASTER=team0
NAME=team0-eth1
ONBOOT=yes
nmcli con mod IFACE team.config JSON-configuration-file-or-string
teamnl team0 ports
teamnl team0 getoption activeport
teamnl team0 setoption activeport 3
teamdctl team0 config dump
11. sotfware bridges
ifcfg-br1
DEVICE=br1
NAME=br1
TYPE=Bridge
BOOTPROTO=none
IPADDR0=172.x.x.x
PREFIX0=24
STP=yes
BRIDGING_OPTS=priority=32768
brctl show : display software bridges
八 定时任务管理
at
atq
3分钟以后执行任务
echo "date > myjob.txt" | at now +3min
atq 查询任务
2. crontab
格式:
minutes hours day month week command
九 临时文件tmpfile
systemd-tmpfiles
/etc/tmpfiles.d/*.conf
Regular cleaning
systemd-tmpfiles --clean
clean.timer
九 日志分析
9.1 rsyslog.conf
八个优先级按照如下所述进行了标准化和评级
编码优先级严重性
0 emerg 系统不可用。
1 alert 必须立即采取措施。
2 crit 严重状况。
3 err 非常严重错误状况。
4 warning 警告状况。
5 notice 正常但重要的事件。
6 info 信息性事件。
7 debug 调试级别消息。
9.2 日志文件轮询
logrotate可以实现日志轮询,
9.3 logger验证
发送一条验证日志:logger -p local7.notice LOGSTRING
9.4 journalctl日志
journalctl从最旧的日志条目开始显示完整的系统日志
journalctl -n 显示最后10条
显示err错误级别日志
journalctl -f
搜索日志:
从哪里开始
journalctl --since today|tomorrow
到哪里结束journalctl --until "2016-08-08"
journalctl -o verbose其他用于搜索关于特定进程或事件的行的选项还有
_COMM 命令的名称
_EXE 进程的可执行文件的路径
_PID 进程的PID
_UID 运行该进程的用户的UID
_SYSTEMD_UNIT 启动该进程的 systemd 单元
journalctl -f 实时查看日志输出
二 常见命令
2.1 history
2.2 bash
2.3 help pinfo
2.4 tar
2.5 yum
2.6 rpm
2.7 mount
2.8 lsof
2.9 find
2.10 grep
三 文件系统
3.1 磁盘划分fdisk
3.2 GPT gdisk
3.3 创建文件系统
3.4 挂载文件系统
3.5 uuid挂载文件系统
3.6 交换分区swap
四 正则表达式
五 输入输出
六 进程管理
七 网络
1.显示IP
2. 显示ip路由
3. 显示sockets统计
4.netstat
5.nmcli
6.修改网络文件
7.配置主机名和名称解析
8. 案例: 配置网络
9.NetworkManager
10.Network Teaming
11. sotfware bridges
八 定时任务管理
1.at
2.crontab
九 日志分析
一 文本编辑器
1.可以查看vim编辑30分钟指南 ,输入:vim -- > help tutor
VIM USER MANUAL - by Bram Moolenaar
查看手册: help usr_24.txt
2.查看接口 help interfaces
3.自定义菜单 $home/menu.vim
4.安装python自动扩展插件
需要下载插件安装包: https://github.com/rkulla/pydiction
unzip pydiction-master.zip mkdir -p ~/.vim/tools/pydiction cp -rp after ~/.vim/ cp complete-dict ~/.vim/tools/pydiction/ [root@localhost ~]# cat ~/.vimrc filetype plugin on let g:pydiction_location = '~/.vim/tools/pydiction/complete-dict' [root@localhost ~]#
按下tab 就可以联想了。 [root@localhost ~]# vim cw.py import os print os.path ~ os.path /root/.vim/tools/pydiction/complete-dict
另外可以根据自己从事的工作,配置适当的vim插件或环境变量,以便提高工作效率。
二 常见命令
2.1 history
-c 清除历史命令
[root@localhost ~]# history -c [root@localhost ~]# history 1 history [root@localhost ~]#
-w 写入命令到文件,默认位置为.bash_history
2.2 bash
使用好下面的快捷键,可以加快输入命令行的速度,提高工作效率。
ctrl+a #jump to the beginning of the command line ctrl+e #跳转到行尾 ctrl+u # 删除目前光标之前的输入 ctrl+k #删除目前光标之后的输入 ctrl+left arrow ctrl+r #搜索历史命令,然后按tab键
2.3 help pinfo
查看帮助信息:
pinfo cat
帮助文件目录:
/usr/share/doc
2.4 tar
-c #create an archive -t #list the contents of an archive -x #extract an archive -v .z # gzip .j #bzip J #xz
a.打包文件
[root@localhost ~]# tar -cvf pydict.tar pydiction-master pydiction-master/ pydiction-master/README pydiction-master/README.md pydiction-master/after/ pydiction-master/after/ftplugin/ pydiction-master/after/ftplugin/python_pydiction.vim pydiction-master/complete-dict pydiction-master/pydiction.py [root@localhost ~]# ls -l pydict.tar -rw-r--r--. 1 root root 2641920 Aug 31 01:08 pydict.tar
b.打包并压缩文件
[root@localhost ~]# tar -cvf pydict.tar.gz pydiction-master pydiction-master/ pydiction-master/README pydiction-master/README.md pydiction-master/after/ pydiction-master/after/ftplugin/ pydiction-master/after/ftplugin/python_pydiction.vim pydiction-master/complete-dict pydiction-master/pydiction.py [root@localhost ~]# ls -l pydict.tar.gz -rw-r--r--. 1 root root 2641920 Aug 31 01:09 pydict.tar.gz
c.解压文件
[root@localhost ~]# tar -xvf pydict.tar -C test/ pydiction-master/ pydiction-master/README pydiction-master/README.md pydiction-master/after/ pydiction-master/after/ftplugin/ pydiction-master/after/ftplugin/python_pydiction.vim pydiction-master/complete-dict pydiction-master/pydiction.py [root@localhost ~]# ls -l test total 0 drwxr-xr-x. 3 root root 86 Jan 6 2016 pydiction-master [root@localhost ~]#
d.bzip压缩文件
[root@localhost ~]# bzip2 pydict.tar [root@localhost ~]# ls -l pydict pydiction-master/ pydiction-master.zip pydict.tar.bz2 pydict.tar.gz [root@localhost ~]# ls -l pydict.tar. pydict.tar.bz2 pydict.tar.gz [root@localhost ~]# ls -l pydict.tar* -rw-r--r--. 1 root root 429250 Aug 31 01:08 pydict.tar.bz2 -rw-r--r--. 1 root root 2641920 Aug 31 01:09 pydict.tar.gz [root@localhost ~]#
2.5 yum
yum repolist yum list yum* yum list installed yum grouplist yum info httpd yum provides /var/www #显示指定路径包显示仓库:
[root@localhost ~]# rpm -qi httpd Name : httpd Version : 2.4.6 Release : 17.el7.centos.1 Architecture: x86_64 Install Date: Wed 31 Aug 2016 01:18:58 AM MDT Group : System Environment/Daemons Size : 9793325 License : ASL 2.0 Signature : RSA/SHA256, Thu 03 Jul 2014 07:55:29 PM MDT, Key ID 24c6a8a7f4a80eb5 Source RPM : httpd-2.4.6-17.el7.centos.1.src.rpm Build Date : Tue 17 Jun 2014 01:12:34 PM MDT Build Host : worker1.bsys.centos.org Relocations : (not relocatable) Packager : CentOS BuildSystem <http://bugs.centos.org> Vendor : CentOS URL : http://httpd.apache.org/ Summary : Apache HTTP Server Description : The Apache HTTP Server is a powerful, efficient, and extensible web server. [root@localhost ~]# yum repolist Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile repo id repo name status base CentOS-7 - Base 3,538 repolist: 3,538 [root@localhost ~]#
显示yum安装包
[root@localhost ~]# yum list yum* Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile Installed Packages yum.noarch 3.4.3-118.el7.centos @anaconda yum-langpacks.noarch 0.4.2-3.el7 @base yum-metadata-parser.x86_64 1.1.4-10.el7 @anaconda yum-plugin-fastestmirror.noarch 1.1.31-24.el7 @anaconda yum-utils.noarch 1.1.31-24.el7 @base Available Packages yum-plugin-aliases.noarch 1.1.31-24.el7 base yum-plugin-changelog.noarch 1.1.31-24.el7 base yum-plugin-tmprepo.noarch 1.1.31-24.el7 base yum-plugin-verify.noarch 1.1.31-24.el7 base yum-plugin-versionlock.noarch 1.1.31-24.el7 base [root@localhost ~]#显示组可以安装的组列表:
[root@localhost ~]# yum grouplist Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile Installed environment groups: GNOME Desktop Available environment groups: Minimal Install Infrastructure Server File and Print Server Basic Web Server Virtualization Host Server with GUI KDE Plasma Workspaces Development and Creative Workstation Available Groups: Compatibility Libraries Console Internet Tools Development Tools Graphical Administration Tools Legacy UNIX Compatibility Scientific Support Security Tools Smart Card Support System Administration Tools System Management Done
显示httpd安装包的详细信息
[root@localhost ~]# yum info httpd Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile Installed Packages Name : httpd Arch : x86_64 Version : 2.4.6 Release : 17.el7.centos.1 Size : 9.3 M Repo : installed From repo : base Summary : Apache HTTP Server URL : http://httpd.apache.org/ License : ASL 2.0 Description : The Apache HTTP Server is a powerful, efficient, and extensible : web server. [root@localhost ~]#
根据文件找包
[root@localhost ~]# yum provides /etc/httpd Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile httpd-2.4.6-17.el7.centos.1.x86_64 : Apache HTTP Server Repo : base Matched from: Filename : /etc/httpd
2.6 rpm
rpm -q -i NAME #显示包的信息 rpm -q -l NAME #列出一个包的所有文件 rpm -q -c NAME #显示包里面的配置文件 rpm -q -d NAME #显示包里面的配置文档 rpm -q --changelog NAME
显示包的详细信息:
[root@localhost ~]# rpm -qi httpd Name : httpd Version : 2.4.6 Release : 17.el7.centos.1 Architecture: x86_64 Install Date: Wed 31 Aug 2016 01:18:58 AM MDT Group : System Environment/Daemons Size : 9793325 License : ASL 2.0 Signature : RSA/SHA256, Thu 03 Jul 2014 07:55:29 PM MDT, Key ID 24c6a8a7f4a80eb5 Source RPM : httpd-2.4.6-17.el7.centos.1.src.rpm Build Date : Tue 17 Jun 2014 01:12:34 PM MDT Build Host : worker1.bsys.centos.org Relocations : (not relocatable) Packager : CentOS BuildSystem <http://bugs.centos.org> Vendor : CentOS URL : http://httpd.apache.org/ Summary : Apache HTTP Server Description : The Apache HTTP Server is a powerful, efficient, and extensible web server. [root@localhost ~]#
显示包里面的文件:
[root@localhost ~]# rpm -ql httpd | head -n10 /etc/httpd /etc/httpd/conf /etc/httpd/conf.d /etc/httpd/conf.d/README /etc/httpd/conf.d/autoindex.conf /etc/httpd/conf.d/userdir.conf /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.modules.d /etc/httpd/conf.modules.d/00-base.conf /etc/httpd/conf.modules.d/00-dav.conf
显示包里面的配置文件:
[root@localhost ~]# rpm -qc httpd | head -n10 /etc/httpd/conf.d/autoindex.conf /etc/httpd/conf.d/userdir.conf /etc/httpd/conf.d/welcome.conf /etc/httpd/conf.modules.d/00-base.conf /etc/httpd/conf.modules.d/00-dav.conf /etc/httpd/conf.modules.d/00-lua.conf /etc/httpd/conf.modules.d/00-mpm.conf /etc/httpd/conf.modules.d/00-proxy.conf /etc/httpd/conf.modules.d/00-systemd.conf /etc/httpd/conf.modules.d/01-cgi.conf
根据文件找包:
[root@localhost ~]# rpm -qf /etc/httpd/conf.d/welcome.conf httpd-2.4.6-17.el7.centos.1.x86_64 [root@localhost ~]#
2.7 mount
blkid 显示设备的UUID
mount UUID="XXXX" /mnt/
查看设备状态:
[root@localhost ~]# blkid /dev/sdb1 /dev/sdb1: UUID="895e1ea6-5c46-469b-9cf3-b1644756ca42" TYPE="xfs" [root@localhost ~]#
通过UUID挂载:
[root@localhost ~]# mount UUID="895e1ea6-5c46-469b-9cf3-b1644756ca42" /data [root@localhost ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos-root 50G 4.1G 46G 9% / devtmpfs 908M 0 908M 0% /dev tmpfs 914M 80K 914M 1% /dev/shm tmpfs 914M 8.9M 905M 1% /run tmpfs 914M 0 914M 0% /sys/fs/cgroup /dev/mapper/centos-home 48G 33M 48G 1% /home /dev/sda1 497M 98M 400M 20% /boot /dev/sr0 3.9G 3.9G 0 100% /mnt /dev/sdb1 20G 33M 20G 1% /data [root@localhost ~]# touch /data/aaa [root@localhost ~]#
2.8 lsof
lsof /mnt 显示挂载目录正在访问的文件
可以查看到是有vi文件正在写文件到目录里面去:
[root@localhost ~]# lsof /data COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME bash 3507 root cwd DIR 8,17 31 128 /data vi 5220 root cwd DIR 8,17 31 128 /data vi 5220 root 4u REG 8,17 12288 132 /data/.aaa.swp [root@localhost ~]#-p 根据进程号,查找文件:
[root@localhost ~]# ps -ef | grep httpd root 5240 1 0 02:11 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 5241 5240 0 02:11 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 5242 5240 0 02:11 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND [root@localhost ~]# lsof -p 5240 COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME httpd 5240 root cwd DIR 253,1 4096 128 / httpd 5240 root rtd DIR 253,1 4096 128 / httpd 5240 root txt REG 253,1 502800 203669726 /usr/sbin/httpd httpd 5240 root mem REG 253,1 58288 201334842 /usr/lib64/libnss_files-2.17.so httpd 5240 root mem REG 253,1 27696 203466142 /usr/lib64/httpd/modules/mod_cgi.so httpd 5240 root mem REG 253,1 44048 201347814 /usr/lib64/librt-2.17.so
2.9 find
find / -name sshd find / -name '*.log' find / -iname '*.log' find -user cw find -group cw find -uid 100 find / -perm -324 find -size +/-10M find / -mmin 120 find / -type f/d/l/b
查找sshd文件:
[root@localhost ~]# find / -name sshd /etc/sysconfig/sshd
查找log结尾的文件:
[root@localhost ~]# find / -name '*.log' /root/.local/share/gvfs-metadata/home-23bb0ced.log /tmp/anaconda.log
查找用户为cw的文件
[root@localhost ~]# find / -user cw /dev/pts/2 /home/cw查找uid为1001的文件
[root@localhost ~]# id cw uid=1001(cw) gid=1001(cw) groups=1001(cw) [root@localhost ~]# find / -uid 1001 /dev/pts/2 /home/cw
2.10 grep
grep --color -E "vmx|svm" /pro/cpuinfo ^ $ -i -v -r -A #显示匹配之后的 -B #显示匹配之前的 -e
以r开头的文件:
[root@localhost ~]# more /etc/passwd | grep '^r' root:x:0:0:root:/root:/bin/bash rpc:x:32:32:Rpcbind Daemon:/var/lib/rpcbind:/sbin/nologin rtkit:x:172:172:RealtimeKit:/proc:/sbin/nologin radvd:x:75:75:radvd user:/:/sbin/nologin rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin [root@localhost ~]#
以sync结尾的文件:
[root@localhost ~]# more /etc/passwd | grep 'sync$' sync:x:5:0:sync:/sbin:/bin/sync [root@localhost ~]#
v 不是以login$结尾的所有文件
[root@localhost ~]# more /etc/passwd | grep -v 'nologin$' root:x:0:0:root:/root:/bin/bash sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt chenwei:x:1000:1000:chenwei:/home/chenwei:/bin/bash cw:x:1001:1001::/home/cw:/bin/bash [root@localhost ~]#
-A显示匹配字符和后面一行
[root@localhost ~]# more /etc/passwd | grep -A 1 'shutdown$' - shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt [root@localhost ~]#
-B 显示匹配字符和匹配字符前面1行
[root@localhost ~]# more /etc/passwd | grep -B 1 'shutdown$'
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
[root@localhost ~
-C 显示匹配字符和匹配字符的前后一行
[root@localhost ~]# more /etc/passwd | grep -C 1 'shutdown$' sync:x:5:0:sync:/sbin:/bin/sync shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown halt:x:7:0:halt:/sbin:/sbin/halt [root@localhost ~]#
三 文件系统
ext4
xfs
btrfs
gfs2
3.1 磁盘划分fdisk
#划分磁盘:
[root@localhost ~]# fdisk /dev/sdb #格式化磁盘sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): p #查看磁盘是否有分区 Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x995ab301 Device Boot Start End Blocks Id System Command (m for help): n #新增加一块分区 Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p #选择主分区 Partition number (1-4, default 1): First sector (2048-41943039, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): Using default value 41943039 Partition 1 of type Linux and of size 20 GiB is set Command (m for help): p Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x995ab301 Device Boot Start End Blocks Id System /dev/sdb1 2048 41943039 20970496 83 Linux Command (m for help): w #保存刚刚的分区 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# mkfs.xfs /dev/sdb1 #格式化磁盘分区 mkfs.xfs: /dev/sdb1 appears to contain an existing filesystem (xfs). mkfs.xfs: Use the -f option to force overwrite. [root@localhost ~]# mkfs.xfs -f /dev/sdb1 meta-data=/dev/sdb1 isize=256 agcount=4, agsize=1310656 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 data = bsize=4096 blocks=5242624, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@localhost ~]#3.2 GPT gdisk
[root@localhost ~]# gdisk /dev/sdb GPT fdisk (gdisk) version 0.8.6 Partition table scan: MBR: MBR only BSD: not present APM: not present GPT: not present *************************************************************** Found invalid GPT and valid MBR; converting MBR to GPT format. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if you don't want to convert your MBR partitions to GPT format! *************************************************************** Command (? for help): q [root@localhost ~]# gdisk /dev/sdb GPT fdisk (gdisk) version 0.8.6 Partition table scan: MBR: MBR only BSD: not present APM: not present GPT: not present *************************************************************** Found invalid GPT and valid MBR; converting MBR to GPT format. THIS OPERATION IS POTENTIALLY DESTRUCTIVE! Exit by typing 'q' if you don't want to convert your MBR partitions to GPT format! *************************************************************** Command (? for help): p Disk /dev/sdb: 41943040 sectors, 20.0 GiB Logical sector size: 512 bytes Disk identifier (GUID): 4D72610D-6B42-4E53-8DFA-C90623FA53B9 Partition table holds up to 128 entries First usable sector is 34, last usable sector is 41943006 Partitions will be aligned on 2048-sector boundaries Total free space is 41942973 sectors (20.0 GiB) Number Start (sector) End (sector) Size Code Name Command (? for help): n Partition number (1-128, default 1): First sector (34-41943006, default = 2048) or {+-}size{KMGTP}: Last sector (2048-41943006, default = 41943006) or {+-}size{KMGTP}: Current type is 'Linux filesystem' Hex code or GUID (L to show codes, Enter = 8300): p Hex code or GUID (L to show codes, Enter = 8300): L 0700 Microsoft basic data 0c01 Microsoft reserved 2700 Windows RE 4200 Windows LDM data 4201 Windows LDM metadata 7501 IBM GPFS 7f00 ChromeOS kernel 7f01 ChromeOS root 7f02 ChromeOS reserved 8200 Linux swap 8300 Linux filesystem 8301 Linux reserved 8e00 Linux LVM a500 FreeBSD disklabel a501 FreeBSD boot a502 FreeBSD swap a503 FreeBSD UFS a504 FreeBSD ZFS a505 FreeBSD Vinum/RAID a580 Midnight BSD data a581 Midnight BSD boot a582 Midnight BSD swap a583 Midnight BSD UFS a584 Midnight BSD ZFS a585 Midnight BSD Vinum a800 Apple UFS a901 NetBSD swap a902 NetBSD FFS a903 NetBSD LFS a904 NetBSD concatenated a905 NetBSD encrypted a906 NetBSD RAID ab00 Apple boot af00 Apple HFS/HFS+ af01 Apple RAID af02 Apple RAID offline af03 Apple label af04 AppleTV recovery af05 Apple Core Storage be00 Solaris boot bf00 Solaris root bf01 Solaris /usr & Mac Z bf02 Solaris swap bf03 Solaris backup bf04 Solaris /var bf05 Solaris /home bf06 Solaris alternate se bf07 Solaris Reserved 1 bf08 Solaris Reserved 2 bf09 Solaris Reserved 3 bf0a Solaris Reserved 4 bf0b Solaris Reserved 5 c001 HP-UX data c002 HP-UX service ed00 Sony system partitio ef00 EFI System ef01 MBR partition scheme ef02 BIOS boot partition fb00 VMWare VMFS fb01 VMWare reserved fc00 VMWare kcore crash p fd00 Linux RAID Hex code or GUID (L to show codes, Enter = 8300): 8300 Changed type of partition to 'Linux filesystem' Command (? for help): p Disk /dev/sdb: 41943040 sectors, 20.0 GiB Logical sector size: 512 bytes Disk identifier (GUID): 4D72610D-6B42-4E53-8DFA-C90623FA53B9 Partition table holds up to 128 entries First usable sector is 34, last usable sector is 41943006 Partitions will be aligned on 2048-sector boundaries Total free space is 2014 sectors (1007.0 KiB) Number Start (sector) End (sector) Size Code Name 1 2048 41943006 20.0 GiB 8300 Linux filesystem Command (? for help): w Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING PARTITIONS!! Do you want to proceed? (Y/N): y OK; writing new GUID partition table (GPT) to /dev/sdb. The operation has completed successfully.
3.3 创建文件系统
[root@localhost ~]# mkfs.xfs /dev/sdb1 -f meta-data=/dev/sdb1 isize=256 agcount=4, agsize=1310655 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 data = bsize=4096 blocks=5242619, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=03.4 挂载文件系统
[root@localhost ~]# mount /dev/sdb1 /data
3.5 uuid挂载文件系统
[root@localhost ~]# blkid /dev/sdb1 /dev/sdb1: UUID="895e1ea6-5c46-469b-9cf3-b1644756ca42" TYPE="xfs" [root@localhost ~]#
3.6 交换分区swap
fdisk /dev/vdb mkswap /dev/vdb2
激活:
swapon -a
检查:
free
[root@localhost ~]# mkswap /dev/sdb1 mkswap: /dev/sdb1: warning: wiping old xfs signature. Setting up swapspace version 1, size = 20970472 KiB no label, UUID=00fafac2-d13b-4b03-b75d-e36746b4be44 [root@localhost ~]# free -m total used free shared buffers cached Mem: 1826 1048 778 9 1 322 -/+ buffers/cache: 724 1102 Swap: 2063 0 2063 [root@localhost ~]# mkswap /dev/sdb1 mkswap: /dev/sdb1: warning: wiping old swap signature. Setting up swapspace version 1, size = 20970472 KiB no label, UUID=84a56b6a-63d8-4f2e-909d-bb365095d9df [root@localhost ~]# swapon -a /dev/sdb1 [root@localhost ~]# free -m total used free shared buffers cached Mem: 1826 1064 762 9 1 322 -/+ buffers/cache: 740 1086 Swap: 22542 0 22542 [root@localhost ~]#
四 正则表达式
* #any string of 0 or more characters. ? #any single character. ~ #the current user's home directory. ~username #user username's home directory. ~+ #the current working directory. ~- #the previous working directory. [abc..] [[:alpha:]] [[:lower:]] [[:upper:]] [[:idgit:]]
五 输入输出
standard input 0 standard output 1 standard error 2
>> file 2>&1
find /etc -name aaa > /tmp/output 2> /tmp/error
六 进程管理
1.状态: R,S,D,K
2.查看进程状态
ps aux
3.后台运行和前台运行
ctrl+z
jobs
bg
[root@localhost ~]#
[root@localhost ~]#
[root@localhost ~]# bg
[1]+ vmstat 1 100 &
4.kill进程
kill -l 查看信号
[root@localhost ~]# kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1 11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERM 16) SIGSTKFLT 17) SIGCHLD 18) SIGCONT 19) SIGSTOP 20) SIGTSTP 21) SIGTTIN 22) SIGTTOU 23) SIGURG 24) SIGXCPU 25) SIGXFSZ 26) SIGVTALRM 27) SIGPROF 28) SIGWINCH 29) SIGIO 30) SIGPWR 31) SIGSYS 34) SIGRTMIN 35) SIGRTMIN+1 36) SIGRTMIN+2 37) SIGRTMIN+3 38) SIGRTMIN+4 39) SIGRTMIN+5 40) SIGRTMIN+6 41) SIGRTMIN+7 42) SIGRTMIN+8 43) SIGRTMIN+9 44) SIGRTMIN+10 45) SIGRTMIN+11 46) SIGRTMIN+12 47) SIGRTMIN+13 48) SIGRTMIN+14 49) SIGRTMIN+15 50) SIGRTMAX-14 51) SIGRTMAX-13 52) SIGRTMAX-12 53) SIGRTMAX-11 54) SIGRTMAX-10 55) SIGRTMAX-9 56) SIGRTMAX-8 57) SIGRTMAX-7 58) SIGRTMAX-6 59) SIGRTMAX-5 60) SIGRTMAX-4 61) SIGRTMAX-3 62) SIGRTMAX-2 63) SIGRTMAX-1 64) SIGRTMAX [root@localhost ~]#
七 网络
显示IP
ip addr show eth0
[root@localhost ~]# ip addr show eno16777736 2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000 link/ether 00:0c:29:df:d2:6e brd ff:ff:ff:ff:ff:ff inet 192.168.85.253/24 brd 192.168.85.255 scope global eno16777736 valid_lft forever preferred_lft forever inet6 fe80::20c:29ff:fedf:d26e/64 scope link valid_lft forever preferred_lft forever [root@localhost ~]#
2. 显示ip路由
ip route
[root@localhost ~]# ip route default via 192.168.85.1 dev eno16777736 proto static metric 1024 192.168.85.0/24 dev eno16777736 proto kernel scope link src 192.168.85.253 [root@localhost ~]#
3. 显示sockets统计
ss -ta
[root@localhost ~]# ss -ta State Recv-Q Send-Q Local Address:Port Peer Address:Port LISTEN 0 100 127.0.0.1:smtp *:* LISTEN 0 128 *:sunrpc *:* LISTEN 0 128 *:ssh *:* LISTEN 0 128 *:55639 *:* ESTAB 0 0 192.168.85.253:ssh 192.168.85.1:57800 ESTAB 0 0 192.168.85.253:ssh 192.168.85.1:51713 ESTAB 0 0 192.168.85.253:ssh 192.168.85.1:58919 LISTEN 0 100 ::1:smtp :::* LISTEN 0 128 :::33902 :::* LISTEN 0 128 :::sunrpc :::* LISTEN 0 128 :::http :::* LISTEN 0 128 :::ssh :::* [root@localhost ~]#4.netstat
-n #show numbers instead of names for interfaces and ports -t #show TCP sockets -u #show UDP sockets. -l #show only listening sockets. -a #show all(listening and established) sockets. -p #show the process using the sockets.
5.nmcli
manage network settings and devices
nmcli con show #显示网络 nmcli dev status #显示设备详细状态 nmcli dev show eth0
使用nmcli创建网络连接
nmcli con add con-name "default" type ethernet ifname eth0 nmcli con add con-name "static" ifname eth0 autoconnect no type ethernet ip4 x.x.x.x/24 gw4 x.x.x. nmcli con up "static" nmcli con up "default"
添加新的网卡,配置ip地址如下:
检查网卡设备:
[root@localhost ~]# nmcli con show NAME UUID TYPE DEVICE eth0 0268a9ca-4f43-4adb-8a28-032a0c505a86 802-3-ethernet eno16777736 static e767f6b6-056c-4fd3-9ca1-4f18643fb61e 802-3-ethernet -- static 2d2dde98-2850-49c6-8582-b3b64f52bb21 802-3-ethernet -- Wired connection 1 8a9407a4-c9f2-4324-9f87-9220af337dc6 802-3-ethernet eno33554984 eno16777736 5e3e98d2-e5d5-4f32-a60e-941d1328f7e4 802-3-ethernet --添加网卡eth1
[root@localhost ~]# nmcli con add con-name "eth1" type ethernet ifname eno33554984 Connection 'eth1' (db99f951-578d-4618-ba54-d791fae52cad) successfully added.
配置网卡eth1地址
[root@localhost ~]# nmcli con add con-name eth1 ifname eno33554984 autoconnect no type ethernet ip4 192.168.124.253/24 gw4 192.168.124.1 Connection 'eth1' (5dfcfc03-75b9-4c46-96c4-0d0beb86ca3a) successfully added.激活网卡eth1
[root@localhost ~]# nmcli con up eth1 Connection successfully activated (D-Bus active path: /org/freedesktop/NetworkManager/ActiveConnection/3)
查看 ip地址
[root@localhost ~]# ifconfig eno16777736: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.85.128 netmask 255.255.255.0 broadcast 192.168.85.255 inet6 fe80::20c:29ff:fedf:d26e prefixlen 64 scopeid 0x20<link> ether 00:0c:29:df:d2:6e txqueuelen 1000 (Ethernet) RX packets 991 bytes 98610 (96.2 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 738 bytes 129912 (126.8 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 eno33554984: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.124.253 netmask 255.255.255.0 broadcast 192.168.124.255 inet6 fe80::250:56ff:fe2d:f2ca prefixlen 64 scopeid 0x20<link> ether 00:50:56:2d:f2:ca txqueuelen 1000 (Ethernet) RX packets 41 bytes 4841 (4.7 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 74 bytes 10719 (10.4 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10<host> loop txqueuelen 0 (Local Loopback) RX packets 11 bytes 1092 (1.0 KiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 11 bytes 1092 (1.0 KiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
修改网络接口
nmcli con mod "static" connection.autoconnect no nmcli con mod "static" ipv4.dns x.x.x.x nmcli con mod "static" +ipv4.dns 8.8.8.8 netcli con mod "static" ipv4.addresses "x.x.x/24 x.x.x" nmcli dev status nmcli net off nmcli con show --active nmcli con mod "System eth0" connection.autoconnect no
删除网络接口信息:
[root@localhost ~]# nmcli con show NAME UUID TYPE DEVICE eth0 0268a9ca-4f43-4adb-8a28-032a0c505a86 802-3-ethernet eno16777736 static e767f6b6-056c-4fd3-9ca1-4f18643fb61e 802-3-ethernet -- static 2d2dde98-2850-49c6-8582-b3b64f52bb21 802-3-ethernet -- eth1 26b9536b-f139-40d5-8a75-9b1222b10a35 802-3-ethernet -- eno16777736 5e3e98d2-e5d5-4f32-a60e-941d1328f7e4 802-3-ethernet -- [root@localhost ~]# nmcli con delete 26b9536b-f139-40d5-8a75-9b1222b10a35 [root@localhost ~]# nmcli con show NAME UUID TYPE DEVICE eth0 0268a9ca-4f43-4adb-8a28-032a0c505a86 802-3-ethernet eno16777736 static e767f6b6-056c-4fd3-9ca1-4f18643fb61e 802-3-ethernet -- static 2d2dde98-2850-49c6-8582-b3b64f52bb21 802-3-ethernet -- eno16777736 5e3e98d2-e5d5-4f32-a60e-941d1328f7e4 802-3-ethernet -- [root@localhost ~]#
nmcli con show 显示网络
[root@localhost ~]# nmcli con show NAME UUID TYPE DEVICE eno16777736 5e3e98d2-e5d5-4f32-a60e-941d1328f7e4 802-3-ethernet eno16777736 [root@localhost ~]#nmcli dev status 显示设备详细状态
[root@localhost ~]# nmcli dev status DEVICE TYPE STATE CONNECTION eno16777736 ethernet connected eno16777736 lo loopback unmanaged -- [root@localhost ~]#
nmcli dev show eth0 显示网卡详细信息
[root@localhost ~]# nmcli dev show eno16777736 GENERAL.DEVICE: eno16777736 GENERAL.TYPE: ethernet GENERAL.HWADDR: 00:0C:29:DF:D2:6E GENERAL.MTU: 1500 GENERAL.STATE: 100 (connected) GENERAL.CONNECTION: eno16777736 GENERAL.CON-PATH: /org/freedesktop/NetworkManager/ActiveConnection/0 WIRED-PROPERTIES.CARRIER: on IP4.ADDRESS[1]: ip = 192.168.85.253/24, gw = 192.168.85.1 IP6.ADDRESS[1]: ip = fe80::20c:29ff:fedf:d26e/64, gw = :: [root@localhost ~]#
实验:创建网络
6.修改网络文件
ifcfg BOOTPROTO=none IPADDR0=xxxxx PREFIX0=24 GATEWAY0=X.X.X.X DEFROUTE=yes DNS1=x.x.x. DOMAIN=example.com ONBOOT=yes NAME=eth0 nmcli con reload nmcli con down "System eth0" nmcli con up "System eth0"
7.配置主机名和名称解析
hostnamectl set-hostname xxx.example.com hostnamectl status
8. 案例: 配置网络
创建一个网络接口,配置下面IP地址:
network: lab
ip address: 10.1.1.1/24
gateway: 10.1.1.254
dns: 10.1.1.254
配置静态IP,并且自动启动
修改网络ip为 x.x.x.x
配置host文件为private
验证配置 lab network grade
#提示: nmcli con add con-name lab ifname eth0 type ethernet ipv4 10.1.1.1/24 gw4 10.1.1.254 nmcli con mod "lab" ipv4.dns 10.1.1.254 nmcli con mod "lab" connection.autoconnect no nmcli con mod "System eth0" connection.autoconnect no nmcli con mod "lab" +ipv4.addresses 10.1.1.1/24 or: echo "PREFIX1=24" >>/etc/sysconfig/network-scripts/ifcfg-lab echo "IPADDR1=10.1.1.1">>
9.NetworkManager
10.Network Teaming
teamd: broadcast roundrobin activebackup loadbalance lacp
nmcli con add type team con-name TEAM0 ifname TEAM0 config '{"runner":{"name":"loadbalance"}}
nmcli con mod team0 ipv4.addresses 1.1.1.1/24
nmcli con mod team0 ipv4.method manual
eg:
nmcli con add type team-slave ifname eth1 master team0
nmcli con add type team-slave ifname eth2 master team0 con-name team0-eth2
teamdctl team0 state
nmcli con add type team con-name team0 ifname team0 config '{"runner":{"name":"activebackup"}}'
ifcfg-team0
DEVICE=team0
DEVICETYPE=Team
TEAM_CONFIG="{\"runner\":{\"name\":\"broadcast\"}}"
BOOTPROTO=none
IPADDR0=1.1.1.1
PREFIX0=24
NAME=team0
ONBOOT=yes
ifcfg-team0-eth1
DEVICE=eth1
DEVICETYPE=TeamPort
TEAM_MASTER=team0
NAME=team0-eth1
ONBOOT=yes
nmcli con mod IFACE team.config JSON-configuration-file-or-string
teamnl team0 ports
teamnl team0 getoption activeport
teamnl team0 setoption activeport 3
teamdctl team0 config dump
11. sotfware bridges
nmcli con add type bridge con-name br0 ifname br0 nmcli con add type bridge-slave con-name br0-port1 ifname eth1 master br0 nmcli con add type bridge-slave con-name br0-port2 ifname eth2 master br0
ifcfg-br1
DEVICE=br1
NAME=br1
TYPE=Bridge
BOOTPROTO=none
IPADDR0=172.x.x.x
PREFIX0=24
STP=yes
BRIDGING_OPTS=priority=32768
brctl show : display software bridges
八 定时任务管理
at
atq
3分钟以后执行任务
echo "date > myjob.txt" | at now +3min
atq 查询任务
2. crontab
crontab -l #list the jobs for the current user. crontab -r #remove all jobs for the current user. crontab -e #edit jobs for the current user.
格式:
minutes hours day month week command
九 临时文件tmpfile
systemd-tmpfiles
/etc/tmpfiles.d/*.conf
Regular cleaning
systemd-tmpfiles --clean
clean.timer
九 日志分析
9.1 rsyslog.conf
八个优先级按照如下所述进行了标准化和评级
编码优先级严重性
0 emerg 系统不可用。
1 alert 必须立即采取措施。
2 crit 严重状况。
3 err 非常严重错误状况。
4 warning 警告状况。
5 notice 正常但重要的事件。
6 info 信息性事件。
7 debug 调试级别消息。
9.2 日志文件轮询
logrotate可以实现日志轮询,
9.3 logger验证
发送一条验证日志:logger -p local7.notice LOGSTRING
9.4 journalctl日志
journalctl从最旧的日志条目开始显示完整的系统日志
journalctl -n 显示最后10条
显示err错误级别日志
journalctl -f
搜索日志:
从哪里开始
journalctl --since today|tomorrow
到哪里结束journalctl --until "2016-08-08"
journalctl -o verbose其他用于搜索关于特定进程或事件的行的选项还有
_COMM 命令的名称
_EXE 进程的可执行文件的路径
_PID 进程的PID
_UID 运行该进程的用户的UID
_SYSTEMD_UNIT 启动该进程的 systemd 单元
journalctl -f 实时查看日志输出
相关文章推荐
- TQ2440 学习笔记—— 11、嵌入式编程基础知识【arm-linux-objcopy、objdump选项】
- Linux学习笔记一 ---- Linux基础知识认知以及初识Linux下C编程入门
- 2018.03.22学习笔记】【linux基础知识1.13-1.14】
- 【2018.03.21学习笔记】【linux基础知识1.9-1.12】
- Linux基础知识学习笔记(一)--文件系统的权限管理
- 【2018.03.23学习笔记】【linux基础知识2.1-2.5】
- 【2018.03.29学习笔记】【linux基础知识2.18-2.22】
- 【2018.03.28学习笔记】【linux基础知识2.14-2.17】
- fedora学习笔记 1:fedora与linux的一点基础知识
- ARM学习笔记——Linux基础知识
- 【学习笔记】Linux基础知识
- 【2018.03.22学习笔记】【linux基础知识1.15-1.16】
- s3c2440硬件学习笔记----嵌入式Linux基础知识和开发环境的构建
- 【2018.03.30学习笔记】【linux基础知识2.23-2.26】
- linux基础知识学习笔记
- Linux系统基础知识-嵌入式迅为4412开发板学习笔记
- 【2018.03.26学习笔记】【linux基础知识2.6-2.9】
- TQ2440 学习笔记—— 9、嵌入式编程基础知识【arm-linux-gcc 选项】
- 【2018.04.03学习笔记】【linux基础知识3.4-3.6】
- linux网络编程学习笔记之一 -----各种基础知识小结