服务器实训

Linux 服务与配置用到的操作步骤
Mysql 操作基于 Ubuntu 20.04, 其他操作基于 CentOS 7

实验配置

启用 Firewalld 服务

systemctl start firewalld

关闭 SElinux

getenforce 查看是否为 Disable

配置软件源

配置 Centos 7 本地源:

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#! /bin/bash

path="/etc/yum.repos.d"
if [! -e "$path"]; then
mkdir "$path"
else
rm -rf "$path"
mkdir "$path"
fi

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
yum remove epel-release -y && yum install epel-release -y;
yum clean all && yum makecache;

iso="/mnt/iso"
if [! -e "$iso"]; then
mkdir $iso
fi
mount /dev/cdrom /mnt/iso;
dvd="/etc/yum.repos.d/dvd.repo"
if [-e "$dvd"]; then
rm $dvd
else
touch $dvd
fi
cat <<EOF>> "$dvd";
[dvd]
name=dvd
baseurl=file:///mnt/cd0
gpgcheck=0
enabled=1
priority=1
EOF

yum -y install yum-priorities;
path="/etc/yum.repos.d/CentOS-Base.repo"
if grep -q "priority=2" "$path"; then
echo "已包含"
else
sed -i '/7/a\priority=2' "$path";
fi
path="/etc/yum.repos.d/epel.repo"
if grep -q "priority=11" "$path"; then
echo "已包含"
else
sed -i '11i\priority=11' "$path";
fi
yum clean all && yum makecache;
yum install httpd -y;
yum repolist;

软件源地址:

1
2
3
4
5
清华大学软件源: https://mirrors.tuna.tsinghua.edu.cn/help
中科大软件源: https://mirrors.ustc.edu.cn/help
华为云软件源: https://mirrors.huaweicloud.com/home
阿里云软件源: https://developer.aliyun.com/mirror/
上海交大软件源: https://mirrors.sjtug.sjtu.edu.cn/docs/ubuntu

我使用的是中科大软件源:

