linux命令使用小技巧

获取相对路径

bin=$(dirname ${BASH_SOURCE-$0})

bin=$(cd $bin; pwd)

使用小技巧

 getconf LONG_BIT

获取cpu位数

获取block大小

 tune2fs -l /dev/sda1

获取系统架构

arch

查询与设置硬件时钟

查看pci设备

lspci

设置pci

setpci

查看usb设备

lsusb

弹出可移动媒体

eject 

查看磁盘使用情况

df (-T 显示挂载类型,-h 以容易阅读的方式显示磁盘空间的使用情况,-B<块大小> 指定显示时的块大小)

查看登录系统的用户信息

last

列出所有系统服务状态

service –status-all

查看日历

cal (-y 指定年份)

十六进制的当前主机标识

hostid

交互式计算器

bc

反复输出指定的字符串,直到取消命令的执行

yes (yes test 会一直输出test,也可以yes|echo 333)

关闭交换空间

swapoff

打开交换空间

swapon

强制将缓存区中的数据存入磁盘

sync

修改目录权限为用户组

chgrp

查看系统启动时间

uptime

文件比较

diff

比较两个文件是否有差异

cmp

比较两个有序文件的不同

comm

比较3个文件的不同

diff3

根据diff的比较结果显示统计数字

diffstat

查找文件

查找指令的位置,包括执行文件、源代码、手册页

whereis

在PATH变量指定的路径中搜索某个系统命令的位置

which

文件内容排序

sort

文件crc校验

cksum

计算并显示文件的校验码

sum

查看文件状态

stat

文件合并

paste (paste file1 file2 或者paste -s file1取消换行符)

文件升级

patch (diff file1 file2 > file.patch  ,  patch -p0 file1 file1.patch )

查看文件信息(查看文件编码)

file

查看进程启动时间

ps axo pid,ppid,comm,pmem,lstart | grep 进程id

查看本地化

locale

$$  表示 脚本进程本身 

$# 表示 脚本的参数的个数

$0 表示 脚本自身的名称

$1,$2,… $n 表示 脚本的参数

tee

双重定向

变量$$代表的是当前shell进程的进和id,这里要特别留意“当前shell”

uniq 

去除重复的行

临时修改内核参数

sysctl -w kernel.msgmni=128 

(其他用法

-a   显示所有的系统参数

    -p   从指定的文件加载系统参数,如不指定即从/etc/sysctl.conf中加载

)

永久修改内核参数

/etc/sysctl.conf

设置文件显示的行宽

fold(fold -4 file1)

替换字符

tr( tr a-z A-Z < file1.txt 把所有小写字母换成大写字母)(tr -d 33 < file.txt删除所有的3字符效果和tr -d 3 <file.txt相同)

文件分割

csplit file1 4(按照四行来分割文件)(-n 4 表示文件名用四个字符标识)发

调整c源代码的格式

indent

系统的默认文件权限为644(rw-r–r–)是由umask算出来了,umask的默认值为022,新建文件的权限值为022的补码,可以理解为777-022然后取消可执行权限

umask 000之后,就会发现新建的文件的权限为rw-rw-rw-

检查程序使用了那个端口

netstat -anp|grep 进程id

自动交互方法

1.

ftp -i -n 192.168.167.187 << EOF

user hzc 123456  

pwd  

cd test  

pwd  

close  

bye  

EOF 

2.

#!/bin/bash  

(echo “curpassword”  

sleep 1  

echo “newpassword”  

sleep 1  

echo “newpassword”)|passwd 

3.

#!/usr/bin/expect  

spawn su root  

expect “password: ”  

send “123456\r”  

expect eof  

exit 

新建分区格式

mkfs -t xfs /dev/sdc

删除旧文件,缓存文件

tmpwatch

选项说明

-a  删除任何类型的文件

-f  强制删除文件或者目录,其效果与指令rm的参数”-f”相似

-q  不显示指令的执行过程

-v  详细显示指令执行过程

-test  仅仅只做测试之用,并不真正删除文件或者目录

tmpwatch 24 /tmp/ #删除/tmp目录中超过一天未使用的文件

归档文件

ar指令或者tar指令

压缩

gzip指令或者compress指令或者zip命令

存取归档包中的文件

cpio

创建”.bz2″格式的压缩文件

bzip2

强制gzip格式的文件夹上”.gz”后缀

zforce

显示zip压缩文件详细信息

zipinfo

将.Z文件转换成.gz文件

znew

压缩和解压缩命令lha

压缩可执行程序gzexe

修复损坏的.bz2文件 bzip2recover

解压缩bzip2文件

bunzip2

解压缩gzip压缩的文件

gunzip

解压缩文件到标准输出

bzcat

解压缩.bz2文件并分屏显示内容

bzless/bzmore

解压缩unarj文件unarj

解压缩.Z文件uncompress

解压缩.zip文件unzip

解压缩文件到标准输出zcat

分屏查看文本文件

less more(建议使用less,功能更全)

显示文件中每行的指定内容

