音乐播放器
Dustin Blog
 
文章 标签
9

Powered by Dustin | Theme: Fog

Centos 服务器的搭建

👏 欢迎使用Centos,本文以X86硬件架构为例,请访问官方镜像站下载!

👇 Centos 系统安装

  • 通过上述下载的镜像,使用 rufus 制作 U 盘启动盘,并记录 U 盘盘符,选择 Troubleshooting,添加 grub 启动参数,否则无输出!
LABEL=U盘盘符 console=ttyS0,115200n8

👇 调整默认 LVM 分区

如果你并不是使用这个分区方式,请无视此部分操作。

  • 如果已经挂载,先卸载 /home 目录。
sudo umount /home
  • 删除逻辑卷 centos-home
sudo lvremove /dev/mapper/centos-home
  • 调整 centos-swap 逻辑卷的大小为 8GB
sudo lvresize -L 8G /dev/mapper/centos-swap
  • 扩展 centos-root 逻辑卷以包含剩余空间。
sudo lvextend -l +100%FREE /dev/mapper/centos-root
  • 如果 centos-root 包含文件系统,需要调整文件系统大小。
sudo resize2fs /dev/mapper/centos-root
  • 如果是文件系统是 xfs 格式。
xfs_growfs /dev/mapper/centos-root
  • 缩小分区操作。
lvreduce -L -10G /dev/mapper/cl-root
  • 确认逻辑卷大小调整和文件系统扩展是否成功。
df -h
  • 在 /etc/fstab 中设置自动挂载。
vi /etc/fstab
######文件修改内容如下######
UUID=bd0382af-caa1-4446-9215-085ad3fe4241 /home ext4  defaults  0 0
######文件修改内容结束######

👇 驱动的更新替换

因为我使用的硬件差异,网卡驱动方面需要额外编译二次开发的新驱动替换默认镜像的驱动。

  • 安装 gcc 编译环境,到 gcc 文件夹路径下,rpm 安装编译环境。
uname -r
mount /dev/sdb1 /mnt
cd /mnt/gcc-3.10.0-1160/
rpm -ivh * --nodeps --force
  • ice 驱动,进入 src 目录下,进行编译安装。
make
make install
  • ixgbe 驱动,进入 src 目录下,进行编译安装。
make
make install
  • igc 驱动,i225、i226 网卡驱动,进入 igc 目录下,进行编译安装。
./autorun.sh

👇 安装 Libevent 环境

  • 我有工程依赖于 libevent 环境,所以需要安装它,并更新动态链接库。
tar -zxvf libevent-2.1.11-stable.tar.gz
cd libevent-2.1.11-stable
./configure
make
make install
ldconfig

👇 网口命名方式调整

  • 调整网络接口重新命名回传统的 ethX 格式,可以通过编辑 /etc/default/grub 文件,修改为 net.ifnames=0
GRUB_CMDLINE_LINUX="crashkernel=auto rd.lvm.lv=centos/root rd.lvm.lv=centos/swap console=ttyS0,115200n8 net.ifnames=0 biosdevname=0"

👇 系统源失败问题

  • 添加 nameserver,解决网络问题。
vi /etc/resolv.conf
######文件修改内容如下######
nameserver 8.8.8.8
nameserver 8.8.8.4
nameserver 8.8.4.4
######文件修改内容结束######
  • 删除本地源,下载阿里云配置,解决源问题。
rm  /etc/yum.repos.d/*
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum install git ssh

👇 生成 GRUB 配置

  • 重新生成配置,并更新内核参数。
grub2-mkconfig -o /boot/grub2/grub.cfg

👇 创建公钥私钥

  • 文件位于 /root/.ssh/,其中 id_rsa 为私钥,id_rsa.pub 为公钥,接下来打开 id_rsa.pub,替换至原本的。修改私钥文件的权限,确保只有所有者可以读写,而其他用户没有权限。
ssh-keygen -t rsa
chmod 600 /root/.ssh/id_rsa

👇 安装常用工具

  • 安装一些常用的工具。
yum install net-tools
yum install git
yum install elfutils-libelf-devel
yum install nfs-utils
yum install xorriso
yum install centos-release-scl
yum install python3
yum install python3-pip
pip3 install requests
yum install devtoolset-8-gcc
yum install bash-completion
  • 修改文件 ~/.bashrc,默认使用高版本 gcc,以及增加 git 命令联想,Tab 自动补全。
vi ~/.bashrc
######文件修改内容如下######
source scl_source enable devtoolset-8
source /usr/share/doc/git-*/contrib/completion/git-completion.bash
######文件修改内容结束######

👇 同步远程目录

  • 通过 rsync 将远程文件夹同步到本地文件夹。
rsync -avzh root@192.168.139.100:/data/huangyj /data/

👇 设置文件夹权限

  • 设置文件夹内只有 root 用户有读写权限,其他人是读权限。
sudo useradd newuser
sudo passwd newuser
sudo chown root:root /path/to/folder
sudo chmod 755 /path/to/folder

👇 搭建 NFS 服务

  • 安装 NFS 服务器软件包。
sudo yum install nfs-utils
  • 配置 NFS 服务器。
sudo vi /etc/exports
######文件修改内容如下######
/data/huangyj/centos-7_2009 *(rw,sync,no_root_squash,no_subtree_check)
/data/huangyj/centos-7_2009-hpc *(rw,sync,no_root_squash,no_subtree_check)
/data/huangyj/centos-8 *(rw,sync,no_root_squash,no_subtree_check)
/data/huangyj/centos-8-hpc *(rw,sync,no_root_squash,no_subtree_check)
######文件修改内容结束######
  • 启动并设置 NFS 服务器开机自启动。
