网卡命名
传统网卡命名机制
传统网卡 | 命名 |
---|
以太网 | eth[0,1,2,…] | wlan | [0,1,2,…] |
回归传统命名
修改网卡配置文件
[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# mv ifcfg-ens160 ifcfg-eth0
[root@localhost network-scripts]# vim ifcfg-eth0
TYPE="Ethernet"
BOOTPROTO="dhcp"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
NAME="eth0"
UUID="a45efe53-9306-49bd-ae33-457a4377b7f9"
DEVICE="eth0"
ONBOOT="yes"
编辑/etc/default/grub配置文件,在以GRUB_CMDLINE_LINUX开头的行内rhgb的前面加上net.ifnames=0 biosdevname=0
[root@localhost ~]# vim /etc/default/grub
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="$(sed 's, release .*$,,g' /etc/system-release)"
GRUB_DEFAULT=saved
GRUB_DISABLE_SUBMENU=true
GRUB_TERMINAL_OUTPUT="console"
GRUB_CMDLINE_LINUX="crashkernel=auto resume=/dev/mapper/rhel-swap rd.lvm.lv=rhel/root rd.lvm.lv=rhel/swap net.ifnames=0 biosdevname=0 rhgb quiet"
GRUB_DISABLE_RECOVERY="true"
GRUB_ENABLE_BLSCFG=true
~
为grub2生成其配置文件
[root@localhost network-scripts]# grub2-mkconfig -o /etc/grub2.cfg
Generating grub configuration file ...
Found linux image: /boot/vmlinuz-3.10.0-862.el7.x86_64
Found initrd image: /boot/initramfs-3.10.0-862.el7.x86_64.img
Found linux image: /boot/vmlinuz-0-rescue-a8e1d9190c704250b222880ee09ff34a
Found initrd image: /boot/initramfs-0-rescue-a8e1d9190c704250b222880ee09ff34a.img
done
重启系统
[root@localhost ~]# reboot
连接断开
网络管理常用命令
ifconfig
查看当前处于活动状态的所有网络接口
[root@localhost yum.repos.d]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.72.128 netmask 255.255.255.0 broadcast 192.168.72.255
inet6 fe80::b08f:bae9:6053:b35b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:c5:83:57 txqueuelen 1000 (Ethernet)
RX packets 21537 bytes 25164903 (23.9 MiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 10645 bytes 1026509 (1002.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 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
仅查看eth0网卡状态
[root@localhost network-scripts]# ifconfig eth0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.72.128 netmask 255.255.255.0 broadcast 192.168.72.255
inet6 fe80::b08f:bae9:6053:b35b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:c5:83:57 txqueuelen 1000 (Ethernet)
RX packets 5531 bytes 511537 (499.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 7288 bytes 916789 (895.3 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
查看所有网卡状态信息, 包括禁用和启用
[root@localhost ~]# ifconfig -a
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.72.128 netmask 255.255.255.0 broadcast 192.168.72.255
inet6 fe80::b08f:bae9:6053:b35b prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:c5:83:57 txqueuelen 1000 (Ethernet)
RX packets 6962 bytes 641700 (626.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 9261 bytes 1147273 (1.0 MiB)
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 1000 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
ip
link | 网络接口属性 |
---|
addr | 协议地址 | route | 路由 |
查看网络接口所有地址
[root@localhost ~]# ip link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:c5:83:57 brd ff:ff:ff:ff:ff:ff
显示报文统计信息
[root@localhost ~]# ip -s link show
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
RX: bytes packets errors dropped overrun mcast
0 0 0 0 0 0
TX: bytes packets errors dropped carrier collsns
0 0 0 0 0 0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:c5:83:57 brd ff:ff:ff:ff:ff:ff
RX: bytes packets errors dropped overrun mcast
1029496 11223 0 0 0 0
TX: bytes packets errors dropped carrier collsns
1836685 15127 0 0 0 0
启用或禁用网络接口ip link set DEV {up|down}
[root@localhost ~]# ip link set lo down
[root@localhost ~]# ip link set lo up
ip addr add ADDRESS dev DEV:添加IP地址
[root@localhost ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:c5:83:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.72.128/24 brd 192.168.72.255 scope global noprefixroute dynamic ens33
valid_lft 1255sec preferred_lft 1255sec
inet6 fe80::b08f:bae9:6053:b35b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]# ip addr add 192.168.1.1/24 dev eth0
[root@localhost ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:c5:83:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.72.128/24 brd 192.168.72.255 scope global noprefixroute dynamic ens33
valid_lft 1077sec preferred_lft 1077sec
inet 192.168.1.1/24 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::b08f:bae9:6053:b35b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
ip addr del ADDRESS dev DEV:删除IP地址
[root@localhost ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:c5:83:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.72.128/24 brd 192.168.72.255 scope global noprefixroute dynamic eth0
valid_lft 1697sec preferred_lft 1697sec
inet 192.168.1.1/24 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::b08f:bae9:6053:b35b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
[root@localhost ~]# ip addr del 192.168.1.1/24 dev eth0
[root@localhost ~]# ip addr show ens33
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:c5:83:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.72.128/24 brd 192.168.72.255 scope global noprefixroute dynamic eth0
valid_lft 1658sec preferred_lft 1658sec
inet6 fe80::b08f:bae9:6053:b35b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
ip addr show DEV:查看网络接口的地址
[root@localhost ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:c5:83:57 brd ff:ff:ff:ff:ff:ff
inet 192.168.72.128/24 brd 192.168.72.255 scope global noprefixroute dynamic ens33
valid_lft 1462sec preferred_lft 1462sec
inet6 fe80::b08f:bae9:6053:b35b/64 scope link noprefixroute
valid_lft forever preferred_lft forever
ss
ss是一种网络状态查看工具,取代netstat
-t | tcp协议相关 |
---|
-u | udp协议相关 | -w | 裸套接字相关 | -x | unix套接字相关 | -l | listen状态的连接 | -a | 所有 | -n | 数字格式 | -p | 相关的程序及pid | -e | 扩展的信息 | -m | 内存用量 | -o | 显示计时器信息 |
常用组合:
[root@localhost ~]# ss -antlp
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:* users:(("sshd",pid=1078,fd=3))
LISTEN 0 100 127.0.0.1:25 *:* users:(("master",pid=1365,fd=13))
LISTEN 0 128 :::22 :::* users:(("sshd",pid=1078,fd=4))
LISTEN 0 100 ::1:25 :::* users:(("master",pid=1365,fd=14))
[root@localhost ~]# ss -tan
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
ESTAB 0 36 192.168.72.128:22 192.168.72.1:54438
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@localhost ~]# ss -tanl
State Recv-Q Send-Q Local Address:Port Peer Address:Port
LISTEN 0 128 *:22 *:*
LISTEN 0 100 127.0.0.1:25 *:*
LISTEN 0 128 :::22 :::*
LISTEN 0 100 ::1:25 :::*
[root@localhost ~]# ss -anu
State Recv-Q Send-Q Local Address:Port Peer Address:Port
UNCONN 0 0 127.0.0.1:323 *:*
UNCONN 0 0 *:68 *:*
UNCONN 0 0 ::1:323 :::*
常见端口:
http | 80/tcp |
---|
https | 443/tcp | ssh | 22/tcp | ftp | 20,21/tcp | mysql | 3306/tcp | rsync | 873/rsync | redis | 6379/tcp |
网卡配置文件详解
TYPE=Ethernet #网络类型 以太网
BOOTPROTO=static
#1.这里可以为=none(过指定方式的办法来获得地址,如果没有指定的话可能会出现各种各样的网络受限 )
#2.=dhcp 自动获取ip地址
#3.=static 固定ip地址
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0 #网卡名称(用户看到的网卡名)
UUID=858e2bc3-1b47-4660-9297-b0029c8cac56 #网卡的唯一标识符
DEVICE=eth0 #系统逻辑设备名
ONBOOT=yes #是否开机自启网卡 选项(no/yes)centos7默认no
IPADDR=192.168.1.3 #指定的ip地址 如果你是dhcp这里是没有的
NETMASK=255.255.255.0 #子网掩码
GATEWAY=192.168.1.2 #网关
DNS1=114.114.114.114 #指定的第一个DNS服务器
硬盘的命名、分区、格式化、临时挂载与永久挂载
硬盘的命名
Linux中磁盘的命名方式与磁盘的接口有关,规则如下:
传统IDE接口硬盘 | /dev/hd[a-z] |
---|
SCISI接口硬盘 | /dev/sd[a-z] | 虚拟化硬盘 | /dev/vd[a-z] |
在设备名称的定义规则如下, 其他的分区可以以此类推 系统的第一块SCSI接口的硬盘名称为/dev/sda 系统的第二块SCSI接口的硬盘名称为/dev/sdb 系统中分区由数字编号表示, 1-4留给主分区使用和扩展分区, 逻辑分区从5开始
硬盘的分区
目的:为了方便管理
分区工具有fdisk和gdisk,当硬盘小于2T的时候我们应该用fdisk来分区,而当硬盘大于2T的时候则应用gdisk来进行分区
MBR分区
生产分区建议: 如无特殊需求, 直接使用整个磁盘即可, 无需分区 学习分区建议:1P+1E(nL) 2P+1E(nL) 3P+1E(nL)
创建分区 1P+1E(1L)
[root@localhost ~]# fdisk /dev/sdb
欢迎使用 fdisk (util-linux 2.23.2)。
更改将停留在内存中,直到您决定将更改写入磁盘。
使用写入命令前请三思。
Device does not contain a recognized partition table
使用磁盘标识符 0x452d7799 创建新的 DOS 磁盘标签。
命令(输入 m 获取帮助):n
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): p
分区号 (1-4,默认 1):
起始 扇区 (2048-41943039,默认为 2048):
将使用默认值 2048
Last 扇区, +扇区 or +size{K,M,G} (2048-41943039,默认为 41943039):+10G
分区 1 已设置为 Linux 类型,大小设为 10 GiB
命令(输入 m 获取帮助):n
Partition type:
p primary (1 primary, 0 extended, 3 free)
e extended
Select (default p): e
分区号 (2-4,默认 2):
起始 扇区 (20973568-41943039,默认为 20973568):
将使用默认值 20973568
Last 扇区, +扇区 or +size{K,M,G} (20973568-41943039,默认为 41943039):
将使用默认值 41943039
分区 2 已设置为 Extended 类型,大小设为 10 GiB
命令(输入 m 获取帮助):n
Partition type:
p primary (1 primary, 1 extended, 2 free)
l logical (numbered from 5)
Select (default p): l
添加逻辑分区 5
起始 扇区 (20975616-41943039,默认为 20975616):
将使用默认值 20975616
Last 扇区, +扇区 or +size{K,M,G} (20975616-41943039,默认为 41943039):+1G
分区 5 已设置为 Linux 类型,大小设为 1 GiB
命令(输入 m 获取帮助):p
磁盘 /dev/sdb:21.5 GB, 21474836480 字节,41943040 个扇区
Units = 扇区 of 1 * 512 = 512 bytes
扇区大小(逻辑/物理):512 字节 / 512 字节
I/O 大小(最小/最佳):512 字节 / 512 字节
磁盘标签类型:dos
磁盘标识符:0x452d7799
设备 Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
/dev/sdb2 20973568 41943039 10484736 5 Extended
/dev/sdb5 20975616 23072767 1048576 83 Linux
命令(输入 m 获取帮助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盘。
[root@localhost ~]# partprobe
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开。
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 3.9G 0 lvm [SWAP]
└─centos-home 253:2 0 45.1G 0 lvm /home
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 10G 0 part
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 1G 0 part
sr0 11:0 1 4.2G 0 rom
格式化
目的:为了创建文件系统mkfs(MaKe FileSystem)
文件系统 | 特点 |
---|
fat32 | 单个文件不能超过4G | ntfs | 单个文件可以超过4G、兼容性差 | exfat | 单个文件可以超过4G、兼容性浩 |
[root@localhost ~]# mkfs.ext3 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
文件系统标签=
OS type: Linux
块大小=4096 (log=2)
分块大小=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
655360 inodes, 2621440 blocks
131072 blocks (5.00%) reserved for the super user
第一个数据块=0
Maximum filesystem blocks=2684354560
80 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
Allocating group tables: 完成
正在写入inode表: 完成
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
[root@localhost ~]# mkfs.xfs /dev/sdb5
meta-data=/dev/sdb5 isize=512 agcount=4, agsize=65536 blks
= sectsz=512 attr=2, projid32bit=1
= crc=1 finobt=0, sparse=0
data = bsize=4096 blocks=262144, 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
磁盘挂载
临时挂载
mount -t 指定文件系统挂载分区,如ext3,ext4, xfs
[root@localhost ~]# mount -t xfs /dev/sdb5 /
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 3.9G 0 lvm [SWAP]
└─centos-home 253:2 0 45.1G 0 lvm /home
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 10G 0 part
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 1G 0 part /
sr0 11:0 1 4.2G 0 rom
mount -o 指定挂载参数
[root@localhost ~]# umount /dev/sdb5
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 3.9G 0 lvm [SWAP]
└─centos-home 253:2 0 45.1G 0 lvm /home
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 10G 0 part
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 1G 0 part
sr0 11:0 1 4.2G 0 rom
[root@localhost ~]# mount -t xfs /dev/sdb5 /
[root@localhost ~]# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 100G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 99G 0 part
├─centos-root 253:0 0 50G 0 lvm /
├─centos-swap 253:1 0 3.9G 0 lvm [SWAP]
└─centos-home 253:2 0 45.1G 0 lvm /home
sdb 8:16 0 20G 0 disk
├─sdb1 8:17 0 10G 0 part
├─sdb2 8:18 0 1K 0 part
└─sdb5 8:21 0 1G 0 part /
sr0 11:0 1 4.2G 0 rom
mount -a
永久挂载
使用blkid命令获取各分区的UUID
[root@localhost ~]# blkid /dev/sdb1
/dev/sdb1: UUID="36543134-40d9-45a5-a17f-8e63b68ca8fb" SEC_TYPE="ext2" TYPE="ext3"
把uuid这行写到/etc/fstab中,永久挂载, 开机自动挂载
[root@localhost ~]# vim /etc/fstab
/dev/mapper/centos-root / xfs defaults 0 0
UUID=fb7987ce-06ed-40de-a0b8-bb5c25e1feed /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
/dev/mapper/centos-swap swap swap defaults 0 0
UUID="160d1a55-f6eb-46c4-9b19-50238b6b2126" /root xfs defaults 0 0
|