cut

  -b  设置输出的字节数或范围

  -c  设置输出的字符数或范围

  -d  指定列(或字段)_的分割字符,默认的分割字符是制表符TAB.只能和-f选项一起使用.

  -f  设置输出的字段

  cut -d: -f 1 /etc/passwd  #指定字段分隔符”:”

以数字编码输出文件内容

od

  -A  设置计算编码的单位,包括a,c,d,f,o,u和x.  a:名称字符,c:ASC II,d:十进制(正负数都包含),f:浮点,o:八进制,u:十进制(只包含正数),x:十六进制

  -j    跳过指定数目的字符

  -N  输出指定的字符数

  -t   输出格式.设置输出格式,字符包括a,c,d,f,o,u和x.

  -v  表示数据时,显示重复的数据.

  -w  用来设置每一行的最大字数

  od  -t o testfile #以八进制显示文件内容

反序显示文件内容(文件内容的最后一行先显示,第一行最后显示)

tac

合并文件的行

paste

  -d  用指定的间隔字符取代跳格字符

  -s  顺序地合并一个文件的多行到一行

file1

zhangsan 80

lisi             70

file2

zhangsan 90

lisi             50

paste file1 file2 #合并并显示合并结果

zhangsan 80  zhangsan 90 

lisi             70  lisi             50

paste -s file1 #合并并显示合并结果

将tab转换为空白

expand

expand -t 50 file2 #转换TAB到50个空白字符

将空白转换为TAB

unexpand

最优化文本模式

fmt

用于将指定的文件内容按照指定格式重新编排后,输出到标准输出.若指定的文件名为”-“,则fmt指令会从标准输入读取要格式化的数据.

显示登录用户名

logname

验证用户密码文件的完整性

pwck   (用于验证用户密码文件/etc/passwd和/etc/shadow的完整性,并将验证后的提示信息显示在标准输出设备)

修改用户的配置信息

usermod

  -c  修改用户备注信息

  -d  修改用户宿主目录

  -e  修改用户的有效期

  -f  修改在密码过期后多少天即关闭该账号

  -g  修改用户所属的组

  -G  修改用户所属的附加组

  -l  修改用户名称

  -L  锁定用户密码,使用户无法登录系统

  -s  修改用户登录后所默认使用的shell

  -u  修改用户ID

  -U  解除密码锁定

(passwd -S oracle可以查看用户密码锁定状态)

查看当前登录系统的所有在线用户

users

用户信息查询程序(查询本地主机和远程主机用户信息)

finger

改变用户的finger信息

chfn

  -f  设置真实姓名

  -h  设置在家电话号码

  -o  设置办公室地址

  -p  设置办公室电话号码

  -u  显示帮助信息

chfn -f xiaoli root  #将root的真实姓名改为xiaoli

chfn -h 88888888 root  #为root设置在家电话号码

改变用户登录后的默认shell环境

chsh -s /bin/csh root  #修改root用户的默认shell为/bin/csh

chsh -l  #显示目前可用的shell

管理组文件/etc/group

gpasswd

  -a 向指定组中增加用户

  -A  设置组中的管理员

  -d  从指定组中删除用户

  -r  删除指定组的密码

  -M  向指定组中同时添加多个用户

创建组

groupadd

  -g  指定要创建的组的组ID

  -r   指定创建系统组

Linux系统中一般组的GID大于1000,系统组的GID小于1000,使用”-r”选项可创建GID小于1000的系统组.

删除组

groupdel 

修改组信息

groupmod

  -g  指定新的ID

  -n  指定新的组名称

显示用户所属的组

groups

验证组文件/etc/group的完整性

grpck

群组的影子口令文件.

