>
或
>>
)将输出写入文件,通过
tee
命令实时保存日志,或调用
systemctl enable
持久化服务配置,部分工具如
iptables-save
可导出防火墙规则,编辑文件后需用
:wq
(vim)等保存退出,确保变更生效。
在服务器管理中,保存命令的执行结果、配置变更记录或操作日志是运维工作的核心需求之一,合理保存这些信息不仅便于故障排查,还能用于审计、回滚及团队协作,以下从操作记录留存、输出重定向、会话管理等场景出发,详细介绍实用方法。
command > output.log
ls -l /var/www > directory_list.txt
command >> output.log
date >> server_status.log
free -h >> server_status.log
command &> full.log
tee
命令command | tee file.log
dmesg | tee kernel_messages.log
command | tee -a file.log
script
命令记录全会话script -a session.log
此后所有操作(包括输入和输出)会被记录到 session.log
,退出时输入 exit
。
script -a --timing=time.log session.log
生成带时间标记的日志,便于复盘操作时序。
screen -L -Logfile screen.log
日志默认保存为 screenlog.0
。
Ctrl+B
后输入 :capture-pane -S -300
可捕获最近300行内容,通过 save-buffer log.txt
保存。history
记录history
history > command_history_$(date +%F).txt
编辑 ~/.bashrc
文件,添加以下配置:
export HISTTIMEFORMAT="%F %T " # 记录时间 export HISTSIZE=10000 # 内存中保存的条数 export HISTFILESIZE=20000 # 文件保存的条数 export HISTCONTROL=ignoredups # 忽略重复命令
生效配置:
source ~/.bashrc
cp httpd.conf httpd.conf.bak_$(date +%s)
rsync
增量备份: rsync -av /etc/nginx/ /backup/nginx_config/
cd /etc/nginx
git init
git add .
git commit -m "Initial config"
git diff
git commit -a -m "Updated SSL settings"
/var/log
通常需 sudo
)。 logrotate
)。 600
。引用说明
man script
、man tee
)