Centos7基本网络操作,防火墙,文件系统挂载及备份分区以及编码格式转换

1.Linux 基本操作和搭建服务器
网络这块
[xlxh@localhost sysconfig]$ cd network-scripts/
[xlxh@localhost network-scripts]$ ls
ifcfg-ens33 ifdown-ppp ifup-ib ifcfg-lo
[xlxh@localhost network-scripts]$ su
Password: 123456
[root@localhost network-scripts]#
[root@localhost network-scripts]# 当前模式是桥接模式
[root@localhost network-scripts]# cat ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=ee195a6f-3ce9-4bd4-99f5-20cce7276318
DEVICE=ens33
ONBOOT=no //这里不能为no,否则会出现下面问题
ping: www.baidu.com: Name or service not known
[root@mysqlcentos01 ~]# ping www.baidu.com
ping: www.baidu.com: Name or service not known
[root@mysqlcentos01 ~]#
这个修改:/etc/sysconfig/network-scripts/ifcfg-ens33中的ONBOOT为yes
[root@mysqlcentos01 ~]# ifconfig -a
-bash: ifconfig: command not found
[root@mysqlcentos01 ~]#
这个ping www.baidu.com通了以后安装
yum install net-tools

临时配置IP地址

临时修改IP地址方法,重启后会失效
Ifconfig 网卡名称 IP地址 ------直接修改网卡的IP地址,重启失效
[root@localhost network-scripts]# ifconfig ens33 192.168.1.111
[root@localhost network-scripts]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.111 netmask 255.255.255.0 broadcast 192.168.1.255
inet6 fe80::20c:29ff:fe98:8a26 prefixlen 64 scopeid 0x20
ether 00:0c:29:98:8a:26 txqueuelen 1000 (Ethernet)
RX packets 94 bytes 10140 (9.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 26 bytes 3920 (3.8 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
重启网卡后,上一步临时设置的IP地址不存在了,如下步骤:
[root@localhost network-scripts]# systemctl restart network
[root@localhost network-scripts]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:98:8a:26 txqueuelen 1000 (Ethernet)
RX packets 95 bytes 10200 (9.9 KiB)
RX errors 0 dropped 0 overruns 0 frame 0

方法2:添加多个临时IP地址
Ifconfig 网卡名称:0 第1个IP地址
Ifconfig 网卡名称:1 第2个IP地址
[root@localhost network-scripts]# ifconfig ens33:0 192.168.1.110
[root@localhost network-scripts]# ifconfig ens33:1 192.168.100.100
[root@localhost network-scripts]# ifconfig ens33:0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.1.110 netmask 255.255.255.0 broadcast 192.168.1.255
ether 00:0c:29:98:8a:26 txqueuelen 1000 (Ethernet)

[root@localhost network-scripts]# ifconfig ens33:1
ens33:1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.100.100 netmask 255.255.255.0 broadcast 192.168.100.255
ether 00:0c:29:98:8a:26 txqueuelen 1000 (Ethernet)

[root@localhost network-scripts]#
删除IP地址
Ifconfig 网卡名称 del IP地址---------------临时删除
[root@localhost network-scripts]# ifconfig ens33:0 del 192.168.1.110
[root@localhost network-scripts]# ifconfig ens33:0
ens33:0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:98:8a:26 txqueuelen 1000 (Ethernet)
RHEL/CENTOS linux 网络配置文件

永久配置IP地址

进入/etc/sysconfig/network-scripts/ --------------设置IP,lo,route
/etc/hostname---------配置主机名
/etc/resolv.conf----------配置DNS
或者输入 nmtui—图形化界面配置网络
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

[root@localhost network-scripts]# cat ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp //none:不指定;static:静态IP
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=ee195a6f-3ce9-4bd4-99f5-20cce7276318 //唯一识别ID
DEVICE=ens33
ONBOOT=yes //boot启动生效
IPADDR=10.10.10.10
PREFIX=24
GATEWAY=10.10.20.1
DNS1=10.10.20.1

或者通过VIM 编辑
/etc/sysconfig/network-scripts/ifcfg-ens33

2.防火墙设置

  • systemctl status firewalld.service //查看firewalld状态

    systemctl stop firewalld //关闭

    systemctl start firewalld //开启

    systemctl enable firewalld //开机自动开启

    systemctl disable firewalld //开机自动关闭

    chkconfig --list |grep network // 查看开机是否启动

2.1 临时和永久关闭Selinux
临时关闭
[root@localhost xlxh]# getenforce
Enforcing
[root@localhost xlxh]# setenforce 0
[root@localhost xlxh]# getenforce
Permissive
永久关闭或开启
进入 vim /etc/selinux/config
[root@localhost xlxh]# vim /etc/selinux/config

  • This file controls the state of SELinux on the system.

  • [root@localhost xlxh]# reboot
  • SELINUX= can take one of these three values:

    enforcing - SELinux security policy is enforced.

    permissive - SELinux prints warnings instead of enforcing.

    disabled - No SELinux policy is loaded. SELINUX=enforcing 或disabled

    SELINUXTYPE= can take one of three two values:

    targeted - Targeted processes are protected,

    minimum - Modification of targeted policy. Only selected processes are protected.

    mls - Multi Level Security protection.

SELINUXTYPE=targeted
[root@localhost xlxh]# setenforce ?
usage: setenforce [ Enforcing | Permissive | 1 | 0 ]
修改完之后执行#reboot 才生效
[root@localhost xlxh]# reboot

2.2 系统光盘开启自动挂载
开机会加载fstab这个文件
[root@localhost xlxh]# vim /etc/fstab

  • /etc/fstab

    Created by anaconda on Wed Apr 11 09:08:54 2018

    Accessible filesystems, by reference, are maintained under ‘/dev/disk’

    See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

/dev/mapper/centos-root / xfs defaults 0 0
UUID=959dd716-31fc-4a06-a2b1-59a7186c823e /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
//挂载的U盘路径
/dev/cdrom /mnt iso9660 0 0(表示开机不检测)
~或者这种方式加入/etc/fstab
[root@localhost xlxh]#
[root@localhost xlxh]# echo “/dev/cdrom /media iso9660 0 0” >> /etc/fstab
[root@localhost xlxh]# cat /etc/fstab

  • /etc/fstab

    Created by anaconda on Wed Apr 11 09:08:54 2018

    Accessible filesystems, by reference, are maintained under ‘/dev/disk’

    See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

/dev/mapper/centos-root / xfs defaults 0 0
UUID=959dd716-31fc-4a06-a2b1-59a7186c823e /boot xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
/dev/cdrom /media iso9660 0 0
[root@localhost xlxh]#
~
验证是否挂载成功
进入挂载目录下查看下是否有挂载U盘的内容
[root@localhost /]# cd /media/
[root@localhost media]# ls
查看所有挂载的目录
#mount –a

2.4 配置YUM源

yum的一切配置信息存储在一个叫yum.repos.d目录下的配置文件中,
删除原有的文件
自己创建一个CentOS7.Repo
[root@localhost yum.repos.d]# vim CentOS7.repo
[CentOS7] //yum的ID。必须唯一
name=CentOS-Server ----------》描述信息
baseurl=file:///media ----》挂载点
gpgcheck=0 ----》不校验
enabled=1 ---------》开启用

验证是否能用
[root@localhost yum.repos.d]# yum clean all //清空相关缓存
[root@localhost yum.repos.d]# yum list //查看列表
安装下httpd
yum –y install httpd

2.5 创建快照
在这里插入图片描述

  1. 登录linux终端
    1.通过tty命令查看当前所属的虚拟终端
    [root@localhost etc]# tty
    /dev/pts/3

Shift+ctrl+N开始打开一个终端
两个终端之间通信:
打开第一个终端,使用tty查看下(/dev/pts/1)
打开第二终端使用tty查看下,并记录(/dev/pts/2)
在第一个终端下输入:echo XXXXXX > /dev/pts/2 后,在第二个终端下看出该XXXXXXX信息
关机命令:
Init 0
Shutdown +10 //10分钟后关机
Shutdown now
Shutdown –c 取消关机

3.2 如何区分内外部命令
使用type 命令,

type cat

Cat是/usr/bin/cat
#type pwd
Pwd是内嵌

3.3 服务器设置开机自启动
进入biso界面—找到Integrated Peripheral------restore ON AC Power off修改成Power on

4.xfs文件系统的备份和恢复
XFS提供了xfsdump和xfsrestore工具协助备份,专为大数据产生,每个单文件可以支持16tb,扩展性高
Xfsdump的备份级别
0 完全备份
1-9 增量备份

实战:添加一个硬盘20G,在开机—》格式化fdisk /dev/sdb
----输入p
N
P
W
新创建的硬盘是不可用的,需要格式化一下在分盘
[root@localhost xlxh]#
[root@localhost xlxh]# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb
[root@localhost xlxh]# fdisk /dev/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.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x08f79ad4.

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: 0x08f79ad4

Device Boot Start End Blocks Id System
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): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition’s system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost xlxh]# ls /dev/sd*
/dev/sda /dev/sda1 /dev/sda2 /dev/sdb /dev/sdb1
[root@localhost xlxh]#