(Linux系统里的用户和群组密码分别存放在/etc目录下的passwd和group文件中.因系统运作所需,任何人都能够读取他们,这会造成安全上的破绽,影子口令文件将文件内的密码改放在/etc目录下的shadow和gshadow文件内,只允许系统管理者读取,同时把原密码置换为”x”字符.

grpconv  #启用组的影子口令文件

grpunconv  #关闭组的影子口令文件

pwconv  #开启用户影子密码

pwunconv  #关闭用户影子密码

进程初始化控制

(init指令是Linux系统启动后运行的第一个进程,他是所有进程的父进程.使用init命令可以控制Linux系统的运行等级.

init

  运行等级                                         说明

  0                                                     关机

  1或者S或s                                       单用户模式,一般用于系统维护

  2                                                     多用户模式,与等级3相同,但是没有启动nfs服务

  3                                                     完全的多用户模式,包括网络功能,服务器经常默认使用的运行等级

  4                                                     未使用

  5                                                     图形界面,桌面用户经常默认使用的运行等级

  6                                                     重启系统

  参数

  -b                                                    不执行任何脚本程序而直接进入单用户模式

  -s 或-S                                             进入单用户模式

  init  1    #切换到单用户模式

写文件

dd if=/dev/urandom of=test1.txt bs=512 count=10

修改文件的属性

chattr,这项命令可以改变存放在ext2,ext3,ext4,xfs,ubifs,reiserfs,jps等文件系统上的文件或目录属性(vfat不可以)

选项

A  不更新文件的最后访问时间

a  仅在指定目录或文件之后追加数据

c  对文件或目录进行压缩

D  同步目录更新

d  不进行转储

I  不能对文件进行任何修改

-R  递归改变目录及其内容属性

s  将数据块清零并存入磁盘

S  同步更新,对文件执行操作后,将修改结果存到磁盘

u  保留文件在系统内的数据块,在意外删除的情况下,可进行恢复

-V  显示chattr指令执行的详细过程

-v  设置目录或文件的版本

显示文件属性 

lsattr

选项

-R  递归显示目录及其内容的文件属性

-V  显示程序版本信息

-a  列出目录中的所有文件,包括以”.”开头的文件

-d  显示指定目录或文件的名称,而不是其内容

-v  显示文件或目录的版本信息

whereis

locate

which

find的区别  http://my.oschina.net/hosee/blog/505878

which是通过 PATH环境变量 到该路径内查找可执行文件,所以基本的功能是寻找可执行文件 

locate是从数据库中找,数据库大概每天更新一次

find从硬盘上查找,效率比较低

whereis  和find相比,whereis查找的速度非常快,这是因为linux系统会将系统内的所有文件都记录在一个 数据库文件 ( 参考资料1以及大多数文章中都是这样描述的,whereis会在一个数据库文件中查找,在参考资料2中找到这个数据库文件目录/var/lib/slocate/slocate.db,我在服务器中并没有找到这个目录,原因应该是我没有装locate命令,那么whereis到底是怎么查找的呢? 找了很久没有,从参考资料3中有一种个人比较相信的答案,从 /{bin,sbin,etc}  /usr{lib,bin,old,new,local,games,include,etc,src,man,sbin,X386,TeX,g++-include} 

/usr/local/{X386,TeX,X11,include,lib,man,etc,bin,games,emacs} 中查找,也没有去看whereis的源码,如果有确定的可以交流一下 )中,当使用whereis和下面即将介绍的locate时,会从数据库中查找数据,而不是像find命令那样,通过遍历硬盘来查找,效率自然会很高。  

但是 该数据库文件并不是实时更新,默认情况下时一星期更新一次,因此,我们在用whereis和locate 查找文件时,有时会找到已经被删除的数据,或者刚刚建立文件,却无法查找到,原因就是因为数据库文件没有被更新。  

排序文件内容

sort 将文件内容按行排序,从首字母开始比较,输出到标准输出

校验文件的CRC信息

cksum file

计算并显示文件的校验码

sum

检验文件的MD5码

md5sum

显示文件状态

stat

向指定的进程发送信号

pkill  #如 pkill -u root java

以忽略挂起信号方式运行程序

nohup

基于名字查询并显示进程号

pgrep

查找正在运行程序的进程号

pidof

显示当前登录用户

w

全屏方式显示指定命令的输出信息

watch  #watch ls -l /etc

用树形图显示进程的父子关系

pstree

根据名称结束进程

killall

kill -9 进程ID #强制结束进程

设置进程优先级

nice (默认的优先级为10,范围是-20~20,只有管理员才可以设置优先级)

调整进程优先级

renice

配置网络接口

ifcfg  

网络接口的激活和关闭

ifconfig eth0 up 或者ifup eth0

ifconfig eth0 down 或者ifdown eth0

网络接口的参数配置

ifconfig eth0 192.168.0.2 netmask 255.255.255.0

配置默认网关

route add default gw 192.168.0.99

临时修改mac地址

ifconfig eth0 hw ether AA:AA:AA:AA:AA:AA

显示本机ARP缓冲区记录

arp

ip地址计算器(方便计算ip地址对应的掩码,广播地址等)

ipcalc

查看网络状态

netstat

netstat -r  #显示路由表

netstat -a  #显示所有活动的socket连接

netstat -i  #显示所有网络接口状态

追踪数据经过的路由

tracepath

traceroute

tracepath和traceroute指令功能相同,tracepath功能简单,没有任何选项,它能发现路径上的最大传输单元的值.

强大的多功能网络配置工具

ip 

选项

-s  显示详细的信息

-r  不使用主机IP地址,而是使用系统的名字解析器去显示DNS名字

ip link show  #显示设备当前配置

ip -s link show  #显示设备当前详细配置

ip link set eth0 down  #关闭网络接口eth0

ip link set eth0 up  #激活网络接口eth0

ip link set eth0 address AA:AA:AA:AA:AA:AA  #设置eth0的mac地址

ip link set help  #显示可以设置的设备参数选项

ip address add 192.168.0.2/24 dev eth0 label eth0:0  #为eth0添加新的IP地址,并添加别名eth0:0

ip neigh show  #显示本机邻居表

ip neigh add 192.168.0.44 lladdr AA:AA:AA:AA:AA:AA dev eth0 #邻居表增加192.168.0.44,mac地址为AA:AA:AA:AA:AA:AA,邻居表由系统维护,不需要管理员手工操作

ip route show  #显示本机路由表

ip route add 10.88.88.88/16 via 59.69.131.193  #增加路由表条目

Linux系统中内置

iptables -L  #输出filter表  或者iptables -L -t filter

iptables -t filter -A INPUT -s 192.168.0.1 -j DROP  #禁止192.168.0.1访问本机

iptables -t filter -A INPUT -s 192.168.0.2 -p tcp -j ACCEPT

iptables -t filter -A INPUT -s 192.168.0.3 -p tcp -dport 135 -j ACCEPT #允许192.168.0.3通过TCP发送到本地135端口的包

iptables -t filter -A INPUT -s 192.168.0.4 -p ! tcp -j DROP  #禁止192.168.0.4所有发向本地的非TCP协议数据包

iptables -t filter -A INPUT -s 172.16.0.0/16 -p tcp -j DROP  #禁止172.16.0.0子网的所有主机的所有发向本机的tcp协议数据包

配置内核的nat表

echo 1 > /proc/sys/net/ipv4/ip_forward  #打开内核的包转发功能

iptables -t nat -A POSTROUTING -s 192.168.0.1/24 -o eth1 -j MASQUERADE  #此命令没有任何输出信息.”MASQUERADE”是一种特殊的目标,可以伪装内部局域网的IP地址为NAT服务器的外部网络接口的IP地址,以达到网络地址转换的目的.

iptables -L -t nat  #显示nat表

配置端口映射

echo 1 > /proc/sys/net/ipv4/ip_forward

iptables -t nat -A PREROUTING -d 218.28.87.22 -p tcp –dport 80 -j DNAT –to 192.168.0.122:80

将所有发往NAT外网服务器的80端口TCP协议的数据包转发给内网的0.122机器

保存转发表

iptables-save > save.bak

恢复转发表

iptables-restore < save.bak

查看已经加载的内核模块

lsmod

检查内核模块间的依赖关系

depmod

加载内核模块并解决依赖关系

modprobe   可加载指定的两个内核模块,也可以加载一组有依赖关系的模块,modprobe指令会根据depmod指令所产生的模块依赖关系,决定要加载那些模块,若在加载过程中,modprobe指令会卸载整组模块.

加载模块到内核

insmod 负责加载内核的功能模块,Linux有很多功能是通过模块的方式在需要时才载入内核,如此可使内核较为精简,进而提高效率,以及保有较大的弹性.这类可载入的模块,通常是设备驱动程序.

使用insmod指令加载一个模块时,如果这个模块所依赖的模块还没有被加载,则会导致加载失败,所以最好使用modprobe指令代替insmod指令,以自动加载模块所依赖的其他所有模块.

显示内核的输出信息

dmesg  指令用于查看在开机时内核的输出信息以及运行过程中内核的输出信息.这些内核的输出信息被保存在内核的ring buffer缓冲区中(内核运行过程中的一些警告报错信息也会保存这个缓冲区中),dmesg指令可以查看这个缓冲区中的内容.

查看内存使用情况    

free 

显示系统信息

uname 

uname -a  #显示全部系统信息

显示系统运行时间及平均负载

uptime

显示虚拟内存的状态

vmstat

vmstat -d  #显示磁盘信息

实时显示内核的slab缓存信息

slabtop

监视系统平均负载情况

tload

tload /dev/pts/6  #在6号网络终端显示系统平均负载

报告cpu,i/o设备及分区状态

iostat

iostat -x  #显示详细信息

iostat -k  #以kb为单位显示输出信息

运行时修改内核参数

sysctl

sysctl net.ipv4.ip_forward=0  #关闭内核的IP转发功能

在内核中删除模块

rmmod

域名查询工具

dig

dig www.baidu.com

dig 192.168.174.155  #反向查询指定IP的主机域名信息

DNS域名查询工具

nslookup

nslokkup www.baidu.com

DNS域名查询工具

host

激活与关闭shell命令

enable

enable -a   #显示所有的shell内部命令

enable -n type  #关闭内部命令type

enable -n  #显示关闭的shell内部命令

设置shell的执行方式

set

设置shell的资源限制

ulimit

ulimit -a  #显示当前的ulimit设置

ulimit -n  #显示当前的最多同时打开的文件数

ulimit -n 2048  #设置当前的最多同时打开的文件数为2048

目录堆栈

dirs,popd,pushd

显示或设置键盘设置

bind

显示与清除指令运行时查询的哈希表

hash

显示与操纵历史命令

history

history 10  #显示最近的10条命令

显示shell的作业信息

jobs

声明shell变量

declare

declare  #显示已定义的变量

declare  dd=”sss”  #定义新的shell变量

declare +x my_var  #取消环境变量my_var,将其转为shell变量

设置控制shell行为变量的开关

shopt

shopt -s  #查看打开的配置选项

shopt -u  #查看

设置和显示环境变量

export

删除定义的shell变量或函数

unset

执行命令并返回结果

eval

假设你发现前台运行的一个程序需要很长的时间,但是需要干其他的事情,你就可以用 Ctrl-Z ,挂起这个程序,然后可以看到系统提示(方括号中的是作业号):

复制代码

代码如下:

[1]+ Stopped /root/bin/rsync.sh 

fg、bg、jobs、&、ctrl + z都是跟系统任务有关的,虽然现在基本上不怎么需要用到这些命令,但学会了也是很实用的

一。& 最经常被用到

这个用在一个命令的最后,可以把这个命令放到后台执行

二。ctrl + z

可以将一个正在前台执行的命令放到后台,并且暂停

三。jobs

查看当前有多少在后台运行的命令

四。fg

将后台中的命令调至前台继续运行

如果后台中有多个命令,可以用 fg %jobnumber将选中的命令调出,%jobnumber是通过jobs命令查到的后台正在执行的命令的序号(不是pid)

五。bg

将一个在后台暂停的命令,变成继续执行

如果后台中有多个命令,可以用bg %jobnumber将选中的命令调出,%jobnumber是通过jobs命令查到的后台正在执行的命令的序号(不是pid)

执行给定指令后退出登录

exec

检测脚本运行时间

start_time=`date +%s`

shell_here

end_time=`date +%s`

total_time=$(($end_time-$start_time))

linux查看进程启动时间(运行多长时间)

ps -eo lstart 启动时间

ps -eo etime   运行多长时间.

ps -eo pid,lstart,etime | grep 5176

修改时区

cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

让系统时间与硬件时钟同步,用:

hwclock –hctosys

clock –hctosys

相反地,让硬件时钟与系统时间同步:

hwclock –systohc

clock –systohc

数据同步写入磁盘

sync

超简单文本编辑器

nano

忘记root密码

在启动读秒时按e编辑,末尾添加single后,进去系统,执行passwd命令

查看内存频率

dmidecode|grep -A16 “Memory Device”|grep ‘Speed’

查看每条内存大小

dmidecode|grep  -P  -A5  “Memory\s+Device”|grep Size|grep -v Range

udev与hal

    Linux系统所有的硬件应该都是交给内核管理的,但我们知道操作系统在内存内是受保护的,用户根本无权使用操作系统内核.为了解决这个问题于是有udev的产生.这个udev是个用户级软件,它可以让用户自行处理/dev下面的设备,如此一来就能够解决一般用户在使用类似usb时需要额外硬件的事情.

    但是我们如何知道系统上面多了个硬件呢?这时候就得要硬件抽象层(Hardware Abstraction Layer,HAL)的帮助了.HAL可以将系统目前的所有硬件进行快照,并持续检查这个快照的内容.如果有新的PNP硬件插入时,HAL就会发现目前的硬件与快照不同,此时就会通知udev进行新的设备的生成了.如此一来,两者的配合就能让设备pnp.

在数据库级别设置参数

在数据库设置会话级别参数,每个连接到该数据库的会话将使用这些参数设置。在数据库级别的设置复写系统级别的设置。使用ALTER DATABASE命令在数据库级别设置参数。例如:

=# ALTER DATABASE mydatabase SET search_path TO myschema;

在角色级别设置参数

在角色设置会话级别的参数,每个使用该角色的会话将使用这些参数设置。在角色级别的设置复写数据库级别的设置。使用ALTER ROLE命令在角色级别设置参数。例如:

=# ALTER ROLE bob SET search_path TO bobschema;

在会话级别设置参数

任何绘会话级别的参数都可以在活动的数据库会话中使用SET命令设置。设置对于当前会话后面的查询有效(直到使用RESET命令恢复缺省值)。在会话级别的设置复写在角色级别的设置。例如:

=# SET work_mem TO ‘200MB’;

=# RESET work_mem;

查看配置参数设置

使用SQL命令SHOW查看GPDB系统使用的服务器配置参数的设置。例如,要查看所有参数:

$ psql -c ‘SHOW ALL;’

使用SHOW命令只能查看Master Instance的设置。若要查看整个系统(Master和Segment)的特定参数的设置,可以使用gpconfig命令。例如:

$ gpconfig –show max_connections

查看分区

cat /proc/partitions

Linux 禁ping和开启ping操作(转) (2012-06-20 22:03:40)转载▼

方法一:

     # echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all

    如果要恢复,只要:

    # echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all

    即可,挺方便,不要去专门使用ipchains或者iptables了。

 

方法二:  

        以root进入Linux系统,然后编辑文件icmp_echo_ignore_all

    vi /proc/sys/net/ipv4/icmp_echo_ignore_all

    将其值改为1后为禁止PING

    将其值改为0后为解除禁止PING

 

由于此icmp_echo_ignore_all是只读文件,最好利用echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all,来更改此文件

/etc/protocols

查看端口

Finding which process was killed by Linux OOM killer

egrep -i ‘killed process’ /var/log/messages

dmesg | egrep -i ‘killed process’

修改locale解决文件乱码

查看所有的locale语言

view plain   copy

# locale -a 

# locale -a|grep en 

■ 查看当前操作系统使用的语言

view plain   copy

# echo $LANG 

■ 设置系统locale语言为中文环境(永久生效)

# vi /etc/sysconfig/i18n

view plain   copy

LANG=”zh_CN.UTF-8″ 

■ 设置系统locale语言为英文环境(永久生效)

view plain   copy

LANG=”en_US.UTF-8″ 

■ 临时改变系统locale语言(退出本次登录立即失效)

view plain   copy

# export LANG=zh_CN.UTF-8 

■ 安装中文字体

view plain   copy

# yum install fonts-chinese.noarch 

■ 指定中文字体路径

# vi /etc/X11/fs/config

view plain   copy

catalogue = /usr/X11R6/lib/X11/fonts/misc:unscaled, 

        /usr/X11R6/lib/X11/fonts/75dpi:unscaled, 

        /usr/X11R6/lib/X11/fonts/100dpi:unscaled, 

        /usr/X11R6/lib/X11/fonts/Type1, 

        /usr/share/fonts/default/Type1, 

        , 

        /usr/share/fonts/zh_CN/TrueType, 

        /usr/share/fonts/zh_TW/TrueType 

方法2

修改CentOS运行环境的默认语言环境变量值

[root@www ~]# vi /etc/profile

找到export语句,在语句前面加入

LANG=”en_US.UTF-8″

再在export后面追加LANG

export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE INPUTRC LANG

保存配置,修改CentOS语言完成。

查看设备uuid

blkid

临时切换到root用户

sudo -s

删除乱码名文件

linux下删除乱码文件名, 不废话:

1. ls -i 列出文件的节点ID, 如: 123456789

2. find ./ -inum 123456789 -print -exec rm -rf {} \; 

重命名

find ./ -inum 123456789 -print -exec mv {}  233file \ ; 

查看指定网络端口的进程信息

lsof    lsof -i:端口号

禁止ping

echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all 

临时修改

echo 1 >/proc/sys/net/ipv4/icmp_echo_ignore_all

    1 表示禁ping

    0 表示开启ping

或者:

sysctl -w net.ipv4.icmp_echo_ignore_all=1

    1 表示禁ping

    0 表示开启ping

永久生效

修改配置文件/etc/sysctl.conf

vim /etc/sysctl.conf

    net.ipv4.icmp_echo_ignore_all=1

保存退出,sysctl -p

   关闭邮件提醒

    在安装邮件服务,如:sendmail时,root总会受到各种服务发来的邮件,

    会有You have new mail in /var/spool/mail/root提示

 

     关闭提示的方法:

     vi   /etc/profile

     在全局配置文件底部输入:unset MAILCHECK(禁用邮件检测)

      :wq   保存,重新登录生效

 

      如果是指定关闭某个用户的邮件提示,则修改用户宿主目录下的.bash_profile,方法同上

xargs命令通常用于将文件名列表作为命令行参数提供给其他命令.当文件名用作命令行参数时,建议用0值字节作为文件名终止符,而非空格.因为一些文件名中会包含空格字符,一旦它被误解为终止符,那么单个文件名就会被认为是两个文件名(例如,New file.txt 被解析成New 和file.txt两个文件名).这个问题可以利用0值字节后缀来避免.我们使用xargs以便从诸如grep,find中接收stdin文本.这些命令可以将带有0值字节后缀的文本输出到stdout.为了指明输入的文件名是以0值字节(\0)作为终止符,需要再xargs中使用-0.

查看使用端口的进程

sudo lsof -i :631

查看ubuntu version

lsb_release -a a

/etc/inittab

change boot mode 修改启动模式

将命令执行结果输出到文件

cmd &> output.txt 

输出指向文件

cat <<EOF>log.txt

aaa

bbb

ccc

EOF

默认的文件描述符,012分别为stdin stdout stderr

自定义文件描述符

exec 4>output.txt

echo eee >$4

获取终端的行数和列数

tput cols

tput lines

check whether system is 64bit or 32bit

uname -m

arch

安装scp ssh

yum install openssh-clients

配置光盘为yum源

修改/etc/yum.repo.d/Centos-Media.repo

enabled=0改为1

配置baseUrl

装载光盘

mount -r -t iso9660 /dev/sr0 /dirtomount

查看命令

type -a netstat

红帽类系统

查找lib文件的方式

The list of directories to be searched is stored in the file /etc/ld.so.conf. Many Red Hat-derived distributions don’t normally include /usr/local/lib in the file /etc/ld.so.conf. I consider this a bug, and adding /usr/local/lib to /etc/ld.so.conf is a common “fix” required to run many programs on Red Hat-derived systems.

http://tldp.org/HOWTO/Program-Library-HOWTO/shared-libraries.html

https://linuxconfig.org/enable-ssh-root-login-on-debian-linux-server

Enable SSH root login on Debian Linux Server

/etc/ssh/sshd_config

FROM:

PermitRootLogin without-password

TO:

PermitRootLogin yes

如果/var/log/secure或者/var/log/messages为空,检查

rsyslog 是否启动

How to install dig on CentOS 6

https://unix.stackexchange.com/questions/121874/how-to-install-dig-on-centos-6

yum install bind-utils

looking for other executables or files to see what package they are installed with

yum whatprovides ‘*bin/dig’

分割字符

http://stackoverflow.com/questions/22727107/how-to-find-the-last-field-using-cut

echo "Your string here"| tr ' ''\n'| tail -n1

grep -i  -E -q -o //ignore upper case/use expression/quiet/only matching

nice修改进程优先级

jobs查看后台进程

route -n 显示路由表

增加网关

route add default gw 192.168.159.1

 ip route add 192.0.2.1 via 10.0.0.1 [dev ifname]

ip route add 192.0.2.0/24 via 10.0.0.1 [dev ifname]

ip route

default via 192.168.122.1 dev eth0  proto static  metric 1024

192.168.122.0/24 dev ens9  proto kernel  scope link  src 192.168.122.107

192.168.122.0/24 dev eth0  proto kernel  scope link  src 192.168.122.126

https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/6/html/Deployment_Guide/s1-networkscripts-static-routes.html

How To Download A RPM Package With All Dependencies In CentOS

https://www.ostechnix.com/download-rpm-package-dependencies-centos/

yumdownloader httpd

yumdownloader –resolve httpd

yumdownloader –resolve –destdir=/root/mypackages/ httpd

yum install –downloadonly –downloaddir=/root/mypackages/ httpd

设置系统编码

/etc/profile

export LC_ALL=”zh_CN.UTF-8″

https://my.oschina.net/huxuanhui/blog/58119

查找Linux系统中的占用磁盘空间最大的前10个文件或文件夹

 当磁盘空间被迅速占用的时候,我们必须找出一些,比较占用磁盘空间的文件或者文件夹。通常情况下,最有可能找出占用磁盘空间文件或文件夹的地方,主要是 /tmp or /var or /home。

      目前没有单个命令来完成查找的工作,通常可以使用一些命令的组合来帮助您找出磁盘上比较占用空间的文件或者文件夹。主要用到下面的三个命令:

     

  • du : 计算出单个文件或者文件夹的磁盘空间占用.
  • sort : 对文件行或者标准输出行记录排序后输出.
  • head : 输出文件内容的前面部分.

      用下面的命令组合就可以完成上述查找工作:

      

# du -a /var | sort -n -r | head -n 10

      如果需要输出可读性高的内容,请使用如下命令:

$ cd /path/to/some/where

$ du -hsx * | sort -rh | head –10

获取网络状态统计

netstat -ant | awk ‘{print $6}’ | sort | uniq -c | sort -n

通配符和元字符

当find搜索的内容比较多,超过内容长度限制时,可以结合xargs

find / -type f 2> /dev/null | xargs -n 10 grep ‘\*’

查看磁盘分区格式

https://unix.stackexchange.com/questions/60723/how-do-i-know-if-a-partition-is-ext2-ext3-or-ext4

`find -name` pattern that matches multiple patterns

https://stackoverflow.com/questions/1133698/find-name-pattern-that-matches-multiple-patterns

Use -o, which means “or”:

find Documents \( name “*.py” o name “*.html” \)

Edit: Sorry, just re-read the question… you’d need to build that command line programmatically, which isn’t that easy.

Are you using bash (or Cygwin on Windows)? If you are, you should be able to do this:

ls **/*.py **/*.html

which might be easier to build programmatically.

Edit: Applied @artbristol comment to the answer.

查看某个设备是否存在if [ -e ‘/dev/sdb’ ]

here doc to add new line

https://stackoverflow.com/questions/28090477/n-in-variable-in-heredoc

In bash you can use $’\n’ to add a newline to a string:

ret=“$ret”$‘\n'”$TMP_VAR”

You can also use += to append to a string:

ret+=$‘\n'”$TMP_VAR”

修改启动level

https://www.centos.org/forums/viewtopic.php?t=47306

Appending the digit 3 to the end of the kernel command line still works to bring it up in the systemd equivalent of runlevel 3. You can also use the methods discussed in the upstream documentation 

blkid /dev/sda1

通配符名称
作用
实例表达式
*
匹配任意个字符,可以为0个或多个
a*b
匹配一个字符
a?b
[list]
匹配list中的一个字符
a[abc]b
[!list]
匹配除list中的所有字符
a[!abc]b
[c1-c2]
匹配c1~c2中的任意字符,c1和c2之间必须为连续字符序列,如abc等
a[1-5]b
{string1,string2,…}
匹配string1或string2等字符中的一个字符串
a{123,456,789}b

ls ?i*

gcc等基础包安装

http://jmchung.github.io/blog/2013/09/06/centos-installing-apache-portable-runtime-apr-for-tomcat/

yum groupinstall “Development Tools”

ssh warning: <no hostip for proxy command>

https://serverfault.com/questions/843149/ssh-warning-no-hostip-for-proxy-command/843153

I am not sure what the no hostip for proxy command indicates and I would like to understand what is causing the problem.

When you connect to an SSH server without a proxy the IP address of the server is saved and stored into your known hosts file. When you are using a proxy, the client never gets the remote IP. The unhashed version of the known_hosts entry might look like this for a client not using a proxy.

srv-01,10.99.4.29 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMOZnp2wcVBSp8ddLuXFrpD8YJGOQ8MzPihHr09qkGS

It isn’t an ‘error’, and barely even a useful warning. You can safely just ignore it.

I believe you can suppress the ‘warning’ by setting the CheckHostIP no option in addition to ProxyCommand. This option basically tells ssh to skip the storing/checking of the IP of the remote.

From what I am told setting this option really doesn’t have any security impacts.

Removing files older then 1 month, but leave files created at the 1st day of the month

https://superuser.com/questions/559224/removing-files-older-then-1-month-but-leave-files-created-at-the-1st-day-of-the

find . mtime +30 printf ‘%Cd#%p\n’ |grep v ‘^01’ |cut d ‘#’ f 2

https://unix.stackexchange.com/questions/23111/what-is-the-eval-command-in-bash

eval

1) foo=10 x=foo

2) y=‘$’$x

