树莓派安装Centos7

吐槽、建议、解惑入口网址

树莓派安装Centos7

树莓派Centos选择下载

简单描述下Centos分为release发版和stream不稳定发版。release迭代较慢,安全。stream迭代较慢,不稳定。

Centos7 下载

目前笔者购买是关于树莓派4B-4G,选择安装包为University CentOS-Userland-7-armv7hl-RaspberryPI-Minimal-4-2003-sda .raw.xz
然后下载Centos包使用对应工具[Balena Etcher]烧录到SD卡上。

树莓派Centos连接

  • 先将树莓派连接网线,并启动
  • 连接的电脑也需要连接WIFI网络上(让电脑和树莓派保持在同一个局域网内)
  • 使用IP Scanner软件进行扫描 ,找到新增ip
    树莓派IPScanner
  • ssh root@xxx.xxx.xxx.xxx 默认密码centos

Centos7 拓展主分区

由于系统默认的映像把根分区的空间设定得很小,因此我们需要手动扩展根分区。 详情参考为树莓派装上 CentOS 7 系统

首次连接树莓派,会发现主分区没有可用的空间。
存储卡硬盘 /dev/mmcblk0 分成3个分区(mmcblk0p1/mmcblk0p2/mmcblk0p3),mmcblk0p3的主分区没有被挂载上。

执行步骤

  • 执行 rootfs-expand
  • 检查硬盘分区拓展情况 df -lh

没有效果则只能将mmcblk0重新进行分区,然后重启

核心操作就是将第三个主分区删除,重新分区。

  • 执行分区命令 fdisk /dev/mmcblk0
  • 删除第三个主分区回车 d
  • 新增第三个主分区回车 n
  • 选择新增第三个主分区回车 p
  • 开始设置分区开始扇区和结束扇区,需要注意设定的开始扇区和结尾扇区,从第二块主分区结尾扇区 + 1开始。不然默认是使用第一块主分区前面的扇区,存储空间很小。
  • 将树莓派重新启动,reboot
  • 启动后,直接再次执行 rootfs-expand或者resize2fs /dev/mmcblk0p3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
设备 Boot      Start         End      Blocks   Id  System
/dev/mmcblk0p1 * 8192 593919 292864 c W95 FAT32 (LBA)
/dev/mmcblk0p2 593920 1593343 499712 82 Linux swap / Solaris
/dev/mmcblk0p3 1593344 31116287 14761472 83 Linux
### 执行前效果
[root@localhost ~]# df -lh
文件系统 容量 已用 可用 已用% 挂载点
/dev/root 1.7G 1.7G 0 100% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.4M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mmcblk0p1 286M 53M 233M 19% /boot
tmpfs 383M 0 383M 0% /run/user/0

[root@localhost ~]# ll /dev | grep mmcb
brw-rw----. 1 root disk 179, 0 1月 1 00:00 mmcblk0
brw-rw----. 1 root disk 179, 1 1月 1 00:00 mmcblk0p1
brw-rw----. 1 root disk 179, 2 1月 1 00:00 mmcblk0p2
brw-rw----. 1 root disk 179, 3 1月 1 00:04 mmcblk0p3

### 执行后效果
[root@localhost ~]# df -lh
文件系统 容量 已用 可用 已用% 挂载点
/dev/root 14G 1.7G 13G 13% /
devtmpfs 1.9G 0 1.9G 0% /dev
tmpfs 1.9G 0 1.9G 0% /dev/shm
tmpfs 1.9G 8.4M 1.9G 1% /run
tmpfs 1.9G 0 1.9G 0% /sys/fs/cgroup
/dev/mmcblk0p1 286M 53M 233M 19% /boot
tmpfs 383M 0 383M 0% /run/user/0
主机名、网络、时区、语言、键盘配置

主机名配置

hostnamectl status
hostnamectl set-hostname xxxx主机名名称


网络配置

nmtui
nmcli d
nmcli d wifi
nmcli d wifi connect xxxWiFi名称 password 'xxxWiFi密码'
cat /etc/resolv.conf
网络连接文件夹: cd /etc/sysconfig/network-scripts cat ifcfg-网络名称
设置网络连接开机自启动 systemctl enable NetworkManager.service


语言配置

localectl
localectl set-locale LANG=zh_CN.UTF-8
locale
update-locale LANG=LANG=zh_CN.UTF-8 LANGUAGE
cat /etc/locale.conf


时区配置

date -R

  • 方案一:
    tzselect
    按照步骤使用命令tzselect选择国家时区
    执行TZ=’Asia/Shanghai’; export TZ 并将这行命令添加到.bash_profile中,然后退出并重新登录。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
You can make this change permanent for yourself by appending the line

to the file '.profile' in your home directory; then log out and log in again.

Here is that TZ value again, this time on standard output so that you
can use the /usr/bin/tzselect command in shell scripts:
Asia/Shanghai
```

* 方案二:
`timedatectl`
直接设置中国-上海时区 `timedatectl set-timezone Asia/Shanghai`


----------------------------
###### 键盘配置
`localectl set-keymap us`

##### 高级配置
###### 切换软件源
> [详情切花软件源网址](https://www.jianshu.com/p/541c737bc947)

* 将软件源替换为阿里源
* 先进入源的目录
`cd /etc/yum.repos.d`
* 备份一下官方源
`mv CentOS-Base.repo CentOS-Base.repo.bak`
* 将阿里源文件下载下来
`wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo`
* 重建源数据缓存
`yum clean all`
`yum makecache`

###### 设置域名解析地址
> [国内常用公共DNS服务器地址有哪些?](https://zhuanlan.zhihu.com/p/39406412)

[root@localhost ~]# cat /etc/resolv.conf

Generated by NetworkManager

nameserver 114.114.114.114
nameserver 8.8.8.8



###### 更改语言包

##### 其他
私有内网的linux获取出口公网地址命令  

`curl ifconfig.me`    
`curl ipinfo.io`

#### 挂载移动硬盘
* 只需要在修改/etc/fstab文件后执行该命令`mount -a` ,即可在不重启的情况下挂载fstab!
* `blkid` 打印块设备属性
* `lsblk` 块设备列表
* `mkfs`  格式化硬盘分区 ext4主流centos7文件系统
* `/etc/fstab` 设置`default,nofail`可以让硬盘不存在启动

[/etc/fstab](http://einverne.github.io/post/2019/02/fstab-file.html)


##### 学习
[SSH 安全加固](https://zzz.buzz/zh/2016/04/18/hardening-sshd/)   
[centos 时区设置](https://www.jianshu.com/p/adefed4862a2)   
[centos 时钟同步](https://cloud.tencent.com/developer/article/1444268)    
[Linux系统扩展硬盘空间](https://www.jianshu.com/p/ba7090b1ef38)
[Linux Kickstart 自动安装讲述anaconda-ks.cfg文件](http://liaoph.com/linux-kickstart/)
[神技能 | 手把手教您把CentOS7服务器变成上网路由器](https://cloud.tencent.com/developer/article/1115169)

#### 参考资料
[树莓派4B 安装CentOS](https://www.cnblogs.com/Evan-fanfan/p/11520170.html)   
[树莓派安装centos系统,并连接WiFi](https://blog.csdn.net/liuzhe465/article/details/76691345)     
[为树莓派装上 CentOS 7 系统](https://zhuanlan.zhihu.com/p/33030757)   
[SpecialInterestGroupAltArcharmhfp](https://wiki.centos.org/SpecialInterestGroup/AltArch/armhfp)     
不曾拥有,所以努力。(坚持原创技术分享,您的支持将鼓励我继续创作!)