服务器配置-从0到分析2:服务器基本设置

发布于:2025-02-21 ⋅ 阅读:(16) ⋅ 点赞:(0)

最基础的:

zerotier组建虚拟局域网,再内网穿透

在vscode(coding)、mobaxterm(normal pc)、termius(normal Android)端配置client,能够remote-ssh再看下面

以及基本的文件读写权限:

sudo chown -R your_username:your_username /data1

sudo chmod 755 #这3个文件夹

一、更新组件,包管理

sudo apt update  #这个命令会更新软件包列表,让系统知道有哪些软件包可以更新。
sudo apt upgrade --only-upgrade #这个命令会安装所有可用的软件包更新。

二、常用工具

1、VIM编辑器

检查有没有安装VIM(主要是我习惯用vim编辑器了,Ubuntu是默认自带nano的)

plain vim --version

配置VIM为默认的系统编辑器。

  • 很简单,执行这条命令,选择 Vim,以后凡是自动调用编辑器的地方,都会用 Vim 啦。
plain sudo update-alternatives --config editor

2、安装 command-not-found

很多服务器提供商可能会提供精简版本的 Ubuntu,于是一些实用的命令行工具并不会预装。比如command-not-found,它可以当你在打命令时,提示对应的但没有安装的 package。

plain #!/bin/bash # 更新软件包列表并升级系统 sudo apt update && sudo apt upgrade -y # 安装常用工具 sudo apt install -y \ curl \ wget \ git \ vim \ htop \ tree \ net-tools \ build-essential \ zip \ unzip \ ufw \ tmux # Optional: 安装其他实用工具 # sudo apt install -y \ # python3-pip \ # openvpn \ # jq \ # neofetch echo "常用工具安装完成。"

安装完后,就可以在使用命令行的过程中更加方便了。

当然tmux其实也有个性化的配置,参考:

https://github.com/gpakosz/.tmux

3、修改时区默认语言

可以先timedatectl命令查看时区信息是否准确

sudo update-locale LANG=en_US.UTF-8
sudo timedatectl set-timezone Asia/Shanghai

reboot #重启

三、添加普通用户

adduser {your-username}
visudo

<font style="color:rgb(44, 62, 80);">User Privilege Specification</font> 下加入一行 <font style="color:rgb(44, 62, 80);">{your-username} ALL=(ALL) NOPASSWD: ALL</font>即可。

检验是否添加成功

plain su - newuser #切换到新用户 ls /root #列出/root目录下的文件(没有root权限是看不了的) sudo ls /root #然后用给普通用户授予root权限(这样就有权限可以看到了,只要没有报错即成功,因为/root目录下可能没有东西) exit #退出

四、防火墙配置

要在 Ubuntu 上使用 ufw(Uncomplicated Firewall)开启端口 22、80 和 443,你可以按照以下步骤进行配置:

  1. 检查**** **ufw** ****是否安装
    • 如果尚未安装 ufw,你可以使用以下命令安装:sudo apt install ufw
  2. 开启端口
    • 开启端口 22:sudo ufw allow 22
    • 开启端口 80:sudo ufw allow 80
    • 开启端口 443:sudo ufw allow 443
  3. 启用防火墙
    • 启用 ufw 防火墙:sudo ufw enable
  4. 检查配置
    • 可以运行 sudo ufw status 来查看防火墙的状态和已开启的端口。

一般Linux系统默认都存在iptables,新机设置前使用iptables放行所有规则。然后你再使用其他的防火墙工具(如:ufw)。因为装在你机子上的所有防火墙工具,它们之间是互相影响的

五、配置 SSH 登录及 SSH Server 安全设定(不强求)
这里有个注意点!如果你想禁用root密码登录,并用普通用户 {your-username} ssh登录服务器,这里SSH登录配置部分一定要用普通用户{your-username}配置,不然配置出来的文件权限和拥有者不对,ssh不能正确读取公钥!

  • 先在windows端生成ssh 公私钥对
plain ssh-keygen -t rsa -f ~/.ssh/id_rsa_xxxx
  • 在用户目录下创建authorized_keys文件,并将公钥(pub结尾)的内容粘入authorized_keys文件中
plain su - {your-username} #不要在root身份下配置 mkdir -p ~/.ssh touch ~/.ssh/authorized_keys #并将公钥(pub结尾)的内容粘入`authorized_keys`文件中 vim ~/.ssh/authorized_keys chmod 600 ~/.ssh/authorized_keys

~ 是指在当前用户的主目录下(如果是root,就是在/root/下;如果是{your-username}则在/home/{your-username}/)。
如果配置在root目录下,那用ssh 密钥对登录时就要选择root用户登录!

  • 禁用root身份登录
    • 找到PermitRootLogin Yes这一项,然后把它后面的设定值改为no即可。
plain sudo vim /etc/ssh/sshd_config
- <font style="color:rgb(44, 62, 80);">如下:</font>
plain PermitRootLogin no #开启ssh密钥对登录 PubkeyAuthentication yes
  • 把 PasswordAuthentication 设置成 no,禁止密码登录,更安全:
plain PasswordAuthentication no
  • SSH端口号改为其他数字,注意了,改成其他端口后,记得防火墙设置也要更新
plain Port {SSH端口号,最好在10000以上}

最后重启 SSH Server 生效:

plain sudo systemctl restart sshd.service

——》当然上面如果不需要,或者说是觉得麻烦的话也不需要设置,正常开放22端口之后,按照我之前博客上zerotier组间局域虚拟网的设置,已经能够vscode remote-ssh了