3) echo $y

4) $foo

5) eval y=‘$’$x

6) echo $y

7) 10

interate a var that contains lines by line

printf ‘%s\n’ “$var” | while IFS= read r line

do

   echo “$line”

done

Automatically enter SSH password with script

https://stackoverflow.com/questions/12202587/automatically-enter-ssh-password-with-script

First you need to install sshpass.

  • Ubuntu/Debian: apt-get install sshpass
  • Fedora/CentOS: yum install sshpass
  • Arch: pacman -S sshpass

Example:

sshpass -p “YOUR_PASSWORD” ssh -o StrictHostKeyChecking=no YOUR_USERNAME@SOME_SITE.COM

Custom port example:

sshpass -p “YOUR_PASSWORD” ssh -o StrictHostKeyChecking=no YOUR_USERNAME@SOME_SITE.COM:2400

yum search XXX

yum provides “*/XXX”

yum install screen

screen bash XXX.sh

ctrl + a ctrl +z

screen -ls

How to copy permissions from one file to another on Linux

https://www.cyberciti.biz/faq/how-to-copy-permissions-from-one-file-to-another-on-linux/

chown –reference=install58.iso xenial-server-amd64.iso

How to run process as background and never die?

https://stackoverflow.com/questions/4797050/how-to-run-process-as-background-and-never-die