格式化文件sdb1
[root@localhost xlxh]# mkfs.xfs /dev/sdb1
meta-data=/dev/sdb1 isize=512 agcount=4, agsize=1310656 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=5242624, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=1
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 xlxh]# mkdir mount-sdb1
[root@localhost xlxh]# ls
Desktop Downloads Music Public Videos
Documents mount-sdb1 Pictures Templates
//把sdb1挂在到刚创建的目录下才可以用
[root@localhost xlxh]# mount /dev/sdb1 /home/xlxh/mount-sdb1/
[root@localhost xlxh]# ls
Desktop Downloads Music Public Videos
Documents mount-sdb1 Pictures Templates

1.备份整个分区(类似于虚拟机快照)
xfsdump -f 备份存放的路径 要备份的路径或设备
注意:要备份的路径名最后不能带/ 字符,可以是/dev/sdb1 或/sdb1 不能写成/sdb1/
->dump_sdb1 //指定备份会话标签
->sdb1 //指定设备标签
2.指定备份时免交互操作,方便后期做定时备份
Xfsdump –f /opt/dump_passwd /sdb1 –L dump_passwd –M sdb1
-L:记录每次备份的session标头,
-M:记录存储媒体的标头

3.指定只备份分区中某个目录
参数:-s:文件路径 只对指定的文件进行备份,-s 指定时, 路径写的是相对路径(-s 可以是文件或目录)

注意:当前目录在/sdb1下
Xfsdump –f /opt/dump_grup2 –s grub2/grub.cfg /boot –L dump_grup2 –M boot-sdb1

4.查看备份信息与内容
在/var/lib/xfsdump/inventory目录下看到生成的档案信息

4.测试备份恢复
步骤:先删除:rm –rf /sdb1/*
Xfsrestore –f

实战1: 在windows中编辑好的中文,放在linux上会有乱码现象

原因:编码问题
通过 iconv 命令转码
参数:-f,–from-code=名称
-t, --to-code=NAME encoding for output
Information:
-l, --list list all known coded character sets
Output control:
-c omit invalid characters from output
-o, --output=FILE output file
-s, --silent suppress warnings
–verbose print progress information
-?, --help Give this help list
–usage Give a short usage message
-V, --version Print program version

#iconv –f gb2312 –t utf8 aaa.txt

实战2:在linux服务器sh脚本导到windows中串行问题
处理回车问题:

unix2dos aaa.sh

猜你喜欢

转载自blog.csdn.net/weixin_42353331/article/details/86031659