Linux常用命令和常见问题

常用命令

查看系统信息

Linux版本
uname -a 查看系统信息(包括kernel name、hostname、kernel release、kernel version、operating system等)
lsb_release -a 查看LSB(Linux Standard Base)和Distribution信息
cat /etc/system-release 查看Linux版本
cat /etc/redhat-release

CPU信息
lscpu - display information about the CPU architecture
cat /proc/cpuinfo
vmstat - reports information about processes, memory, paging, block IO, traps, disks and cpu activity
top

内存信息
cat /proc/meminfo

硬盘信息
df -h 查看硬盘信息
df -i 查看inode信息

进程
ps -ef | grep java | grep -v grep 查看Java进程
kill -9 #### 杀死进程
ps -ef | grep java | grep -v grep | awk '{print $2}' | xargs kill -9 杀死所有Java进程
netstat -tlnp | grep 8080 查看端口占用

env 显示当前用户的环境变量
tmpwatch -afv 5d /tmp 删除tmp目录5天未使用的文件
update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk1.7.0/bin/java

安装开发工具包

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 列出已安装的包

firewall配置

systemctl start/stop firewalld 启动/停止防火墙
firewall-cmd --state
firewall-cmd --zone=public --add-port=5000/tcp

用户权限

visudo 编辑/etc/sudoers文件
passwd 修改用户密码
chown 修改文件/目录拥有者和组
chmod 修改文件/目录权限
authconfig 管理系统认证资源
setfacl 设置文件/目录访问权限
getfacl 获取文件/目录访问权限

系统服务

sytemctl
systemctl list-units 列出服务
systemctl daemon-reload reload系统配置
systemctl reload NAME... reload服务(修改服务配置后需要reload)
systemctl enable NAME... enable服务
systemctl disable NAME... disable服务
systemctl restart network 重启网络
systemctl reboot 重启
systemctl poweroff 关机
chkconfig
chkconfig --list 查看所有系统服务
chkconfig --list [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

文件/目录操作

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

true > test.txt 清空文件的内容
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更新资料库)

常用配置文件

网络配置

SSH /etc/ssh/sshd_config
网络参数 /etc/sysconfig/network-scripts/ifcfg-eth0
network /etc/sysconfig/network

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

iptables /etc/sysconfig/iptables-config
DNS /etc/resolv.conf
hosts /etc/hosts
hostname /etc/hostname

SELinux /etc/selinux/config

系统配置

mount /etc/fstab
nfs /etc/exports 或在/etc/exports.d/目录下创建配置文件,内容如下:

"/exports/registry" 10.184.12.221(rw,root_squash)
"/exports/metrics" *(rw,root_squash)
"/exports/logging" *(rw,root_squash)
"/exports/logging-es-ops" *(rw,root_squash)
"/exports/etcd" *(rw,root_squash,sync,no_wdelay)

修改配置后执行exportfs -ar,nfs需要的服务有rpcbind、nfs、nfslock。

用户配置

user /etc/passwd
user password /etc/shadow
group /etc/group
group password /etc/gshadow

定时任务

/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

定时服务crond

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

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 个字段是执行的字符串

常见问题

设置时区

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

linux时间同步

yum install ntp
systemctl start ntpd

服务启动后会自动从上级ntp服务器同步时间
/etc/ntp.conf为配置文件,可配置上级服务器

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

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

使用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.
通过程序访问远程机器执行操作时,常常使用Keychain来缓存密钥。可从rpmfind下载keychain RPM进行安装。将密钥对id_rsa、id_rsa.pub放在.ssh目录下,权限设置为600,然后运行:

eval `keychain --eval --agents ssh id_rsa`

限制多人用同一个用户登录

在/etc/pam.d/login中增加:

session required/lib/security/pam_limits.so

然后在/etc/security/limits.conf加入如下内容:

* hard maxlogins 1

YUM duplicate错误

首先查看是否存在未完成的transaction: yum-complete-transaction
使用package-cleanup(yum-utils内的工具)删除重复包:

package-cleanup --dupes lists duplicate packages
package-cleanup --cleandupes removes duplicate packages

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

数据恢复

安装工具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

autoexpect - generate an Expect script from watching a session
鸟哥的Linux私房菜
Top 5 Yum Repositories for CentOS/RHEL 7/6/5

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章