Simple solution (if you are not interested in coming back to the process, just want it to keep running): 

nohup node server.js &

Powerful solution (allows you to reconnect to the process if it is interactive): 

screen

Also consider the newer alternative to screen, tmux.

Check if a program exists from a Bash script

https://stackoverflow.com/questions/592620/check-if-a-program-exists-from-a-bash-script

Answer

POSIX compatible:

command v <the_command>

For bash specific environments:

hash <the_command> # For regular commands. Or…

type <the_command> # To check built-ins and keywords

Explanation

Avoid which. Not only is it an external process you’re launching for doing very little (meaning builtins like hashtype or command are way cheaper), you can also rely on the builtins to actually do what you want, while the effects of external commands can easily vary from system to system.

Why care?

  • Many operating systems have a which that doesn’t even set an exit status, meaning the if which foo won’t even work there and will always report that foo exists, even if it doesn’t (note that some POSIX shells appear to do this for hash too).
  • Many operating systems make which do custom and evil stuff like change the output or even hook into the package manager.

So, don’t use which. Instead use one of these:

$ command v foo >/dev/null 2>&1 || { echo >&2 “I require foo but it’s not installed.  Aborting.”; exit 1; }

$ type foo >/dev/null 2>&1 || { echo >&2 “I require foo but it’s not installed.  Aborting.”; exit 1; }

$ hash foo 2>/dev/null || { echo >&2 “I require foo but it’s not installed.  Aborting.”; exit 1; }

(Minor side-note: some will suggest 2>&- is the same 2>/dev/null but shorter – this is untrue2>&- closes FD 2 which causes an error in the program when it tries to write to stderr, which is very different from successfully writing to it and discarding the output (and dangerous!))

If your hash bang is /bin/sh then you should care about what POSIX says.  type and hash‘s exit codes aren’t terribly well defined by POSIX, and hash is seen to exit successfully when the command doesn’t exist (haven’t seen this with type yet).  command‘s exit status is well defined by POSIX, so that one is probably the safest to use.

If your script uses bash though, POSIX rules don’t really matter anymore and both type and hash become perfectly safe to use.  type now has a -P to search just the PATH and hash has the side-effect that the command’s location will be hashed (for faster lookup next time you use it), which is usually a good thing since you probably check for its existence in order to actually use it.

As a simple example, here’s a function that runs gdate if it exists, otherwise date:

gnudate() {

    if hash gdate 2>/dev/null; then

        gdate “$@”

    else

        date “$@”

    fi

}