# Tiny Core Linux 安装配置
Date: 2020-09-01


## 简介

`Tiny Core Linux`是一个`mini Linux`操作系统,基于 `BusyBox`和`FLTK`提供基本功能。 其发行版本在`11M-16M` 官网上也提供`Plus`版本也只有`106M`。它很精致。

## 安装

### 操作系统安装

#### 镜像下载

下载页面 [http://tinycorelinux.net/downloads.html](http://tinycorelinux.net/downloads.html) 我下载了两个版本镜像:

- x86版本: `TinyCore-11.1.iso`
- x86\_64位版本:`TinyCorePure64-11.1.iso`

#### 环境准备

我们使用的`vmware` 首先新建一个`linux`虚拟机， 我选的版本是`其他Linux5.x或更高版本内核64位`, 并添加了两块`SATA`的硬盘（默认的SCSI硬盘识别似乎有些问题）

#### 开始安装

安装过程主要下面三个步骤 ：

- 光盘启动操作系统;
- 配置网络
- 下载安装脚本安装;

1 . **选择镜像**: 光盘镜像选择 x86版本:`TinyCore-11.1.iso` (不影响安装) 2 . **启动系统**: 启动后会出现四个选项, 选择第三项

```C
Boot TinyCore
Boot TinyCore (on slow devices, waitusb=5)
Boot Core (command line only)
Boot Core (command line only on slow devices, waitusb=5)
```

可以看到系统后进入命令行窗口，但是这种模式下进行操作重启将全部丢失。要保留我们相关配置,需要挂载硬盘。

3 . **配置网络**: 配置网卡地址路由及域名服务器。

```C
sudo ifconfig eth0 192.168.xxx.202 netmask 255.255.255.0
sudo route add default gw 192.168.xxx.1 dev eth0
sudo echo nameserver 192.168.xxx.1 >> /etc/resolv.conf
```

4 . **下载安装脚本**: 会联机下载相关安装包

```C
tce-load -wi tc-install.tcz
```

5 . **安装**:

- 启动安装脚本

```C
sudo tc-install.sh
```

- 选择`c`从光盘启动

```C
Core Installation.
Install from [R]unning OS, from booted [C]drom, from
 [I]so file, or from [N]et. (r/c/i/n): c
```

- 选择`f`安装到本地硬盘

```C
Select install type for /mnt/sr0/boot/core.gz

Frugal
* Use for frugal hard drive installations.
Note: You will be prompted for disk/partion and 
formatting options.

HDD
* Use for pendrives. Your BIOS must support USB-HDD booting.
* A single FAT partition will be made.
Note: Requires dosfstools extension.
Warning: This is a whole drive installation!

Zip
* Use for pendrives. Drive will be formatted into two FAT partitions.
* One small one for USB_ZIP boot compatibility, and used to hold Tiny Core.
* The remaining partition will be used for backup & extensions.
Note: Requires dosfstools and perl extensions.
Warning: This is a whole drive installation!

Select Install type [F]rugal, [H]DD, [Z]ip. (f/h/z): f
```

- 选择`1`使用整个硬盘安装

```C
Select Target for Installation of core

 1. Whole Disk
 2. Partition

Enter selection ( 1 - 2 ) or (q)uit: 1
```

- 选择`1`安装到第一块硬盘上

```C
Select disk for core

 1. sda
 2. sdb

Enter selection ( 1 - 2 ) or (q)uit: 1
```

- 安装`bootloader`选择`y`

```C
Would you like to install a bootloader?
Most people should answer yes unless they are trying to embed Core
into a different Linux distribution with an existing bootloader.

Enter selection ( y, n ) or (q)uit: y
Install Extensions from this TCE/CDE Directory: 
```

- 选择分区格式 `ext4`

```C
Select Formatting Option for sda

 1. ext2
 2. ext3
 3. ext4
 4. vfat

Enter selection ( 1 - 4 ) or (q)uit: 3
```

- 设置一些启动选项, 这里先不输入， 直接回车

```C
Enter space separated boot options: 
Example: vga=normal syslog showapps waitusb=5
```

- 选择`y`,开始安装

```C
Last chance to exit before destroying all data on sda
Continue (y/..)?
```

- 安装完毕后有以下信息回显, 安装完毕.

```C
Writing zero's to beginning of /dev/sda
Partitioning /dev/sda
Formatting /dev/sda1
mke2fs 1.45.4 (23-Sep-2019)
1+0 records in
1+0 records out
440 bytes (440B) copied, 0.000846 seconds, 507.9KB/s
UUID="XXXXXXX"
Applying syslinux.
/mnt/drive/tce/boot/extlinux is device /dev/sda1
Setting up core image on /mnt/sda1
Installation has completed
Press Enter key to continue.
```

按照这个步骤把第二块硬盘也安装一下. 第二块硬盘使用镜像 `TinyCorePure64-11.1.iso` 安装, 使用`umount`卸载原有`x86`镜像，`mount`重新挂载新的镜像。

```C
sudo umount /dev/sr0
...
sudo mount /dev/sr0
```

6 . **重启动**，进入配置阶段

## 配置

启动后默认进入`X86`版本系统, 下面开始配置`grub2`,`openssh`等，其中需要注意的是 `Tiny linux`相关设置如果不执行`filetool.sh`, 只是临时生效，重启系统就会丢失。

相关软件包安装需要先配置网络然后联网通过`tce`工具下载即可, 如果要长期使用需要做持久化处理。

### 网络配置

配置网络并支持永久生效

- 生成文件/opt/eth0.sh 文件内容:

```C
sudo ifconfig eth0 192.168.xxx.202 netmask 255.255.255.0
sudo route add default gw 192.168.xxx.1 dev eth0
sudo echo nameserver 192.168.xxx.1 >> /etc/resolv.conf
```

- 增加可执行权限

```C
chmod 755 /opt/eth0.sh
sudo /opt/eth0.sh
```

- 设置开机启动及永久生效

```C
sudo echo "/opt/eth0.sh" >> /opt/.filetool.lst
sudo chmod 775 /opt/bootlocal.sh
sudo echo "/opt/eth0.sh &" >> /opt/bootlocal.sh
filetool.sh -b
```

### 开机启动配置

- 联网后使用`tce`安装`grub2`

```C
tce-load -wi grub2-multi.tcz
sudo grub-install --target=i386-pc --boot-directory=/mnt/sda1/boot /dev/sda
```

- 生成 `grub.cfg`文件

```C
menuentry "x86" {
    set root=(hd0, 1)
    linux /tce/boot/vmlinuz tce=sda1
    initrd /tce/bot/core.gz
}

menuentry "x86_64" {
    set root=(hd1, 1)
    linux /tce/boot/vmlinuz64 tce=sdb1
    initrd /tce/bot/corepure64.gz
}
```

这部分修改由于本事就在硬盘上不需要做持久化处理, 重新启动就可以看到grub2启动界面

```
*x86
 x86_64
```

### openssh配置

- 如果需要远程登录可以安装`openssh`

```C
tce-load -wi openssh.tcz
```

- 相关配置文件生成

```C
cd /usr/local/etc/ssh
sudo cp sshd_config.orig sshd_config
```

- 设置密码

```C
passwd
```

- 持久化处理

```C
sudo echo '/usr/local/etc/ssh' >> /opt/.filetool.lst
sudo echo '/etc/shadow' >> /opt/.filetool.lst
filetool.sh -b
sudo echo '/usr/local/etc/init.d/openssh start &' >> /opt/bootlocal.sh
```

- 配置免密 生成SSH key, 使用 `ssh-keygen`或`PuTTY Key Generator`可以生成公私钥对, 公钥上传服务器`/home/tc/.ssh`目录命名为`authorized_keys`
    
- 相关问题 安装过程中报错 : server refused our key
    
    - 公钥格式： 如果使用 `putty key generator`界面上会提示要拷贝的公钥文件内容,需要改为 openssh 使用的公钥格式, 或者使用下面命令转换
    
    ```C
    ssh-keygen -i -f 公钥文件名
    ```
    
    - 权限调整:去掉组权限
    
    ```C
    chmod 700 ~/.ssh
    chmod 600 ~/.ssh/authorized_keys
    ```
    
- 永久保存处理
    

```C
sudo echo '/home/tc/.ssh/' >> /opt/.filetool.lst
filetool.sh -b
```

这样就可以通过ssh远程免密登录了

### 新增硬盘

- 安装`gdisk`

```C
tce-load -wi gdisk.tcz
```

- 使用`gdisk` 创建两个`500M`分区, 重启生效

```C
brw-rw---- 1 root     staff       8,  33 Sep  2 04:51 /dev/sdc1
brw-rw---- 1 root     staff       8,  34 Sep  2 04:51 /dev/sdc2
```

- 格式化

```C
mkfs.ext2 /dev/sdc1
mkfs.ext3 /dev/sdc2
```

- 修改`/etc/fstab`

```C
/etc/fstab
/dev/sdc1       /mnt/sdc1       ext2     noauto,users,exec    0 0 # Added by TC
/dev/sdc2       /mnt/sdc2       ext3     noauto,users,exec    0 0 # Added by TC
```

- 修改`bootlocal.sh`启动后自动挂载

```C
echo "sudo mount /dev/sdc1 &" >> /opt/bootlocal.sh
echo "sudo mount /dev/sdc2 &" >> /opt/bootlocal.sh
filetool.sh -b
sudo reboot
...

$ mount
rootfs on / type rootfs (rw,size=633520k,nr_inodes=223783)
...
/dev/sdc1 on /mnt/sdc1 type ext2 (rw,relatime)
/dev/sdc2 on /mnt/sdc2 type ext3 (rw,relatime)
```

## 参考

[Assign Persistent Static IP Address to Tiny Core Linux](https://iotbytes.wordpress.com/assign-persistent-static-ip-address-to-microcore-tiny-linux/) [Install Tiny Core Linux on Local Disk (Frugal)](https://iotbytes.wordpress.com/install-microcore-tiny-linux-on-local-disk/) [Configure SSH Server on Tiny Core Linux using openSSH](https://iotbytes.wordpress.com/configure-ssh-server-on-microcore-tiny-linux/) [Trying to do ssh authentication with key files: server refused our key](https://askubuntu.com/questions/306798/trying-to-do-ssh-authentication-with-key-files-server-refused-our-key#comment1049569_662066)

