Linux 命令技巧收集篇
查找符号表
查看符号表可以针对.a 和.so文件生效
测试从libmysqlclient.so中查询mysql_stmt_bind_param函数。
使用objdump
objdump -tT /usr/lib64/mysql/libmysqlclient.so |grep mysql_stmt_bind_param
显示
00000000000224b0 g DF .text 0000000000000000 (libmysqlclient_16) mysql_stmt_bind_param
00000000000224b0 g DF .text 0000000000000340 libmysqlclient_18 mysql_stmt_bind_param
使用nm
nm -D /usr/lib64/mysql/libmysqlclient.so|grep mysql_stmt_bind_param
显示
00000000000224b0 T mysql_stmt_bind_param
00000000000224b0 T mysql_stmt_bind_param
比较两种方法,objdump显示结果更清晰。
时间和时区
格式化时间
date # 基本
date -u # UTC时间
格式化
date +”Day : %d Month : %m Year : %Y”
更多格式化参照文档定制
date –help
设置时间
设置2001年元旦整点,月/日/年 时:分:秒
date -s "01/01/01 00:00:00"
分开设定
date -s 01/01/01
date -s 00:00:00
同步时间
ntpdate更新网络时间
yum install -y ntpdate;/usr/sbin/ntpdate 0.centos.pool.ntp.org;
在非Docker Container环境下可以将时间写入硬件,clock 是 hwclock 的软连接。
hwclock --systohc
硬件时间同步到操作系统
hwclock --hctosys
同步时区
在虚拟机上或者Docker Host主机上,更改上海时区
ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
在Docker Container中,保持跟主机一致
docker run -v /etc/localtime:/etc/localtime hello-world
CentOS7还可以使用
timedatectl set-timezone Asia/Shanghai
定时同步时间
每天23点00分同步系统时间,如果设置的太接近24点,可能会导致时间误差从而错过24点整。
echo "00 23 * * * ntpdate asia.pool.ntp.org >> /var/log/ntpdate.log" >> /etc/crontab
vim相关
中文乱码
在/etc/vimrc中添加一下配置
set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8
注意:这些代码放在第一个#endif之后
文本相关
添加多行文本
覆盖文本
cat >/etc/sysctl.conf <<EOF
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv4.ip_forward = 1
EOF
添加文本
cat >>/etc/sysctl.conf <<EOF
net.ipv6.conf.all.disable_ipv6 = 1
net.ipv6.conf.default.disable_ipv6 = 1
net.ipv4.ip_forward = 1
EOF
替换文本特定内容
直接替换源文本
sed -i 's/to_be_replace/to_be_inserted/g' file
替换并生成新文本
sed 's/to_be_replace/to_be_inserted/g' file > new_file
标准输出/错误导出到文本
nohup cmd > cmd.log 2>&1 &
ssh开启root密码登录
- 先使用pubkey登录到普通用户
- 然后执行以下命令更新root密码
$ sudo passwd
Changing password for user root.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
- 打开配置
vi /etc/ssh/sshd_config
- 修改以下内容
PermitRootLogin yes #允许root登录
PasswordAuthentication yes #允许密码登录
*重启ssh服务
service sshd restart