CentOS 7:

  • 使用 Vim(没有就用 Vi) 打开 /etc/yum.repos.d/CentOS-Base.repo

  • 把下面的软件源输入进去

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
[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
baseurl=https://mirrors.ustc.edu.cn/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
baseurl=https://mirrors.ustc.edu.cn/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
baseurl=https://mirrors.ustc.edu.cn/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
baseurl=https://mirrors.ustc.edu.cn/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  • 使用 sudo yum update 更新

Ubuntu 20.04:

  • 使用 vi 打开 /etc/apt/sources.list

  • 输入软件源:

1
2
3
4
5
# 默认注释了源码仓库,如有需要可自行取消注释
deb https://mirrors.ustc.edu.cn/ubuntu/ focal main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-security main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-updates main restricted universe multiverse
deb https://mirrors.ustc.edu.cn/ubuntu/ focal-backports main restricted universe multiverse
  • 使用 sudo apt update 更新

使用 RDP 方式连接 Linux 服务器

开通防火墙的 3389 端口,即可

1
2
firewall-cmd --permanent --add-port=3389/tcp
firewall-cmd --reload # 使上面的配置生效

制作 DHCP 服务器

本实验需要用到云计算机 A、B 和 C, 其中 A、B 装有 centos 7, C 装有 windows xp
使用 A、B 的 ens33 网卡,C 使用本地连接 2

DHCP 服务安装和配置

在云计算机 A 中操作

安装

使用 sudo yum install dhcp -y 安装,使用 dhcpd 检查是否成功安装 dhcp

配置

1
2
3
4
5
6
7
8
9
10
11
12
13
cat<<EOF> /etc/dhcp/dhcpd.conf
ddns-update-style none; #配置 DHCP-DNS 为不自动更新模式
log-facility local7; #local7 以上级别的日志发送到 dhcp 日志
default-lease-time 600; #默认租约时间长度
max-lease-time 3600; #最大租约时间长度
subnet 192.168.24.0 netmask 255.255.255.0 { #子网号和掩码
range 192.168.24.50 192.168.24.100; #IP 地址分配范围
option domain-name-servers 192.168.24.1; #域名服务器地址
option domain-name "JQE.com"; #域名
option routers 192.168.24.1; #网关地址
option broadcast-address 192.168.24.254; #广播地址
}
EOF

修改 IP

  • 在 A 中使用 nmtui 命令工具修改 ens33 网卡 IP 为 192.168.24.1, 然后重新激活网卡

  • 在 B 中使用 nmtui 命令修改 ens33 网卡 IP 为自动分配

  • 在 C 中找到网络连接把本地连接 2 改成自动分配 IP

制作 SSH 服务器

  • 创建 Docker 虚拟机:docker run -itd --name nczy --hostname chpw --privileged=true centos:7 /usr/sbin/init

  • 进入:docker exec -it nczy bash

  • 进入虚拟机可能会没网,所以在进入之前先:systemctl restart docker

  • 如果还是没网,就照下面的输:

1
2
3
4
5
6
7
# 在宿主机
cat<<EOF> /etc/sysctl.conf
net.ipv4.ip_forward = 1
EOF
sysctl -p
systemctl restart docker
docker exec -it nczy bash # 启动 nczy

虚拟机里输入:

1
2
3
yum install openssh-server
systemctl restart sshd --now
exit

在宿主机里输入:firewall-cmd --zone=public --add-forward-port=port=2222:proto=tcp:toaddr=172.17.0.2:toport=22
在终端输入:
ssh root@宿主机 IP 地址 -p 2222

在 nczy 安装 RDP 服务,从校园网访问

  • 在虚拟机中输入:

1
2
3
4
5
6
7
8
yum install -y epel-release   #添加第三方安装源 epel-release
yum install -y xrdp
yum groupinstall -y Xfce # 基于上面 epel-release,得先安装它
echo "xfce4-session" > ~/.Xclients
chmod +x ~/.Xclients
sed -i 's/ssl_protocols=.*/ssl_protocols=TLSv1/' /etc/xrdp/xrdp.ini
systemctl start xrdp --now
systemctl enable xrdp --now
  • 在宿主机中输入:

1
2
3
firewall-cmd --permanent --add-port=3390/tcp
firewall-cmd --reload
firewall-cmd --zone=public --add-forward-port=port=3390:proto=tcp:toaddr=172.17.0.2:toport=3389

DNS

添加 Docker 国内镜像

1
2
3
4
5
6
7
8
9
10
11
cat<<EOF> /etc/docker/daemon.json
{
"registry-mirrors": [
"https://9cpn8tt6.mirror.aliyuncs.com",
"https://dockerproxy.com",
"https://hub-mirror.c.163.com",
"https://mirror.baidubce.com",
"https://ccr.ccs.tencentyun.com"
]
}
EOF

拉取 Bind 服务器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
systemctl start docker

docker pull sameersbn/bind

mkdir -p /opt/docker/bind

lsof -i :53 # 查看哪些服务监听着 53 端口
systemctl stop dnsmasq # 暂停 dnsmasq 服务
kill -9 PID # PID 在上一条命令显示出了

docker run --name bind -d \
--restart=always \
-p 53:53/tcp \
-p 53:53/udp \
-p 10000:10000/tcp \
--volume /opt/docker/bind:/data \
sameersbn/bind

docker restart bind

安装 Mysql

基于 Ubuntu 20.04

  • 安装 Mysql 服务
    sudo apt install mysql-server

  • 启动服务
    sudo service mysql start

  • 启动服务
    mysql -u root -p

  • 修改密码和授予远程访问权限

1
2
3
CREATE USER 'root'@'%' IDENTIFIED BY '123456'; # 创建一个用户为 root 并且密码设置为 123456
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' WITH GRANT OPTION; # 授予 root 用户远程访问权限
FLUSH PRIVILEGES; # 刷新配置
  • 修改配置文件在 /etc/mysql/mysql.cnf 文件中输入下面两行

1
2
[mysqld]
bind-address = 0.0.0.0

/etc/mysql/mysql.conf.d/mysqld.conf 中下面两行注释掉

1
2
bind-address = 127.0.0.1
mysqlx-bind-address = 127.0.0.1

service mysql restart

使用 Python 程序向 Linux 系统发送命令

1
2
3
4
5
6
7
8
9
import subprocess

result = subprocess.run(
['ssh', 'root@172.16.124.24', 'ls -l /'],
capture_output=True,
text=True,
encoding='utf-8' # 使用 UTF-8 编码
)
print(result.stdout)