常用命令
查看系統信息
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