Centos初始化操作
1.单机配置
1.1最低机器硬件配置
1.2软件环境配置
1.3前置申请
2.软件环境安装
机器预先安装操作系统centos7
更换软件源(其他版本更换源参考centos安装教程)
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
# 或者
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
安装小工具
#安装ipconfig,vim,wget,curl,unzip
yum install -y net-tools.x86_64 vim wget crul unzip
2.1配置admin用户
避免直接以root用户进行运维
登录系统 ssh root@server_ip_address
创建admin用户
# 创建admin用户
adduser admin
# 设置admin用户密码
passwd admin
# 使work用户支持sudo
usermod -aG wheel admin
设置admin用户仅使用publickey登录
# 在用户本机执行(xshell无需登录,直接运行) 拷贝ssh publickey到work用户,例如:ssh-copy-id work@47.99.247.96
# 生成密钥对的命令 ssh-keygen -t rsa
ssh-copy-id admin@${server_ip_address}
用ssh登录系统
ssh admin@server_ip_address
如果报错/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
# 使用如下命令,删除host里面的老配置即可
ssh-keygen -f ~/.ssh/known_hosts -R ${ip}
修改sshd配置 vim /etc/ssh/sshd_config
小技巧
vim命令下可以使用/xxx
进行查找,推出高亮状态:non
# 关闭root用户
PermitRootLogin no
PasswordAuthentication no
# 末尾添加
Match User admin
PasswordAuthentication no
AuthenticationMethods publickey
重启
sshd
服务
service sshd restart
补充知识:如何添加其他ssh登陆用户
#切换到用户主目录(非root用户)
cd ~
#查看是否存在.ssh隐藏文件夹
ls -la
#修改用户的ssh
vim authorized_keys
# 重启sshd
sudo systemctl restart sshd
2.2.安装docker
参考:Centos7安装docker - 知识星光 (litecode.top)
2.3 安装配置nginx
参考:Centos安装配置Nginx - 知识星光 (litecode.top)
3.创建虚拟内存
3.1创建swap文件
#在root用户下,进入/usr目录
[root@localhost usr]$ pwd
/usr
#创建swap文件夹,并进入该文件夹
[root@localhost usr]# mkdir swap
[root@localhost usr]# cd swap/
#创建swapfile文件,使用命令dd if=/dev/zero of=/usr/swap/swapfile bs=1M count=4096
[root@localhost swap]# dd if=/dev/zero of=/usr/swap/swapfile bs=1M count=4096
记录了4096+0 的读入
记录了4096+0 的写出
4294967296字节(4.3 GB)已复制,15.7479 秒,273 MB/秒
3.2查看swap文件
#使用命令du -sh /usr/swap/swapfile,可以看到我们创建的这个swap文件为4g
[root@localhost swap]# du -sh /usr/swap/swapfile
4.1G /usr/swap/swapfile
3.3将目标设置为swap分区文件
#使用命令mkswap /usr/swap/swapfile将swapfile文件设置为swap分区文件
[root@localhost swap]# mkswap /usr/swap/swapfile
mkswap: /usr/swap/swapfile: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 4194300 KiB
no label, UUID=5bd241ff-5375-449d-9975-5fdd429df784
3.4激活swap区,并立即启用交换区文件
[root@localhost swap]# swapon /usr/swap/swapfile
#查看分区
[root@localhost swap]# free -h
total used free shared buffers cached
Mem: 980 910 70 3 8 575
-/+ buffers/cache: 326 654
Swap: 4095 0 4095
3.5设置开机自动启用虚拟内存
#在/etc/fstab文件中加入如下命令
vim /etc/fstab
/usr/swap/swapfile swap swap defaults 0 0
3.6重启服务器
[root@localhost swap]# shutdown -r now
Broadcast message from liaocheng@localhost.localdomain
(/dev/pts/1) at 3:56 ...
The system is going down for reboot NOW!
[root@localhost swap]# Connection to 192.168.136.142 closed by remote host.
Connection to 192.168.136.142 closed.
[进程已完成]
重启完成过后使用free -m 命令来查看现在的内存是否挂在上了。
[root@localhost swap]# free -h
total used free shared buffers cached
Mem: 980 910 70 3 8 575
-/+ buffers/cache: 326 654
Swap: 4095 0 4095