修改系统时间:
- 自动同步:
- 修改时区:
\cp -a /usr/share/zoneinfo/PRC /etc/localtime
- 安装ntpdate(端口:udp 123):
yum -y install ntp
- 同步时间命令:
/usr/sbin/ntpdate us.pool.ntp.org
- 加入计划任务(每10分钟同步一次):
*/10 * * * * /usr/sbin/ntpdate us.pool.ntp.org(国外) */10 * * * * /usr/sbin/ntpdate cn.pool.ntp.org(国内)
- 修改时区:
- 手动修改:
- 更改系统时间:
date -s "2017-12-18 09:40:00"
- 写入系统时间:
hwclock -w
- 更改系统时间:
- 自动同步:
修改主机名:
- 永久修改:
vi /etc/hostname
- 临时修改:
hostname lee
- 查看:
hostname
- 永久修改:
DNS配置:
- 静态IP-域名映射:
vim /etc/hosts
xx.xx.xx.xx www.test.com
- 配置域名解析服务器:
vim /etc/resolv.conf
nameserver 8.8.8.8 nameserver 114.114.114.114
- 静态IP-域名映射:
修改系统语言:
- 永久:
vi /etc/profile,添加
export LANG="zh_CN.UTF-8" 或 export LANG="en_US.UTF-8"
- 临时:
命令行下
LANG="zh_CN.UTF-8"
- 永久:
别名设置:
- 添加:
alias ll='ls -lah --color'
- 删除:
unalias ll
- 添加:
yum 常见操作:
- yum 源存储位置:
/etc/yum.repos.d
- 下载epel-release:
yum -y install epel-release
- 列出软件的所有可用版本:
yum list [软件] --showduplicates
- 安装指定版本:
yum -y install [软件]-[版本]
- 查看软件详细信息:
yum info [软件]-[版本]
- 清除缓存:
yum clean all yum makecache
- 跳过 gpg校验:
yum [命令] --nogpgcheck
- yum 源存储位置:
vim配置:
- 配置:
vim ~/.vimrc
set nocompatible set number set ruler set wrap set showcmd set history=1000 set nobackup set noswapfile "set cursorline "set cursorcolumn set showmatch set autoindent set cindent syntax enable syntax on set t_Co=256 set ignorecase "set mouse=a set tabstop=4 set shiftwidth=4 set softtabstop=4 set smarttab set expandtab set encoding=utf-8 set fileencodings=ucs-bom,utf-8,cp936,gb18030,big5,euc-jp,euc-kr,latin1 set ff=unix filetype on filetype indent on filetype plugin on filetype plugin indent on
- 修改默认编辑器:
vim /etc/profile
export EDITOR=/usr/bin/vim
- 配置:
安装网络工具:
- 网卡诊断:
yum -y install net-tools
- 路由诊断:
yum -y install bind-utils
- 网卡诊断:
安装开发者工具:
yum -y groupinstall "development tools"
关闭selinux:
- 修改配置文件:
vim /etc/selinux/config
或将 SELINUX=enforcing 改为 SELINUX=disabled
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
- 临时生效:
setenforce 0
- 修改配置文件:
移除firewalld、添加iptables:
- 移除firewalld:
yum -y remove firewalld
- 添加iptables:
yum -y install iptables iptables-services
- 启用iptables:
systemctl enable iptables
- 移除firewalld:
修改ssh端口:
- 打开配置文件:
vim /etc/ssh/sshd_config
- 找到port:
Port 22
- 打开配置文件:
修改欢迎语:
vim /etc/motd
Happy codding!!!
软链接操作:
- 创建:
# 将 /home/lee/go 目录链接到当前 ln -s /home/lee/go ./go
- 删除:
# 注意,后面没有 / rm -rf ./go
- 修改:
# 将之前创建的 /home/lee/go 的指向替换为 /home/lee/golang ln -snf /home/lee/golang ./go
- 创建:
升级内核:
- 导入公共密钥:
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
- 安装 yum 源:
yum install -y https://www.elrepo.org/elrepo-release-7.el7.elrepo.noarch.rpm
- 替换为国内的 yum 源(可选):
sed -i "s/mirrorlist=/#mirrorlist=/g" /etc/yum.repos.d/elrepo.repo sed -i "s#elrepo.org/linux#mirrors.tuna.tsinghua.edu.cn/elrepo#g" /etc/yum.repos.d/elrepo.repo
- 查看可用的内核版本:
yum --disablerepo="*" --enablerepo="elrepo-kernel" list available
- 升级为长期支持(lt)版本:
yum -y --enablerepo=elrepo-kernel install kernel-lt
- 查看可用内核版本及启动顺序:
awk -F\' '$1=="menuentry " {print i++ " : " $2}' /boot/grub2/grub.cfg
- 查看启动顺序:
yum -y install grub2-pc grub2-editenv list
- 设置开机启动:
grub2-set-default 0
- 重启:
reboot
- 检查内核是否升级成功:
uname -smr
- 导入公共密钥:
设置密码:
passwd
允许 ssh 通过密码登录:
vim /etc/ssh/sshd_config
PermitRootLogin yes PasswordAuthentication yes
systemctl restart sshd
linux 常用命令:
- 查看 centos 系统版本:
cat /etc/redhat-release
- 查看 linux 发型版本:
lsb_release -a
- 查看 linux 内核、版本、架构(32位/64位):
uname -smr # amd/arm dpkg --print-architecture
- 查看 cpu 使用率 –
htop
:# 安装 yum -y install htop # 使用 htop
- 查看 cpu 核心数:
cat /proc/cpuinfo| grep "cpu cores"| uniq
- 查看 内存 使用率:
# 以 MB 为单位显示内存 free -m
- 查看 磁盘 使用率:
df -h
- 统计 文件/文件夹 占用磁盘大小:
du -sh 文件/文件夹
- 统计 文件总行数:
wc -l 文件名
- 根据启动命令 显示进程号:
ps -aux | grep nginx
- 杀死进程:
# 根据命令 杀死进程 pkill nginx # 根据进程号 杀死进程 kill -9 进程号(pid)
- 根据 端口号 查找命令(需要安装 net-tools):
netstat -nutlp | grep 80 或 lsof -i:80
- 下载文件:
wget http://www.test.com/xx.jpg -O xx.jpg
- 压缩/解压文件:
- 压缩:
# 仅压缩 tar -czf xxx.tar.gz /path/to/dir # 压缩并移除原文件夹 tar -czf xxx.tar.gz /path/to/dir --remove-files
- 解压:
# 解压到当前目录 tar -xzf xxx.tar.gz # 解压到指定目录 tar -xzf xxx.tar.gz -C /path/to/dir
- 压缩:
- 写入多行内容到文件:
# ">":写入;">>":追加 cat << EOF >> test.txt 第一行 第二行 ... EOF
- 批量删除指定后缀的文件:
find . -name "*.meta" | xargs rm -rf
- 找出系统中大于20兆的文件:
find / -size +20M
- 查看服务运行日志:
journalctl -f -u nginx
- 以其他用户的身份执行命令:
su otherUser -l -c "/run/some/command"
- curl 代理设置:
export http_proxy=http://x.x.x.x:1087;export https_proxy=http://x.x.x.x:1087;
- 释放内存:
swapoff -a swapon -s swapon -a echo 3 > /proc/sys/vm/drop_caches
- 关闭防火墙:
systemctl stop firewalld && systemctl disable firewalld
- 关闭 selinux:
setenforce 0 && sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
- 查看 centos 系统版本:
文档更新时间: 2024-04-20 10:57 作者:lee