CentOS常用命令收藏

一:使用CentOS常用命令查看cpu
more /proc/cpuinfo | grep “model name”
grep “model name” /proc/cpuinfo
[root@localhost /]# grep “CPU” /proc/cpuinfo
model name : Intel(R) Pentium(R) Dual CPU E2180 @ 2.00GHz
model name : Intel(R) Pentium(R) Dual CPU E2180 @ 2.00GHz
如果觉得需要看的更加舒服
grep “model name” /proc/cpuinfo | cut -f2 -d:

二:使用CentOS常用命令查看内存
grep MemTotal /proc/meminfo

grep MemTotal /proc/meminfo | cut -f2 -d:

free -m |grep “Mem” | awk ‘{print $2}’

三:使用CentOS常用命令查看cpu是32位还是64位 
查看CPU位数(32 or 64)
getconf LONG_BIT

四:使用CentOS常用命令查看当前linux的版本 
more /etc/redhat-release
cat /etc/redhat-release

五:使用CentOS常用命令查看内核版本 
uname -r
uname -a

六:使用CentOS常用命令查看当前时间 
date上面已经介绍如何同步时间了

七:使用CentOS常用命令查看硬盘和分区 
df -h
fdisk -l
也可以查看分区
du -sh
可以看到全部占用的空间
du /etc -sh
可以看到这个目录的大小

八:使用CentOS常用命令查看安装的软件包 
查看系统安装的时候装的软件包
cat -n /root/install.log
more /root/install.log | wc -l
查看现在已经安装了那些软件包
rpm -qa
rpm -qa | wc -l
yum list installed | wc -l
不过很奇怪,我通过rpm,和yum这两种方式查询的安装软件包,数量并不一样。没有找到原因。

九:使用CentOS常用命令查看键盘布局
cat /etc/sysconfig/keyboard
cat /etc/sysconfig/keyboard | grep KEYTABLE | cut -f2 -d=

十:使用CentOS常用命令查看selinux情况 
sestatus
sestatus | cut -f2 -d:
cat /etc/sysconfig/selinux

十一:使用CentOS常用命令查看ip,mac地址
在ifcfg-eth0 文件里你可以看到mac,网关等信息。
ifconfig
cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep IPADDR
cat /etc/sysconfig/network-scripts/ifcfg-eth0 | grep IPADDR | cut -f2
-d
=

ifconfig
eth0 |grep “inet addr:” |awk ‘{print $2}’|cut -c 6-

ifconfig | grep ‘inet addr:’| grep -v ’127.0.0.1′ | cut -d: -f2 | awk ‘{ print $1}’

查看网关

cat /etc/sysconfig/network

查看dns

cat /etc/resolv.conf

十二:使用CentOS常用命令查看默认语言
echo $LANG $LANGUAGE
cat /etc/sysconfig/i18n

十三:使用CentOS常用命令查看所属时区和是否使用UTC时间 
cat /etc/sysconfig/clock

十四:使用CentOS常用命令查看主机名
hostname
cat /etc/sysconfig/network
修改主机名就是修改这个文件,同时最好也把host文件也修改。

十五:使用CentOS常用命令查看 系统资源使用情况 ( 开机运行时间 ) 
uptime
09:44:45 up 67 days, 23:32, …
看来刚才确实是网段的问题,我的机器还是67天前开机的。
#系统资源使用情况

vmstat 1 -S m

procs ———–memory———- —swap– —–io—- –system– —–cpu——

r b swpd free buff cache si so bi bo in cs us sy id wa st

0 0 0 233 199 778 0 0 4 25 1 1 3 0 96 0 0

0 0 0 233 199 778 0 0 0 0 1029 856 13 1 86 0 0

十六:实用命令
wget 网址 下载资源
tar zxvf 压缩包名称 解压
hostname or cat /etc/sysconfig/network 查看主机名
pkill mysqld 如何杀死mysql进程
find / -type f -size +100000k -ls 查询大小超过100M的文件

十七:CentOS文件常用命令
创建/改变文件系统的CentOS常用命令

NO1. 创建文件系统类型
[root@rehat root]# umount /dev/sdb1
[root@rehat root]# mkfs -t ext3 /dev/db1
[root@rehat root]# mount /dev/sdb1 /practice

改变文件或文件夹权限的CentOS常用命令

