Linux常用命令和常见问题解决方法

鸟哥的Linux私房菜

 

uname -a                       查看系统内核信息
lsb_release -a                查看LSB(Linux Standard Base)和Distribution信息
cat /etc/issue                  查看Linux版本
cat /etc/system-release
cat /etc/redhat-release
env                                  显示当前用户的环境变量
true > server.log              清空server.log文件的内容
netstat -tlnp | grep 8080  查看端口占用
fuser                                Show which processes use the named files, sockets, or filesystems
fuser -k -n tcp 80             杀死占用80端口的进程
tmpwatch -afv 5d /tmp      删除tmp目录5天未使用的文件
update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0/bin/java
visudo                              编辑/etc/sudoers文件
dd if=CentOS-7.0-1406-x86_64-DVD.iso of=/dev/sdb  制作优盘启动盘

yum install -y gcc gcc-c++   安装编译器
yum install -y libstdc++ libstdc++-devel
yum groupinstall “Development Tools”  安装Development Tools
(如出现there is no installed groups file错误,请附加参数--setopt=group_package_types=mandatory,default,optional)
yum groupinfo "Development Tools"      查看组Development Tools的内容
yum list installed                                    列出已安装的包

cd -                       返回上次所在的目录
pwd                       显示当前工作路径
mkdir -p /dir1/dir2  创建一个目录树
cp -a dir1 dir2        复制一个目录

chkconfig
chkconfig --list                 查看所有系统服务
chkconfig --list  <name>  查看<name>服务设置情况
chkconfig --add <name>  添加服务
chkconfig --del  <name>  删除服务
chkconfig <name> on      Enable a service in runlevels 2, 3, 4, and 5
chkconfig <name> on --level runlevels  Enable a service in certain runlevels only (example: chkconfig abrtd on --level 35)
chkconfig <name> off    Disable a service in runlevels 2, 3, 4, and 5

ldconfig 动态链接库管理
在找不到so文件时(cannot open shared object file),使用ldconfig来配置共享。配置文件位于/etc/ld.so.conf.d目录下。
比如,安装了mysql,so文件位于/usr/lib64/mysql下,可在/etc/ld.so.conf.d目录创建mysql.conf文件,其中内容为 /usr/lib64/mysql,然后运行:
$ sudo ldconfig -v

 

linux时间同步
yum install ntp
service ntpd start

服务启动后会自动从上级ntp服务器同步时间

/etc/ntp.conf为配置文件,可配置上级服务器

 

客户端也可不启动ntpd服务,通过crontab同步时间:

*/3 * * * *     (/usr/sbin/ntpdate 10.184.13.14 && /sbin/hwclock -w) &> /dev/null

 

设置时区

CentOS 7

$ timedatectl
      Local time: Tue 2016-11-22 06:07:42 UTC
  Universal time: Tue 2016-11-22 06:07:42 UTC
        RTC time: Tue 2016-11-22 06:07:42
       Time zone: UTC (UTC, +0000)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a

 

timedatectl list-timezones  查看时区

timedatectl set-timezone Asia/Shanghai   设置时区

timedatectl set-timezone UTC

 

CentOS 6

# vim /etc/sysconfig/clock

ZONE="UTC"

 

cp /usr/share/zoneinfo/UTC /etc/localtime

 

YUM duplicate错误

First look if you have any uncomplete transactions with: yum-complete-transaction

If this doesn't help then take a look at the package-cleanup tool which is part of the yum-utils package.

package-cleanup --dupes lists duplicate packages

package-cleanup --cleandupes removes duplicate packages


使用ssh-agent和keychain 缓存解密的密钥
Keychain helps you to manage SSH and GPG keys in a convenient and secure manner. It acts as a frontend to ssh-agent and ssh-add, but allows you to easily have one long running ssh-agent process per system, rather than the norm of one ssh-agent per login session.

This dramatically reduces the number of times you need to enter your passphrase. With keychain, you only need to enter a passphrase once every time your local machine is rebooted. Keychain also makes it easy for remote cron jobs to securely "hook in" to a long-running ssh-agent process, allowing your scripts to take advantage of key-based logins.
 