sudo systemctl start nfs-server
sudo systemctl enable nfs-server
  • 如果系统使用防火墙,确保允许 NFS 服务通过防火墙。
sudo firewall-cmd --permanent --zone=public --add-service=nfs
sudo firewall-cmd --reload

👇 搭建 FTP 服务

  • 安装 FTP 服务器软件包。
sudo yum install vsftpd
  • 配置 FTP 服务器。
sudo vi /etc/vsftpd/vsftpd.conf
######文件修改内容如下######
anon_root=/data/chfsfile/chfsfile
anonymous_enable=YES
allow_writeable_chroot=YES
anon_other_write_enable=YES
######文件修改内容结束######
  • 启动并设置 FTP 服务器开机自启动。
sudo systemctl start vsftpd
sudo systemctl enable vsftpd
  • 如果系统使用防火墙,确保允许 FTP 服务通过防火墙。
sudo firewall-cmd --permanent --add-service=ftp
sudo firewall-cmd --reload

👇 搭建 TFTP 服务

  • 安装 TFTP 服务器软件包。
sudo yum install xinetd tftp-server
  • 配置 TFTP 服务器。
sudo vi /etc/xinetd.d/tftp
######文件修改内容如下######
service tftp
{
        socket_type             = dgram
        protocol                = udp
        wait                    = yes
        user                    = root
        server                  = /usr/sbin/in.tftpd
        server_args             = -s /data/chfsfile/chfsfile
        disable                 = no
        per_source              = 11
        cps                     = 100 2
        flags                   = IPv4
}
######文件修改内容结束######
  • 启动并设置 TFTP 服务器开机自启动。
sudo systemctl start xinetd.service
sudo systemctl enable xinetd.service
  • 如果 systemctl 启动服务的时候报错 Error:no space left on device
sudo vi /etc/sysctl.conf
######文件修改内容如下######
fs.inotify.max_user_watches = 262144
######文件修改内容结束######
  • 使配置生效。
sysctl -p
  • 如果系统使用防火墙,确保允许 TFTP 服务通过防火墙。
sudo firewall-cmd --permanent --add-service=tftp
sudo firewall-cmd --reload

👇 安装宝塔面板

rm -f /www/server/panel/data/admin_path.pl
sudo systemctl enable bt

👇 内网穿透服务

  • 使用cpolar实现免费内网穿透。
cpolar authtoken NmJmMTgwMzYtOTE5ZS00YmE0LWE1MDctYjUxYTQ3MDkyZGY2
# Authtoken saved to configuration file: /usr/local/etc/cpolar/cpolar.yml
authtoken: NmJmMTgwMzYtOTE5ZS00YmE0LWE1MDctYjUxYTQ3MDkyZGY2
tunnels:
  site:
    proto: http
    addr: "88"
    region: cn_vip_top
  bt:
    proto: tcp
    addr: "8888"
    region: cn_vip_top

👇 设置开机自启动

  • 可以通过修改 rc.local,添加一些私有化自启动服务。
sudo vi /etc/rc.local

######文件修改内容如下######
systemctl disable firewalld                                                 # 永久禁用防火墙服
chfs-linux-amd64-3.1 --file=/data/chfsfile/chfs.ini &     # 开启文件共享服务器
######文件修改内容结束######

👇 安装 Docker 容器

  • 删除 Docker 软件包。
sudo yum remove docker-ce docker-ce-cli containerd.io
  • 安装 yum-utils 是为方便添加 yum 源使用的,device-mapper-persistent-data 和 lvm2 是储存设备映射 (devicemapper) 必须的两个软件包。
sudo yum install -y yum-utils lvm2 device-mapper-persistent-data
  • 由于默认是国外的 yum 源,基本不可用,我们这边修改为阿里源或清华源。
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
  • 更新下修改的软件源。
sudo yum makecache fast
  • 默认版本是 ce 的稳定版本。
sudo yum -y install docker-ce
  • 安装完成后执行如下命令检测安装是否成功。
docker version
sudo mkdir -p /etc/docker
sudo tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://e7glsap4.mirror.aliyuncs.com"]
}
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker
  • 设置 Docker 自启动。
sudo systemctl start docker
sudo systemctl enable docker
  • 测试搜索并拉取更新镜像。
docker search hello-world
docker pull hello-world
  • 如果搜索并拉取更新镜像失败,可以查看服务器 DNS 网络配置。
vi /etc/resolv.conf
######文件修改内容如下######
nameserver 8.8.8.8
nameserver 8.8.8.4
nameserver 8.8.4.4
######文件修改内容结束######

👇 安装 Nascab 系统

  • Docker安装Nascab系统,实现私有NAS!
docker search nascab
docker pull ypptec/nascab
docker run -v /data/nascab/myData:/myData -v /data/nascab/nascabData:/root/.local/share/nascab -p 8080:80 -p 8090:90 -p 8443:443 -p 8021:21 -d --name nascab --privileged=true --log-opt max-size=10m --log-opt max-file=3 ypptec/nascab
  • 填坑:文件内权限不足会导致启动 Docker 失败。
docker exec -it nascab /bin/sh    # 进入docker内部
docker images
docker rmi cf6ea82ce974
docker ps -a
docker rm nascab
docker logs nascab
netstat -tuln
  • 设置 Docker 某容器开机自启动。
docker update --restart=always 容器ID(或者容器名)

😘 Thank you for browsing,Enjoy~