chmod
NO1. 将自己的笔记设为只有自己才能看
[root@rehat root]# chmod go-rwx test.txt
或者
[root@rehat root]# chmod 700 test.txt
NO2. 同时修改多个文件的权限
[root@rehat root]# chmod 700 test1.txt test2.txt
NO3. 修改一个目录的权限,包括其子目录及文件
[root@rehat root]# chmod 700 -R test

改变文件或文件夹拥有者的CentOS常用命令

chown 该命令只有 root 才能使用
NO1. 更改某个文件的拥有者
[root@rehat root]# chown jim:usergroup test.txt
NO2. 更改某个目录的拥有者,并包含子目录
[root@rehat root]# chown jim:usergroup -R test

查看文本文件内容的CentOS常用命令

cat
NO1. 查看文件内容,并在每行前面加上行号
[root@rehat root]# cat -n test.txt
NO2. 查看文件内容,在不是空行的前面加上行号
[root@rehat root]# cat -b test.txt
NO3. 合并两个文件的内容
[root@rehat root]# cat test1.txt test2.txt > test_new.txt
NO4. 全并两具文件的内容,并追回到一个文件
[root@rehat root]# cat test1.txt test2.txt >> test_total.txt
NO5. 清空某个文件的内容
[root@rehat root]# cat /dev/null > test.txt
NO6. 创建一个新的文件
[root@rehat root]# cat > new.txt 按 CTRL + C 结束录入

编辑文件文件的CentOS常用命令

vi
NO1. 新建档案文件
[root@rehat root]# vi newfile.txt
NO2. 修改档案文件
[root@rehat root]# vi test.txt test.txt 已存在
NO3. vi 的两种工作模式:命令模式,编辑模式
NO4. 进入 vi 后为命令模式,按 Insrt 键进入编辑模式
按 ESC 进入命令模式,在命令模式不能编辑,只能输入命令
NO5. 命令模式常用命令
:w 保存当前文档
:q 直接退出 vi
:wq 先保存后退出 。

十八:批量替换文件
今天使用svn进行系统迁移,结果发现最初的路径写错了,导致无法访问源服务器,查看 .svn/entries 大致了解了一下里面的内容。重新迁移时间太久了,还是直接把文件替换掉吧

for f in $(find ./ -type f -name ‘entries’)
do
sed -i “s/202\.68\.134\.18/202\.68\.134\.34/g” $f
done

sed 简单说明:

sed “s/sourcestring/newstring/g” $f

把 $f 文件中的 sourcestring 换成 newstring,输出到终端。s 表示搜索替换,/g表示全局。

sed -i $f

表示直接在 $f 中修改。

sed -iback $f

表示修改后的文件另存为 $fback

sed 中所有正则表达式都必须使用严格的转义符 \ 来限定

sed 的正则比较严格: ” \ / ! 都需要分别用 \” \/ \\ \! 转义。

\n 表示换行

十九. shell 变量 字符串操作
mono 跑在linux下时,apache+mod_mono有时候需要加载的 Assembly 必须配置在 GAC 中,下面是一个脚本完成此功能

cd bin

for f in $(find ./ -name “*.dll”)

do

gacutil -i $f

done

如果要从 GAC 中批量卸载这些 Assembly, 可以如下

for f in $(ls *.dll)

do

gacutil -u ${f%%.dll}

done

其中就用到了字符串变量的替换, ${f%%.dll}

${f%%.dll} 的意义为 删除 $f 变量 .dll 及之后的所有内容

相关的变量操作还有:

${f##.} 等,后面再补充

二十、 查看当前连接

netstat -an

二十一、有关重启
shutdown -r now   重新启动系统,使设置生效
shutdown -h now 关机
reboot 重启
poweroff 关机

二十二、开机自启动设置
编辑rc.local文件
#vim /etc/rc.d/rc.local

# du -sh # 查看指定目录的大小

# uptime # 查看系统运行时间、用户数、负载

# cat /proc/loadavg # 查看系统负载

# iptables -L # 查看防火墙设置
# route -n # 查看路由表
# netstat -lntp # 查看所有监听端口
# netstat -antp # 查看所有已经建立的连接
# netstat -s # 查看网络统计信息

# w # 查看活动用户
# id # 查看指定用户信息
# last # 查看用户登录日志
# cut -d: -f1 /etc/passwd # 查看系统所有用户
# cut -d: -f1 /etc/group # 查看系统所有组
# crontab -l # 查看当前用户的计划任务
# chkconfig –list # 列出所有系统服务
# chkconfig –list | grep on # 列出所有启动的系统服务

猜你喜欢

转载自whxhz.iteye.com/blog/1322133