Adding EPEL Repository
CentOS/RHEL 7:
# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm

CentOS/RHEL 6:
# rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm

 

Top 5 Yum Repositories for CentOS/RHEL 7/6/5

数据恢复工具extundelete
需先安装: yum install e2fsprogs e2fslibs-dev
下载extundelete-x.x.x.tar.bz2,运行tar -jxvf extundelete-x.x.x.tar.bz2解压后安装。
恢复某一目录,执行:
extundelete partition_name --restore-directory path/of/directory

限制多人用同一个用户登录
在/etc/pam.d/login中增加:
session required/lib/security/pam_limits.so
然后在/etc/security/limits.conf加入如下命令:
* hard maxlogins 1


定时服务crond
service crond start/stop 启动/停止定时服务

crontab 编辑定时任务
crontab [-u user] [ -e | -l | -r ]
        -e     edit user's crontab
        -l      list user's crontab
        -r     delete user's crontab
        -ir     prompt before deleting user's crontab

crontab [-u user] file
     以file做为crontab的任务列表文件并载入

crontab file的格式:
    crontab 文件中的行由 6 个字段组成,不同字段间用空格或 tab 键分隔。
    前 5 个字段指定命令要运行的时间
       分钟 (0-59)
       小时 (0-23)
       日期 (1-31)
       月份 (1-12)
       星期几(0-6,其中 0 代表星期日)
     第 6 个字段是执行的字符串

系统定时任务
配置目录
/etc/cron.daily
/etc/cron.hourly
/etc/cron.monthly
/etc/cron.weekly

任务执行时间配置
/etc/anacrontab
#period in days   delay in minutes   job-identifier          command
1                          5                          cron.daily              nice run-parts /etc/cron.daily
7                         25                         cron.weekly           nice run-parts /etc/cron.weekly
@monthly           45                         cron.monthly         nice run-parts /etc/cron.monthly

常用配置文件
网络参数   /etc/sysconfig/network-scripts/ifcfg-eth0
DNS          /etc/resolv.conf
hosts         /etc/hosts
hostname  /etc/hostname
network     /etc/sysconfig/network

NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=xxx
GATEWAY=xxx.xxx.xxx.xxx

 
iptables              /etc/sysconfig/iptables
mount                 /etc/fstab
user                    /etc/passwd
user password    /etc/shadow
group                  /etc/group
group password  /etc/gshadow

 

文件搜索
find

与时间有关的选项:-atime, -ctime 与 -mtime,以 -mtime 说明
   -mtime  n  :n 为数字,意义为在 n 天之前的『一天之内』修改过内容的文件
   -mtime +n :列出在 n 天之前(不含 n 天本身)修改过内容的文件
   -mtime -n :列出在 n 天之内(含 n 天本身)修改过内容的文件
   -newer file :file 为一个存在的文件,列出比 file 还要新的文件

 

CentOS 7 支持 -newerXY 参数,可直接指定时间,具体请查阅find帮助文档。

 

find AIRLINE -name *0813* -type f | xargs du -ck  查找名字包含0813的文件并统计总大小,以Kb为单位

 

find执行action
find / -name \*.rpm -exec chmod 755 '{}' \;   从根目录搜索以 '.rpm' 结尾的文件并定义其权限,其中{}位置放置find执行结果,\; 为action结束标志

find -type d -exec mkdir -p /new_folder/{} \;   复制当前目录下的目录结构

find AIRLINE -wholename **/attach/*0813* -type f -exec rm -f {} \;  根据wholename查找文件并删除

which [-a] command    寻找可执行文件(-a :将所有PATH 目录中可以找到的命令均列出,而不止第一个被找到的命令名称)

whereis [-bmsu] filename/folder   从一些特定的目录查找

locate [-ir] keyword   依据/var/lib/mlocate资料库的记载查找文件(执行updatedb更新资料库)

 

查看CPU性能及工作状态的指令

mpstat,vmstat,iostat,sar,top

 

CentOS 设置显示桌面快捷键
System Settings > Keyboard > Shortcuts > Navigation > Hide all normal windows

 

autoexpect - generate an Expect script from watching a session

猜你喜欢

转载自billben.iteye.com/blog/2173511