目录
前言
随着大数据时代的来临,人们对虚拟机的使用也逐渐增加。在使用过程中很多linux命令容易忘记。这里我整理了部分常用命令与使用参数配置。希望对大家学习linux命令有所帮助。
关机与重启命令
命令 | 作用 |
shutdown -h now | 即刻关机 |
shutdown -h 10 | 10分钟后关机 |
shutdown -h 13:00 | 指定时间关机(13:00) |
shutdown -h +10 | 指定时间关机(10分钟后) |
shutdown -c | 取消指定事件关机 |
shutdown -r now | 重启 |
shutdown -r 10 | 10分钟后重启 |
正常关机与重启命令
命令 | 作用 |
reboot | 重启 |
init 6 | 重启 |
init 0 | 关机 |
telinit | 关机 |
poweroff | 关机 |
halt | 关机 |
常用系统服务命令
命令 | 作用 |
chkconfig --list | 列出系统服务 |
service<服务名>status service<服务名>start service<服务名>stop service<服务名>restart | 查看某个服务 启动某个服务 终止某个服务 重启某个服务 |
systemctl status<服务名> systemctl start<服务名> systemctl stop<服务名> systemctl restart<服务名> systemctl enable<服务名> systemctl disable<服务名> | 查看某个服务 启动某个服务 终止某个服务 重启某个服务 开机自启动 关闭自启动 |
文件与目录操作命令
命令 | 作用 |
cd<目录名> cd .. cd ../.. cd cd - | 进入某个目录 回上级目录 回退两级目录 进入个人主目录 回到上一步所在的目录 |
pwd | 显示当前路径 |
ls ls -F ls -I ls -a ls -lh ls -ISr | 查看文件目录列表 查看目录中内容 查看文件和目录的详细列表 查看隐藏文件 查看文件和目录的详情列表 查看文件和目录列表 |
tree | 查看文件和目录的树形结构 |
mkdir<目录名> mkdir dir1 dir2 mkdir -p /tmp/dir1/dir2 | 创建目录 同时创建连个目录 创建目录树 |
rm -f file1 rmdir dir1 rm -rf dir1 rm -rf dir1 dir2 mv old_dir new_dir | 删除file1文件 删除dir目录 删除dir目录及其内容 同时删除两个目录及其内容 重命名/移动目录 |
cp file1 file2 cp dir/* . cp -a dir1 dir2 cp -a /tmp/dir1 . | 复制文件 复制某目录下的所有文件至当前目录 复制目录 复制一个目录至当前目录 |
ln -s file1 link1 ln file1 link1 | 创建指向文件/目录的软连接 创建指向文件/目录的物理连接 |
find / -name file1 find / -user user1 find /dir -name *.bin | 从根目录开始搜索文件/目录 搜索用户user1的文件/目录 在目录/dir中搜带有.bin后缀的文件 |
locate<关键词> locate *.mp4 | 快速定位文件 寻找.mp4结尾的文件 |
whereis<关键词> which<关键词> | 显示某二进制文件/可执行文件的路径 查看系统目录下某的二进制文件 |
chmod ugo+rwx dir1 | 设置目录所有者(u)、群组(g)及其他人(o)的读(r)写(w)执行(x)权限 |
chmod go-rwx dir1 chown user1 file1 chown -R user1 dir1 chgrp group1 file1 chown user1:group1 file1 | 移除群组(g)与其他人(o)对目录的读写权限 改变文件的所有者属性 改变目录的所有者属性 改变文件群组 改变文件的所有人和群组 |
文件查看和处理命令
命令 | 作用 |
cat file1 cat -n file1 cat xxx.txt | 查看文件内容 查看内容并标示行数 查看文本文件内容 |
tac file1 more file1 less file1 head -2 file1 tail -2 file1 tail -f /log/msg | 从最后一行开始反看文件内容 查看一个长文件的内容 类似more命令,但允许反向操作 查看文件前两行 查看文件后两行 实时查看添加到文件中的内容 |
grep code hello.txt grep ^code hello.txt grep [0-9] hello.txt | 在文件hello.txt中查找关键词code 在文件hello.txt查看以code开头的内容 在文件hello.txt文件中所有有数字的行 |
sed 's/s1/s2/g' hello.txt
sed '/^$/d' hello.txt
sed '/ *#/d; /^$/d' hello.txt
sed -e '1d' hello.txt
sed -n '/s1/p' hello.txt
sed -e 's/ *$//' hello.txt
sed -e 's/s1//g' hello.txt
sed -n '1,5p;5q' hello.txt
sed -n '5p;5q' hello.txt
| 将hello.txt文件中的s1替换成s2 从hello.txt文件中删除所有的空白行 hello.txt文件中删除所有注释和空白行 从文件hello.txt中排出第一行 查看只包含关键词s1的行 删除每一行最后的空白字符 从文件中只删除词汇s1 查看从第一行到第5行内容 查看第五行的内容 |
paste file1 file2 paste -d'+'file1 file2 | 合并两个文件或两栏的内容 合并两个文件的内容,中间用+区分 |
sort file1 file2 | 排序两个文件的内容 |
comm -1 file1 file2 comm -2 file1 file2 comm -3 file1 file2 | 比较两个文件的内容(去除file1的内容) 比较两个文件的内容(去除file2的内容) 比较两个文件的内容(去除两个文件中共有内容) |
打包和解压命令
命令 | 作用 |
zip xxx.zip file zip -r xxx.zip file1 file2 dir1 | 压缩至zip包 将多个文件与目录压成zip包 |
unzip xxx.zip | 解压zip包 |
tar -cvf xxx.tar file
tar -cvf xxx.tar file1 file2 dir1
tar -tf xxx.tar
tar -xvf xxx.tar
tar -xvf xxx.tar -C /dir
tar -cvfj xxx.tar.bz2 dir
tar -jxvf xxx.tar.bz2
tar -cvfz xxx.tar.gz dir
tar -zxvf xxx.tar.gz
|
创建⾮压缩tar包
将多个⽂件+⽬录打tar包
查看tar包的内容
解压tar包
将tar包解压⾄指定⽬录
创建bz2压缩包
解压bz2压缩包
创建gzip压缩包
解压gzip压缩包
|
bunzip2 xxx.bz2
bzip2 filename
gunzip xxx.gz
gzip filename
gzip -9 filename
|
解压bz2压缩包
压缩⽂件
解压gzip压缩包
压缩⽂件
最⼤程度压缩
|
RPM包管理命令
命令 | 作用 |
rpm -qa rpm -q pkg_name rpm -q --whatprovides xxx rpm -q --whatrequires xxx rpm -q --changelog xxx | 查看已安装的rpm包 查询某个rpm包 显示xxx功能是由哪个包提供的 显示xxx功能被哪个程序包依赖的 显示xxx包的更改信息 |
rpm -qi pkg_name
rpm -qd pkg_name
rpm -qc pkg_name
rpm -ql pkg_name
rpm -qf filename
rpm -qR pkg_name
| 查看一个包的详细信息 查询一个包所提供的的文档 查看已安装rpm包提供的配置文件 查看一个包安装了哪些文件 查看某个文件属于哪个包 查询包的依赖关系 |
rpm -ivh xxx.rpm
rpm -ivh --test xxx.rpm
rpm -ivh --nodeps xxx.rpm
rpm -e xxx
rpm -Fvh pkg_name
rpm -Uvh pkg_name
rpm -V pkg_name
| 安装rpm包 测试安装rpm包 安装rpm包时忽略依赖关系 卸载程序包 升级确定已安装的rpm包 升级rpm包(若未安装则会安装) RPM包详细信息检验 |
系统信息与系统性能的命令
命令 | 作用 |
uname -a uname -f uname -m | 查看内核/OS/CPU信息 查看内核版本 查看处理器架构 |
hostname | 查看计算机名 |
who who am i whoami | 显示当前登录系统的用户 显示登录时的用户名 显示当前用户名 |
cat /proc/version cat /proc/cpuinfo cat /proc/interrupts cat /proc/loadavg | 查看linux版本信息 查看CPU信息 查看中断 查看系统负载 |
uptime env | 查看系统运行事件、用户数、负载数量 查看系统环境变量 |
lsusb -tv lspci -tv lsmod | 查看系统USB设备信息 查看系统PCI设备信息 查看已加载的系统模块 |
grep MemTotal /proc/meminfo grep MemFree /proc/meminfo free -m | 查看内存总量 查看空闲内存量 查看内存用量与交换区用量 |
date cal x(x可以为任意年份) | 显示系统日期事件 显示x年日历表 |
top vmstat 1 20 iostat | 动态显示cpu/内存/进程等情况 每一秒采一次系统状态,采20次。次数与事件可以调整 查看io读写/cpu使用情况 |
sar -u 1 10 sar -d 1 10 | 查看cpu使用情况(1秒一次,共10次)参数可以修改 查看磁盘性能(1秒一次,共10次)参数可以修改 |
磁盘和分区常用命令
命令 | 作用 |
fdisk -l swapon -s | 查看所有磁盘分区 查看所有交换分区 |
df -h df -hl du -sh /dir du -sk * | sort -rn | 查看磁盘使用情况及挂载点 查看磁盘使用情况及挂载点 查看指定某个目录的大小 从高到底依次显示文件和目录大小 |
mount /dev/hda2 /mnt/hda2 mount -t ntfs /dev/sdc1 /mnt/usbhd1 mount -o loop xxx.iso /mnt/cdrom mount /dev/sda1 /mnt/usbdisk | 挂载hda2盘 指定文件系统类型挂载(如ntfs) 挂载iso文件 挂载USB盘/闪存设备 |
umount -v /dev/sda1 umount -v /mnt/mymnt fuser -km /mnt/hda1 | 通过设备名卸载 通过挂载点卸载 强制卸载(谨慎使用) |
用户与用户组常用命令
命令 | 作用 |
useradd codesheep userdel -r codesheep usermod -g group_name user_name usermod -aG group_name user_name usermod -s /bin/ksh -d/home/codepig -g dev codesheep | 创建用户 删除用户 修改用户的组 将用户添加到组 修改用户codesheep的登录Shell、主目录以及用户组 |
groups test groupadd froup_name groupdel group_name groupmod -n new_name old_name | 查看test用户所在的组 创建用户组 删除用户组 重命名用户组 |
su - user_name | 切换到一个用户环境 |
passwd passwd codesheep | 修改口令 修改某用户的口令 |
w | 查看活动用户 |
id codesheep | 查看指定用户codesheep信息 |
last | 查看用户登录日志 |
crontab -l cut -d: -f1 /etc/passwd cut -d: -f1 /etc/group | 查看当前用户的计划任务 查看系统所有用户 查看系统所有组 |
网络和进程管理命令
命令 | 作用 |
ifconfig ifconfig eth0 route -n | 查看网络接口属性 查看某网卡的配置 查看路由表 |
netstat -lntp netstat -antp netstat -lutp | 查看所有监听端口 查看已经建立的TCP连接 查看TCP/UDP的状态信息 |
ifup eth0 ifdown eth0 iptables -L | 启用eth0网络设备 禁用eth0网络设备 查看iptables规则 |
ifconfig eth0 192.168.1.1 netmask
255.255.255.0
| 配置eth0ip地址为192.168.1.1 |
dhclient eth0 | 以dhcp模式启用eth0 |
route add -net 0/0 gw Gateway_IP
route add -net 192.168.0.0 netmask
255.255.0.0 gw 192.168.1.1
route del 0/0 gw Gateway_IP
| 配置默认网关 配置静态路由到达网络192.168.0.0/16’ 删除静态路由 |
host www.codesheep.cn nslookup www.codesheep.cn | 解析主机名 查询DNS记录,查看域名解析是否正常 |
ps -ef ps -ef | grep codesheep | 查看所有进程 过滤出你需要的进程(codesheep为参数) |
kill -s name kill -s pid | kill指定名称的进程 kill指定pid的进程 |
YUM包管理命令
命令 | 作用 |
yum repolist enabled yum search pkg_name yum install pkg_name
yum install --downloadonly pkg_name
| 显示可用的源仓库 搜索软件包 下载并安装软件包 只下载不安装软件包 |
yum list
yum list installed
yum list updates
yum check-update
yum updateate
yum update pkg_name
| 显示所有程序包 查看当前系统已安装包 查看可以更新的包列表 查看看可升级的软件包 更新所有软件包 升级指定软件包 |
yum deplist pkg_name
yum remove pkg_name
yum clean all
yum clean packages
yum clean headers
| 列出软件包依赖关系 删除软件包 清除缓存 清除缓存的软件包 清除缓存的header |
APT软件工具命令
命令 | 作用 |
apt-cache search pkg_name apt-cache show pkg_name apt-get install pkg_name apt-get purge pkg_name apt-get remove pkg_name | 搜索程序包 获取包的概览信息 安装/升级软件包 卸载软件(包括配置信息) 卸载软件(不包括配置信息) |
apt-get update apt-get upgrade apt-get clean | 更新包索引信息 更新已安装软件包 清理缓存 |