第一章—Linux新手入门:从零开始的初始化配置指南
初学linux得一些初始化配置详细步骤
1. 配置SSH(远程连接)
CentOS/Rocky Linux
① 编辑SSH配置文件
vim /etc/ssh/sshd_config
② 配置SSH登录方式
# 允许root登录(学习环境)
PermitRootLogin yes
# 生产环境建议使用密钥登录,禁用密码登录
# PasswordAuthentication no
# PubkeyAuthentication yes
③ 重启SSH服务
systemctl restart sshdS
Ubuntu
① 安装SSH服务
# 检查SSH状态
systemctl status sshd
# 安装OpenSSH服务端
apt install openssh-server
# 启动SSH服务
systemctl start ssh
# 设置root密码
passwd root
② 配置SSH(同CentOS)
编辑 /etc/ssh/sshd_config 并重启服务:
systemctl restart ssh
SSH安全加固(扩展)
# 修改默认端口(可选)
Port 2222
# 禁用空密码
PermitEmptyPasswords no
# 限制最大尝试次数
MaxAuthTries 3
# 设置空闲超时
ClientAliveInterval 300
ClientAliveCountMax 2
配置防火墙
Ubuntu:
- 查看防火墙状态
status ufw - 临时开启/关闭防火墙
ufw start/stop - 永久开启/关闭防火墙
ufw enable/disable
Rocky:
-
查看防火墙状态
systemctl status firewalld -
开启/关闭防火墙
systemctl enable/disable firewalld -
关闭selinux
# 永久关闭Selinux [root@localhost ~]# nano /etc/selinux/config SELINUX=disabled # SELINUX的值改为disabled # 临时关闭selinux [root@localhost ~]# setenforce 0 # 关闭防火墙 [root@localhost ~]# systemctl disable firewalld.service Removed "/etc/systemd/system/multi-user.target.wants/firewalld.service". Removed "/etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service".
2、换源
-
Ubuntu换源
[root@magedu-VMware-Virtual-Platform]# vim /etc/apt/sources.list # Ubuntu sources have moved to /etc/apt/sources.list.d/ubuntu.sources deb https://mirrors.aliyun.com/ubuntu/ noble main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ noble-updates main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ noble-backports main restricted universe multiverse deb https://mirrors.aliyun.com/ubuntu/ noble-security main restricted universe multiverse [root@magedu-VMware-Virtual-Platform]# apt update -
Centos换源
- 更改配置文件
[root@localhost /etc/yum.repos.d]# mkdir -p /etc/yum.repos.d/backup [root@localhost /etc/yum.repos.d]# mv /etc/yum.repos.d/*.reop /etc/yum.repos.d/backup [root@localhost /etc/yum.repos.d]# vim rocky.repo # rocky.repo # # The mirrorlist system uses the connecting IP address of the client and the # update status of each mirror to pick current mirrors that are geographically # close to the client. You should use this for Rocky updates unless you are # manually picking other mirrors. # # If the mirrorlist does not work for you, you can try the commented out # baseurl line instead. [baseos] name=Rocky Linux $releasever - BaseOS #mirrorlist=https://mirrors.rockylinux.org/mirrorlist? arch=$basearch&repo=BaseOS-$releasever$rltype baseurl=https://mirrors.aliyun.com/rockylinux/10/BaseOS/x86_64/os/ gpgcheck=1 enabled=1 gpgkey=https://mirrors.aliyun.com/rockylinux/RPM-GPG-KEY-Rocky-10 [AppStream] name=Rocky Linux $releasever - AppStream - Debug #mirrorlist=https://mirrors.rockylinux.org/mirrorlist? arch=$basearch&repo=BaseOS-$releasever-debug$rltype baseurl=https://mirrors.aliyun.com/rockylinux/10/AppStream/x86_64/os/ gpgcheck=1 enabled=1 gpgkey=https://mirrors.aliyun.com/rockylinux/RPM-GPG-KEY-Rocky-10 [extras] name=Rocky Linux $releasever - Extras - Source #mirrorlist=https://mirrors.rockylinux.org/mirrorlist?arch=source& repo=BaseOS-$releasever source$rltype baseurl=https://mirrors.aliyun.com/rockylinux/10/extras/x86_64/os/ gpgcheck=1 enabled=1 gpgkey=https://mirrors.aliyun.com/rockylinux/RPM-GPG-KEY-Rocky-10 [epel] name=Rocky Linux $releasever - EPEL - Source #mirrorlist=https://mirrors.rockylinux.org/mirrorlist?arch=source& repo=BaseOS-$releasever source$rltype baseurl=https://mirrors.aliyun.com/epel/10/Everything/x86_64/ gpgcheck=1 enabled=1 gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-10 -
生成元数据缓存
yum makecache -
安装epel-release
yum install epel-release -
更新源
yum update
-
一键更新源
wget https://www.mysticalrecluse.com/script/Shell/switchsource bash switchsource -h 查看帮助 Options: -h, --help Show this help message and exit -a, --ali Specify alibaba software source#阿里 -1, --163 Specify 163-wangyi software source#网易 -t, --tsinghua Specify tsinghua software source#清华 -H, --huawei Specify huawei software source#华为 -u, --ustc Specify ustc-zhongkeda software source#中科院
3、配置提示符
-
格式:
-
提示符格式为:
[\u@\h \W]\$ -
其中:
\u表示当前用户\h表示主机名\W表示当前工作目录\$表示提示符
-
添加颜色
PS1='[\e[32m\u@\h \W\e[0m]\$ ' # 豆包搜自己喜欢的
-
-
Centos:
-
编辑bashrc文件
sudo vim .bashrc -
配置提示符
PS1='[\u@\h \W]\$ ' -
使配置生效
source/. .bashrc -
对所有普通用户生效
# 对所有普通用户生效 vim /etc/profile.d/env.sh # env.sh自行创建 PS1="\[\e[1;33m\][\u@\h \w] \$\[\e[0m\]" source env.sh #使其生效
-
-
Ubuntu:
-
编辑bashrc文件
vim .bashrc -
修改#force_color_prompt=yes,将#删除解除注释
-
使配置生效
source/. .bashrc -
对所有普通用户生效
# 对所有普通用户生效 vim /usr/share/bash-completion/bash_completion # 在文件下方追加 PS1="\[\e[1;33m\][\u@\h \w] \$\[\e[0m\]" source /usr/share/bash-completion/bash_completion #使其生效
-
-
注意
- 全局变量与当前用户的优先级
- shell类型,不支持sh的用户
- 注意内核版本
4、配置环境变量
- 目的:
将root用户主目录下的bin文件夹添加到系统的可执行文件搜索路径,如果你在 `~/bin` 中放置了自定义脚本或程序(如 `mytool`),可以直接在终端输入命令名运行,而无需指定完整路径(例如 `./mytool`)。
- 配置bashrc文件
sudo vim /etc/bash.bashrc - 配置环境变量
export PATH=$PATH:~/bin - 使配置生效
source /etc/bash.bashrc
5、配置别名
- 编辑bashrc文件
sudo vim /etc/bash.bashrc - 配置别名(显示高亮)
#示例 # 系统管理 alias stu='systemctl status' # 查看服务状态 alias sta='systemctl start' # 开启服务 alias res='systemctl restart'# 重启服务 alias sto='systemctl stop' # 停止服务 alias sen='systemctl enable' alias sdis='systemctl disable' # 安全操作 alias rm='rm -i' alias cp='cp -i' alias mv='mv -i' # 便捷查看 alias ll='ls -lh' alias la='ls -la' alias lt='ls -ltr' # 网络相关 alias myip='curl ipinfo.io/ip' alias ports='netstat -tulanp' # 使配置生效 source /etc/bash.bashrc - 使配置生效
source /etc/bash.bashrc
6、修改主机名
hostnamectl set-hostname <主机名>
exit 生效
7、日期与时间
7.1 设置时区
# 查看当前时区
timedatectl status
# 设置上海时区
timedatectl set-timezone Asia/Shanghai
# 验证
date
7.2 时间同步
# Ubuntu
sudo apt install chrony
sudo systemctl enable --now chrony
# Rocky/CentOS
sudo yum install chrony
sudo systemctl enable --now chronyd
# 查看同步状态
chronyc sources -v
7、配置登陆前提示(区别虚拟终端和伪终端)
- ubuntu(远程ssh):
- 在ubuntu中修改登录前提示,通常涉及两个部分
-
1、
motd (Message Of The Day):这是用户成功登录后看到的欢迎信息。 -
2、
issue / issue.net:这是在登录提示符出现之前显示的系统标识或欢迎信息,本地登录看 issue,远程 SSH 登录看issue.net。 -
编辑/etc/issue.net文件
sudo vim /etc/issue.net -
配置登陆前提示
欢迎来到ubuntu 24.04 -
SSH 默认可能不会显示
issue.net的内容。你需要修改 SSH 配置文件来启用它。sudo vim /etc/ssh/sshd_config -
找到
#Banner none这一行,将其修改为:Banner /etc/issue.net -
重启 SSH 服务
sudo systemctl restart ssh -
关于显示其他登录前提示
Ubuntu 的登录提示(MoTD)是由/etc/update-motd.d/目录下的可执行脚本生成的,不同脚本对应不同提示内容:
以下是/etc/update-motd.d/目录下常见脚本的 详细说明表格,包含脚本文件名、对应提示内容、作用及备注,方便精准禁用:脚本文件名 对应提示内容 作用 备注 00-header显示欢迎标题,例如: Welcome to Ubuntu 22.04.3 LTS (GNU/Linux 5.15.0-107-generic x86_64)
包含系统版本、内核信息系统登录欢迎头 默认启用,展示系统基本信息 10-help-text显示帮助链接信息: Documentation: https://help.ubuntu.comManagement: https://landscape.canonical.comSupport: https://ubuntu.com/advantage提供官方文档、管理工具、支持服务入口 对新手友好,老用户可禁用 50-motd-news显示 Ubuntu 官方新闻或推广信息,例如: Welcome to Ubuntu Pro (Infra-only subscription).
或活动通知、软件更新提示推送官方动态、商业服务(如 Ubuntu Pro) 可通过 sudo pro disable motd-news单独禁用80-esm显示扩展安全维护(ESM)提示,例如: Ubuntu Pro: ESM Inactive - You are not receiving security updates for critical packages.
或启用后的状态提示提醒 ESM 服务状态(适用于 LTS 版本) 仅当系统未启用 ESM 时显示,禁用后不再提示 90-updates-available显示系统待更新数量及命令提示: 12 updates can be applied immediately.To see these updates run: apt list --upgradable
包含安全更新、普通更新的数量提醒系统更新,保障安全性 核心提示,建议保留;若无需提醒可禁用 91-release-upgrade提示系统版本升级信息,例如: New release '24.04 LTS' available.Run 'do-release-upgrade' to upgrade to it.通知可用的系统版本升级(如从 22.04 到 24.04) 仅当有新版本时显示,禁用后不再提醒 95-hwe-eol显示硬件启用栈(HWE)生命周期结束提示,例如: Your Hardware Enablement Stack (HWE) will reach end of life on 2025-04-30.
提醒更换内核版本针对使用 HWE 内核的系统,提示内核支持期限 若未使用 HWE 内核,该脚本可能不生效 98-fsck-at-reboot提示系统重启后需执行文件系统检查(fsck),例如: The system will check the filesystem on reboot.通知文件系统存在问题,需重启检查 仅当系统检测到 fsck 需求时显示,禁用后可能错过重要提示 98-reboot-required提示系统需要重启以应用更新,例如: *** System restart required ***提醒用户重启系统,确保更新生效 核心提示,建议保留;若临时无法重启,可暂时禁用 99-footer部分系统可能存在的尾注脚本,显示空行或版权信息 优化提示格式,与其他提示分隔 部分 Ubuntu 版本无此脚本,作用较小 补充说明:
- 禁用方法:对不需要的脚本执行
sudo chmod -x 脚本名(如sudo chmod -x 50-motd-news),重启会话即可生效。 - 恢复方法:若需恢复提示,执行
sudo chmod +x 脚本名重新赋予执行权限。 - 用户级关闭:在用户主目录创建
.hushlogin文件(touch ~/.hushlogin),可关闭当前用户的所有登录提示(优先级高于系统级配置)。
- 删除 / 关闭登录提示的方法
根据需求选择对应的方式: - 方法 1:按需禁用特定提示(推荐)
只关闭不需要的提示,保留有用信息(比如更新提醒):
进入/etc/update-motd.d/目录:cd /etc/update-motd.d/ - 查看所有脚本(数字开头的文件):
ls -l - 禁用不需要的脚本(移除执行权限):
例如,禁用 “帮助链接” 和 “新闻推广”:sudo chmod -x 10-help-text 50-motd-news - (其他脚本同理,比如90-updates-available对应更新提示)
- 方法 2:完全关闭所有 MoTD 提示
如果希望登录时无任何提示:
禁用/etc/update-motd.d/下所有脚本:sudo chmod -x /etc/update-motd.d/* - (可选)修改 SSH 配置(仅对 SSH 登录生效):
编辑/etc/ssh/sshd_config,将PrintMotd设为no:sudo nano /etc/ssh/sshd_config - 添加 / 修改:
ini PrintMotd no - 重启 SSH 服务:
sudo systemctl restart sshd - 方法 3:用户级关闭(仅对当前用户生效)
在当前用户主目录创建.hushlogin文件,即可关闭该用户的登录提示:touch ~/.hushlogin
- 禁用方法:对不需要的脚本执行
-
8、修改登录后的欢迎信息 (motd)
简单方法:直接编辑motd文件
bash
sudo vim /etc/motd
添加内容示例:
text
=====================================================
欢迎回来!今天是 $(date +"%Y年%m月%d日 %A")
系统负载:$(uptime | awk '{print $10,$11,$12}')
=====================================================
推荐方法:动态脚本
① 创建自定义脚本
bash
sudo vim /etc/update-motd.d/99-custom-message
② 脚本内容
bash
#!/bin/bash
echo "====================================================="
echo " 欢迎使用 $(hostname) 服务器"
echo " 系统版本: $(lsb_release -d | cut -f2 2>/dev/null || cat /etc/redhat-release)"
echo " 当前时间: $(date '+%Y-%m-%d %H:%M:%S %Z')"
echo " 运行时间: $(uptime -p)"
echo " 系统负载: $(uptime | awk -F'load average:' '{print $2}')"
echo " 内存使用: $(free -h | awk '/^Mem:/ {print $3"/"$2}')"
echo " 磁盘空间: $(df -h / | awk 'NR==2 {print $3"/"$2 " ("$5")"}')"
echo "====================================================="
③ 添加执行权限
bash
sudo chmod +x /etc/update-motd.d/99-custom-message
④ 测试效果
bash
sudo /etc/update-motd.d/99-custom-message
9、man实现高亮显示与实时翻译(扩展了解)
-
编辑bashrc文件
sudo vim .bashrc 输入export MANPAGER='vim -M +MANPAGER -' -
wq保存退出
. .bashrc使配置生效 -
连接代理服务器
export http_proxy=http://101.35.250.82:18888 -
下载minpaccha插件
git clone https://github.com/k-takata/minpac.git ~/.vim/pack/minpac/opt/minpac -
编辑vimrc文件
sudo vim .vimrc -
添加以下内容
colorscheme murphy runtime! ftplugin/man.vim if exists('*minpac#init') " Minpac is loaded. call minpac#init() call minpac#add('k-takata/minpac', {'type': 'opt'}) " Other plugins call minpac#add('tpope/vim-eunuch') call minpac#add('yegappan/mru') call minpac#add('bujnlc8/vim-translator') endif if has('eval') " Minpac commands command! PackUpdate packadd minpac | source $MYVIMRC | call minpac#update('', {'do': 'callminpac#status()'}) command! PackClean packadd minpac | source $MYVIMRC | call minpac#clean() command! PackStatus packadd minpac | source $MYVIMRC | call minpac#status() endif if !has('gui_running') " 设置文本菜单 if has('wildmenu') set wildmenu set cpoptions-=< set wildcharm=<C-Z> nnoremap <F10> :emenu <C-Z> inoremap <F10> <C-O>:emenu <C-Z> endif endif let g:translator_cache=1 let g:translator_cache_path='~/.cache' let g:translator_channel='baidu' let g:translator_target_lang ='zh' " 目标语言为中文 let g:translator_source_lang ='auto' let g:translator_outputype='popup' " 普通模式翻译光标所在单词 noremap <leader>tc :<C-u>Tc<CR> " 可视模式翻译选中内容 vnoremap <leader>tv :<C-u>Tv<CR> autocmd FileType man setlocal readonly -
保存退出
.vimrc使配置生效重新进入man页面即可看到高亮显示,翻译功能需输入PackUpdate按q推出再输入更新插件
10、Linux中更改静态IP(非DHCP环境下)
Rocky
①、命令行(掌握)
nmcli con mod 连接名(ens160) ipv4.method manual
nmcli con mod 连接名(ens160) ipv4.addresses 10.0.0.20/24
nmcli con mod 连接名(ens160) ipv4.gateway 10.0.0.2
nmcli con mod 连接名(ens160) ipv4.dns "8.8.8.8,114.114.114.114"
nmcli con mod 连接名(ens160) ipv4.ignore-auto-dns yes
#重启网络连接配置使其生效(没有立即生效再打命令)
nmcli con down ens160 && nmcli con up ens160
②、修改NetworkManager配置文件(掌握)
1、进入配置文件目录并编辑对应文件
cd /etc/NetworkManager/system-connections
vim 连接名.nmconnection
2、修改 | 添加 [ipv4] 段内容
[ipv4]
method=manual
addresses=10.0.0.20/24,10.0.0.2
dns=8.8.8.8,114.114.114.114;
ignore-auto-dns=yes
3、保存并重启网络服务
systemctl restart NetworkManager #重启服务之后reboot主机
reboot
③、文本界面nmtui(推荐)
#1、启动nmtui
nmtui
#2、根据节面内容设置
#3、保存后退出,重启网络连接
nmcli con down 连接名 && nmcli con up 连接名
openEuler
编辑配置文件(掌握)
同rocky
Ubuntu
①、修改配置文件(推荐)
# 首先查查询网卡名称,Ubuntu为ens33
ip a
#配置文件路径为
cd /etc/netplan/
ls查看该目录下有.yaml文件
#自行再创建一个00-installer-config.yaml
vim 00-installer-config.yaml
#输入以下内容
network:
version: 2
ethernets:
ens33:
addresses:
- "10.0.0.19/24"
nameservers:
addresses:
- 10.0.0.2
search: []
routes:
- to: "default"
via: "10.0.0.2"
②、使用 NetworkManager (nmcli)
nmcli connection modify "Wired connection 1" \
ipv4.addresses "10.0.0.19/24" \
ipv4.gateway "10.0.0.1" \
ipv4.dns "8.8.8.8 114.114.114.114" \
ipv4.method manual \
connection.autoconnect yes
# 激活链接
nmcli connection up "static-ens33"
③、文本界面nmtui

#需下载net
apt install network-manager -y
nmtui
11、在Linux中安装man中文环境
①、Rocky
export https proxy=http://101.35.250.82:18888
# 编译工具OpenCC的安装
git clone https://github.com/BYVoid/OpenCC.git
chown -R $USER:$USER OpenCC
cd OpenCC/
mkdir build
cd build
# 需要安装 cmake, gcc-c++, python3
# yum install -y cmake gcc-c++ python3
cmake -DCMAKE_BUILD_TYPE=Release ..
make -j"$(nproc)"
make install
# 把 /usr/local/lib(或 lib64)加入动态库搜索路径
bash -c 'cat >/etc/ld.so.conf.d/opencc.conf <<EOF
/usr/local/lib
/usr/local/lib64
EOF'
ldconfig
# 查看
ldconfig -p | grep opencc
# GitHub 项目: man-pages-zh :
git clone https://github.com/man-pages-zh/manpages-zh
cd manpages-zh
mkdir build
cd build
cmake ..
make
make install
②、Ubuntu
- 安装中文语言包
apt install language-pack-zh-hans language-pack-zh-hans-base
- 验证:
dpkg -l | grep language-pack-zh
- 安装中文 man 文档
apt install manpages-zh
浙公网安备 33010602011771号