https://blog.csdn.net/weixin_62528784/article/details/144095579?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522f76f3bf9879979b2911cbba8b1eb4816%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=f76f3bf9879979b2911cbba8b1eb4816&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2blogfirst_rank_ecpm_v1~rank_v31_ecpm-1-144095579-null-null.nonecase&utm_term=zerotier&spm=1018.2226.3001.4450

六、自定义shell 界面安装

plain sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

可以通过bash和zsh来回切换shell

cat /etc/shells #查看当前所有的shell

其实可以将zsh作为默认的shell,毕竟功能兼容bash且比bash强大

使用 **<font style="color:rgb(236, 72, 153);background-color:rgb(245, 245, 245);">chsh -s /bin/zsh</font>** 命令将 zsh 设置为系统默认 shell。新开一个 Shell Session,就可以开始使用 zsh 了。

下面是如何配置zsh,就是修改主题

https://github.com/ohmyzsh/ohmyzsh/wiki/Themes

https://github.com/ohmyzsh/ohmyzsh/wiki/Themes 中查看内置的主题样式和对应的主题名。这些内置主题已经放在 **<font style="color:rgb(236, 72, 153);background-color:rgb(245, 245, 245);">~/.oh-my-zsh/themes</font>** 目录下,不需要再下载。

安装插件

oh-my-zsh 已经内置了 git 插件,内置插件可以在 **<font style="color:rgb(236, 72, 153);background-color:rgb(245, 245, 245);">~/.oh-my-zsh/plugins</font>** 中查看 ,下面介绍一下我常用的三个插件,更多插件可以在 awesome-zsh-plugins 里查看。

zsh-autosuggestions

zsh-autosuggestions 是一个命令提示插件,当你输入命令时,会自动推测你可能需要输入的命令,按下右键可以快速采用建议。

安装步骤:暂时不能执行(有问题)!!!

参考:https://www.hackerneo.com/blog/dev-tools/better-use-terminal-with-zsh

1.把插件下载到本地的 **<font style="color:rgb(236, 72, 153);background-color:rgb(245, 245, 245);">~/.oh-my-zsh/custom/plugins</font>** 目录:

git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions

2.在 **<font style="color:rgb(236, 72, 153);background-color:rgb(245, 245, 245);">.zshrc</font>** 中,把 **<font style="color:rgb(236, 72, 153);background-color:rgb(245, 245, 245);">zsh-autosuggestions</font>** 加入插件列表:

plugins=(
    # other plugins...
    zsh-autosuggestions  # 插件之间使用空格隔开
)

七、Docker配置(不强求)

1、安装 Docker 和 docker-compose

| plain #国内一键安装 sudo curl -sSL https://get.daocloud.io/docker | sh #国外一键安装 sudo curl -sSL get.docker.com | sh #docker-compose sudo curl -L "https://github.com/docker/compose/releases/download/1.24.1/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose sudo chmod +x /usr/local/bin/docker-compose |
| — |

  • 安装好 Docker 以后,记得将当前用户加到 docker 用户组里去(如果你不想每次都用 sudo 用 Docker 的话)
plain sudo gpasswd docker -a username

2、Docker日志管理

全局配置日志大小限制

  • 创建或修改文件 /etc/docker/daemon.json,并增加以下配置(3份日志、每份10M)。
plain { "log-driver": "json-file", "log-opts": { "max-file": "3", "max-size": "10m" } }
  • 随后重启 Docker 服务
plain sudo systemctl daemon-reload sudo systemctl restart docker

不过已存在的容器不会生效,需要重建才可以!

单个容器日志大小限制

  • 写在docker-compose中
plain logging: driver: json-file options: max-size: "100m" max-file: "3"

八、swap配置(不强求)

plain sudo swapoff -a #删除原分区 sudo dd if=/dev/zero of=/root/swapfile bs=1M count=1024 #配置新分区大小 sudo mkswap /root/swapfile sudo swapon /root/swapfile sudo vim /etc/fstab #最后设置开机启动:可以编辑 /etc/fstab 文件,把最后一行改成:`/root/swapfile swap swap defaults 0 0`

参考文章:

九、logrotate日志大小限制

plain sudo apt install logrotate sudo apt install cron

配置文件目录:/etc/logrotate.d/

plain /var/log/syslog /var/log/mail.log /var/log/kern.log /var/log/auth.log /var/log/user.log /var/log/cron.log { weekly rotate 3 maxsize 100M missingok notifempty compress delaycompress sharedscripts postrotate /usr/lib/rsyslog/rsyslog-rotate endscript }

查看服务状态

plain 1 2 plain sudo systemctl status logrotate.service sudo systemctl status logrotate.timer

参考文章:

十、Fail2ban 封禁 IP

一、正常安装

plain sudo apt-get install fail2ban

十一、面板安装——1panel

plain #apt install curl sudo curl -sSL https://resource.fit2cloud.com/1panel/package/quick_start.sh -o quick_start.sh && sudo bash quick_start.sh #sudo apt autoremove docker-compose

参考文章:

十二、常规安全更新

  • 安装 unattended-upgrades 来自动更新 security upgrade

通过 unattended-upgrades,可以使 Ubuntu 系统自动进行常规的安全相关更新,使系统一直保持 security。

plain sudo apt install unattended-upgrades sudo dpkg-reconfigure unattended-upgrades

参考:
https://linux.do/t/topic/115839/31

下一篇博客:就是在服务器上安装并更新github插件、R以及python语言环境以及IDE的配置