时间:2026-01-06 09:37
人气:
作者:admin
vim /etc/ssh/sshd_config
# 允许root登录(学习环境)
PermitRootLogin yes
# 生产环境建议使用密钥登录,禁用密码登录
# PasswordAuthentication no
# PubkeyAuthentication yes
systemctl restart sshdS
# 检查SSH状态
systemctl status sshd
# 安装OpenSSH服务端
apt install openssh-server
# 启动SSH服务
systemctl start ssh
# 设置root密码
passwd root
编辑 /etc/ssh/sshd_config 并重启服务:
systemctl restart ssh
# 修改默认端口(可选)
Port 2222
# 禁用空密码
PermitEmptyPasswords no
# 限制最大尝试次数
MaxAuthTries 3
# 设置空闲超时
ClientAliveInterval 300
ClientAliveCountMax 2
status ufw
ufw start/stop
ufw enable/disable
查看防火墙状态
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".
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#中科院
格式:
提示符格式为:[\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 #使其生效
注意
将root用户主目录下的bin文件夹添加到系统的可执行文件搜索路径,如果你在 `~/bin` 中放置了自定义脚本或程序(如 `mytool`),可以直接在终端输入命令名运行,而无需指定完整路径(例如 `./mytool`)。
sudo vim /etc/bash.bashrc
export PATH=$PATH:~/bin
source /etc/bash.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
hostnamectl set-hostname <主机名>
exit 生效
# 查看当前时区
timedatectl status
# 设置上海时区
timedatectl set-timezone Asia/Shanghai
# 验证
date
# Ubuntu
sudo apt install chrony
sudo systemctl enable --now chrony
# Rocky/CentOS
sudo yum install chrony
sudo systemctl enable --now chronyd
# 查看同步状态
chronyc sources -v
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),可关闭当前用户的所有登录提示(优先级高于系统级配置)。cd /etc/update-motd.d/
ls -l
sudo chmod -x 10-help-text 50-motd-news
sudo chmod -x /etc/update-motd.d/*
sudo nano /etc/ssh/sshd_config
ini
PrintMotd no
sudo systemctl restart sshd
touch ~/.hushlogin
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
编辑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推出再输入更新插件
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
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
#1、启动nmtui
nmtui
#2、根据节面内容设置
#3、保存后退出,重启网络连接
nmcli con down 连接名 && nmcli con up 连接名
同rocky
# 首先查查询网卡名称,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"
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"

#需下载net
apt install network-manager -y
nmtui
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
apt install language-pack-zh-hans language-pack-zh-hans-base
dpkg -l | grep language-pack-zh
apt install manpages-zh