Linux主机初始化
- 系统下载
- 网络配置
- 主机名配置
- yum源更新
- 时钟同步
- 内核参数配置
- 语言时区
- 关闭SELINUX, 防火墙
- SSH参数配置
实验环境
- Hyper V, VMware, VirtualBox等任意一种虚拟机软件
- Centos 7, 假设你已经会安装
- XShell, gitbash, putty等任意一种远程软件
系统下载
https://developer.aliyun.com/mirror/ , 到国内的镜像站下载系统, 速度一般快点
点击进去后你能找到下载地址
一般而言, 路径如 https://mirrors.aliyun.com/centos/7.7.1908/isos/x86_64/
, 选择最小的版本即可
至于是不是centos7无所谓, 目前最近已经到8了(建议用新不用旧)
网络配置
默认地刚安装好的Linux是动态获取(dhcp)ip地址的
[root@localhost ~]# ip addr
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN qlen 1
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:15:5d:e9:01:6e brd ff:ff:ff:ff:ff:ff
inet 172.23.39.154/28 brd 172.23.39.159 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::dfd4:bb10:afa6:20ef/64 scope link
valid_lft forever preferred_lft forever
[root@localhost ~]# ip route
default via 172.23.39.145 dev eth0 proto static metric 100
172.23.39.144/28 dev eth0 proto kernel scope link src 172.23.39.154 metric 10
根据上述命令知道系统的ip地址, 子网掩码, 网关信息, 注意你看见的地址等信息应该和我是不一样的
- ip addr/netmask 172.23.39.154/28 (子网掩码即255.255.255.240)
- geteway 172.23.39.144/28
先备份网卡配置, 做错了可以重来
注意: 因为Centos版本不同, 你的网卡名字可能和我不一样
cp -a /etc/sysconfig/network-scripts/ifcfg-eth0{,.bak}
配置网络
编辑vi /etc/sysconfig/network-scripts/ifcfg-eth0
, 默认地网卡配置是这样的
TYPE="Ethernet"
BOOTPROTO="dhcp"
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"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="eth0"
UUID="dbd5ee26-4d06-4684-beae-632ecbec8c97"
DEVICE="eth0"
ONBOOT="yes"
设置固定IP是必须的, 保留上述配置不要动(新手不要乱动, 有一定基础, 直接复制下面某云的网卡静态IP配置写法), 作如下修改
# 将DHCP修改为static
BOOTPROTO="static"
# ONBOOT下新增静态网络配置信息
IPADDR=172.23.39.154
NETMASK=255.255.255.240
GATEWAY=172.23.39.145
更新网络服务
[root@localhost ~]# systemctl restart network
# 查看地址是否正确, 路由是否正确, 对比一下
[root@localhost ~]# ip addr
[root@localhost ~]# ip route
# 测试网络连通性
[root@localhost ~]# ping 172.23.39.145
最后, 附上ali云的ECS静态IP配置
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=172.18.249.206
NETMASK=255.255.240.0
配置域名解析
如果上一步可以Ping通外网, 这一步可以跳过
[root@localhost ~]# vi /etc/resolv.conf
# google
#nameserver 8.8.8.8
# ali
#nameserver 100.100.2.138
#nameserver 100.100.2.136
options timeout:2 attempts:3 rotate single-request-reop
主机名配置
hostnamectl set-hostname --pretty centos01
很多云主机设置了这个主机名, 建议这条命令不用打
hostnamectl set-hostname centos-01
hostnamectl set-hostname --pretty centos01
vi /etc/hosts
127.0.0.1 centos01 centos-01 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
必须重新连接(可以不用重启), 此时主机名配置生效
验证
[root@centos-01 ~]# hostnamectl
Static hostname: centos-01
Pretty hostname: centos01
Icon name: computer-vm
Chassis: vm
Machine ID: f11f734cc1fb436281c55b315d831a93
Boot ID: ee71a19bc67748b793fb5d2948e367d2
Virtualization: microsoft
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-1062.12.1.el7.x86_64
Architecture: x86-64
yum源更新
官方教程非常详细, 此处不介绍, 云主机一般都有自己的源, 不需要设置, 实验环境需要
时钟同步
云主机一般用不上, 实验环境一般不用, 所以这个跳过吧
安装时钟同步软件
[root@centos-01 ~]# yum install -y ntpdate ntp-doc
timedatectl set-timezone Asia/Shanghai
/usr/sbin/ntpdate 0.cn.pool.ntp.org > /dev/null 2>&1
/usr/sbin/hwclock --systohc
/usr/sbin/hwclock -w
cat > /var/spool/cron/root << EOF
10 0 * * * /usr/sbin/ntpdate 0.cn.pool.ntp.org > /dev/null 2>&1
* * * * */1 /usr/sbin/hwclock -w > /dev/null 2>&1
EOF
chmod 600 /var/spool/cron/root
/usr/bin/systemctl restart crond
内核参数配置
cp /etc/sysctl.conf /etc/sysctl.conf.bak
cat > /etc/sysctl.conf << EOF
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv4.tcp_syn_retries = 1
net.ipv4.tcp_synack_retries = 1
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_probes = 3
net.ipv4.tcp_keepalive_intvl =15
net.ipv4.tcp_retries1 = 3
net.ipv4.tcp_retries2 = 5
net.ipv4.tcp_fin_timeout = 10
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_max_tw_buckets = 60000
net.ipv4.tcp_max_orphans = 32768
net.ipv4.tcp_max_syn_backlog = 16384
net.ipv4.tcp_mem = 94500000 915000000 927000000
net.ipv4.tcp_wmem = 4096 16384 13107200
net.ipv4.tcp_rmem = 4096 87380 17476000
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.route.gc_timeout = 100
net.core.somaxconn = 32768
net.core.netdev_max_backlog = 32768
net.nf_conntrack_max = 6553500
net.netfilter.nf_conntrack_max = 6553500
net.netfilter.nf_conntrack_tcp_timeout_established = 180
vm.overcommit_memory = 1
vm.swappiness = 1
fs.file-max = 65535
EOF
立即生效
sysctl -p
验证
sysctl -a
延申阅读
语言时区
一般而言云主机都不需要设置
,变更为英文, 验证ls -al
显示的是全英文
echo "LANG=\"en_US.UTF-8\"">/etc/locale.conf
source /etc/locale.conf
# timedatectl list-timezones
timedatectl set-timezone Asia/Shanghai
验证时区
[root@centos-01 ~]# date -R
Wed, 26 Feb 2020 10:26:55 +0800
[root@centos-01 ~]# timedatectl status
Local time: Wed 2020-02-26 10:27:39 CST
Universal time: Wed 2020-02-26 02:27:39 UTC
RTC time: Wed 2020-02-26 02:27:39
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/
关闭SELINUX, 防火墙
一般而言云主机都是关闭的
, 可以跳过, 实验环境是必定关闭的,免去很多干扰
sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
systemctl stop firewalld.service
systemctl disable firewalld.service
SSH参数配置
注意一下参数, 一般而言PermitRootLogin yes
就可以用root远程连接了
...
ListenAddress 0.0.0.0
...
PermitRootLogin yes
systemctl restart sshd