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

news/2025/2/23 6:49:38

最基础的:

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 界面安装

  • 安装 oh-my-zsh:
    https://github.com/ohmyzsh/ohmyzsh
plain sh -c "$(curl -fsSL https://raw.github.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"

  • 其他主题、插件等配置参考:
    • https://www.hackerneo.com/blog/dev-tools/better-use-terminal-with-zsh

可以通过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

  • 官方地址:https://docs.docker.com/install/linux/docker-ce/ubuntu/

| 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`

参考文章:

  • https://skywt.cn/blog/changeswap/

九、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

参考文章:

  • https://www.cnblogs.com/uglyliu/p/13206868.html
  • https://wsgzao.github.io/post/logrotate/
  • https://www.noisyfox.io/logrotate.html
  • https://www.cnblogs.com/liujunjun/p/17924699.html
  • https://nj.transwarp.cn:8180/?p=10556

十、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

参考文章:

  • https://blog.pppfz.com/posts/5.html

十二、常规安全更新

  • 安装 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的配置


http://www.niftyadmin.cn/n/5863146.html

相关文章

041集——封装之:新建图层(CAD—C#二次开发入门)

如图所示&#xff1a;增加一个图层“新图层”&#xff0c;颜色为红&#xff08;1&#xff09;&#xff0c;当图层颜色定义为黄&#xff08;2&#xff09;时&#xff0c;直接覆盖之前图层颜色&#xff0c;图层名不变。 代码如下&#xff1a; /// </summary>/// <param …

《A++ 敏捷开发》- 16 评审与结对编程

客户&#xff1a;我们的客户以银行为主&#xff0c;他们很注重质量&#xff0c;所以一直很注重评审。他们对需求评审、代码走查等也很赞同&#xff0c;也能找到缺陷&#xff0c;对提升质量有作用。但他们最困惑的是通过设计评审很难发现缺陷。 我&#xff1a;你听说过敏捷的结对…

PolyOS 是面向 RISC-V 架构的智能终端和 AIoT 开源操作系统(基于开源鸿蒙)

PolyOS 是面向 RISC-V 架构的智能终端和 AIoT 开源操作系统 官网&#xff1a;PolyOS | PolyOS PolyOS Mobile&#xff0c;作为一款面向 RISC-V 架构的智能终端操作系统&#xff0c;我们希望能够构建卓越的特性和创新的生态系统来引领未来移动操作系统技术的发展。该操作系统基…

Windows 上源码安装 FastGPT

FastGPT 是一个强大的 AI RAG 平台&#xff0c;值得我们去学习了解。与常见的 Python 体系不同&#xff0c;Fast GPT 采用 Node.js/Next.js 平台&#xff08;对于广大 JS 开发者或前端开发者比较亲切友好&#xff09;&#xff0c;安装或部署比较简单。虽然一般情况下推荐简单的…

微软CEO-纳德拉访谈-AGI计划

在与知名科技播客主播 Dwarkesh Patel 的深度访谈中,微软 CEO 萨提亚・纳德拉围绕 AI、量子计算、微软发展等多方面分享深刻见解,下面是针对访谈内容的介绍,其中还是有很多值得我们学习的地方。 1 AI 领域见解 影响力评估:纳德拉直言行业所标榜的 AGI 里程碑是 “无意义的基…

C++——list模拟实现

目录 前言 一、list的结构 二、默认成员函数 构造函数 析构函数 clear 拷贝构造 赋值重载 swap 三、容量相关 empty size 四、数据访问 front/back 五、普通迭代器 begin/end 六、const迭代器 begin/end 七、插入数据 insert push_back push_front 八、…

A097基于SpringBoot实现的社区博客管理系统

系统介绍 基于SpringBootVue实现的社区博客管理系统设计了超级管理员、系统观察者、用户三种角色&#xff0c;超级管理员可对用户授权&#xff0c;具体实现的功能如下 • 文章采用了比较火的Markdown编辑器。 • 系统集成了邮件发送功能。 • 系统可以自定义友情链接。…

无人机避障——Mid360+Fast-lio感知建图+Ego-planner运动规划(胎教级教程)

电脑配置&#xff1a;Xavier-nx、ubuntu 18.04、ros melodic 激光雷达&#xff1a;Livox_Mid-360 结果展示&#xff1a;左边Mid360Fast-lio感知建图&#xff0c;右边Ego-planner运动规划 1、读取雷达数据并显示 无人机避障——感知篇&#xff08;采用Livox-Mid360激光雷达获…