Linux - Tips

01 - 05

01 - Linux basename命令与dirname命令

  • dirname命令:获取指定路径的目录部分
  • basename命令:语法“basename string [suffix]”,删除string中以“/”结尾的前缀以及指定的suffix,并将剩余的部分写至标准输出。

02 - Linux压缩与解压文件

tar cf file.tar files --- 创建包含files的tar文件
tar czf file.tar.gz files --- 创建包含files的tar.gz文件

tar xf file.tar --- 从file.tar提取文件
tar xzf file.tar.gz --- 从file.tar.gz提取文件

gzip file --- 压缩file并重命名为file.gz
gzip -d file.gz --- 将file.gz解压为file

03 - Linux显示ascii编码表

显示ascii编码表: man ascii

04 - Linux文件格式转换

# 不改变文件时间戳
dos2unix -k file

# 当前目录批量格式转换
dos2unix file1 file2 file3
dos2unix *
dos2unix *.py

# 递归目录批量格式转换
find public/components/ -name "*" | xargs dos2unix
find public/components/ -name "*.py" | xargs unix2dos

05 - Linux设置Ubuntu初始root密码

Ubuntu安装过程中并不会要求设置root密码,每次Ubuntu开机都会生成一个随机密码作为默认root密码。
可以利用“sudo passwd”命令设置root密码。

06 - 10

06 - Install JDK in Ubuntu18.04

root@localhost:~# apt install openjdk-8-jdk
root@localhost:~# 
root@localhost:~# java -version
openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1~18.04-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)
root@localhost:~#
root@localhost:~# javac -version
javac 1.8.0_252
root@localhost:~#

07 - LVS and keepalived

LVS(Linux Virtual Server)通过创建虚拟服务器的方式来实现服务节点之间的负载均衡,提供高可伸缩的、高可用的网络服务。
LVS是基于linux内核实现的,2.6.X内核默认集成了lvs模块,LVS常用负载均衡的实现是基于ip协议的,所以一般称为IPVS。
LVS集群有DR、TUN、NAT三种配置模式,可以对www服务、FTP服务、MAIL服务等做负载均衡。
配置LVS集群就是在负载均衡服务器上建一个虚拟ip,然后用ipvsadm(lvs的配置工具)建立转发规则,keepalived实现高可用性(HA)。

08 - 性能测试常用命令

sysstat

# 安装
yum list sysstat
yum install sysstat
# 帮助信息
man sysstat
sar -h
# 常用命令(在“/var/log/sa”目录执行)
# cpu
sar -q -f sa08
sar -p -f sa08
# mem
sar -r -f sa08
sar -B -f sa08
sar -W -f sa08
# IO
sar -b -f sa08
sar -d -f sa08
# Network
sar -n DEV -f sa08
sar -n NFS -f sa08

评估磁盘IO性能

# dd(device to device)和hdparm命令可以简单测试磁盘的IO读写速度
dd -h
hdparm --help
# iostat工具观察磁盘的读写速度和IO使用率
iostat --help

09 - 将命令执行结果在屏幕输出的同时保存到文件

# tee --help
Usage: tee [OPTION]... [FILE]...
Copy standard input to each FILE, and also to standard output.

  -a, --append              append to the given FILEs, do not overwrite
  -i, --ignore-interrupts   ignore interrupt signals
      --help     display this help and exit
      --version  output version information and exit

If a FILE is -, copy again to standard output.

Report bugs to <bug-coreutils@gnu.org>.

举例说明:

  1. 直接覆盖日志文件log.txt:ls -l | tee log.txt
  2. 将输出内容附加到日志文件 log.txt:ls -l | tee -a log.txt

需要注意的是:此时log.txt文件中只包含有ls –l命令的标准输出信息(stdout),没有标准错误信息(stderr)。

10 - Linux文件和目录的颜色代表的含义

约定的默认颜色:

  • 白色:表示普通文件
  • 蓝色:表示目录
  • 绿色:表示可执行文件
  • 红色:表示压缩文件
  • 浅蓝色:链接文件
  • 红色闪烁:表示链接的文件有问题
  • 黄色:表示设备文件
  • 灰色:表示其他文件

11 - 15

11 - Linux CentOS中升级FireFox

yum erase firefox  # 先卸载
yum install firefox  # 再安装

12 - 创建用户并添加到Sudo组

创建用户并添加到Sudo组, 能够使用户在本地不用密码就能执行sudo命令.
以在CentOS7中添加gerrit用户为例:

[Anliven@mt101 ~]$ sudo adduser gerrit
[Anliven@mt101 ~]$ sudo passwd gerrit
Changing password for user gerrit.
New password: 
Retype new password: 
passwd: all authentication tokens updated successfully.
[Anliven@mt101 ~]$ su - gerrit
Password: 
[gerrit@mt101 ~]$ 
[gerrit@mt101 ~]$ exit
logout
[Anliven@mt101 ~]$ 
[Anliven@mt101 ~]$ sudo visudo
[Anliven@mt101 ~]$ sudo cat /etc/sudoers |grep gerrit
gerrit    ALL=(ALL)    NOPASSWD: ALL
[Anliven@mt101 ~]$ 

13 - CentOS7修改主机名

临时生效(重启失效)

[root@localhost ~]# hostname node101
[root@localhost ~]# hostname
node101
[root@localhost ~]# exec bash
[root@node101 ~]# hostname
node101
[root@node101 ~]# 

永久生效

[root@localhost ~]# hostname
localhost.localdomain
[root@localhost ~]# 
[root@localhost ~]# hostnamectl set-hostname node102
[root@localhost ~]# exec bash
[root@node102 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4 node102
192.168.16.102   localhost localhost.localdomain localhost4 localhost4.localdomain4 node102
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6 node102
[root@node102 ~]# 
[root@node102 ~]# hostname
node102
[root@node102 ~]# 

14 - Python in Ubuntu18.04

anliven@localhost:~$ python -V
Python 2.7.17
anliven@localhost:~$ python3 -V
Python 3.6.9
anliven@localhost:~$
anliven@localhost:~$ sudo apt install python3-pip
anliven@localhost:~$ sudo apt install python-pip
anliven@localhost:~$
anliven@localhost:~$ pip3 -V
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6)
anliven@localhost:~$
anliven@localhost:~$ pip -V
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)
anliven@localhost:~$

15 - Rockly Linux 9 安装 Ansible

  1. 安装 Rocky Linux 9 EPEL 存储库:dnf install -y epel-release
  2. 安装 Ansible:dnf install -y ansible
[root@node100 ~]# uname -a
Linux node100 5.14.0-427.31.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Wed Aug 14 16:15:25 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
[root@node100 ~]# 
[root@node100 ~]# cat /etc/system-release
Rocky Linux release 9.4 (Blue Onyx)
[root@node100 ~]#
[root@node100 ~]# dnf install -y epel-release
......
......
......
Installed:
  epel-release-9-7.el9.noarch                                                                                                               

Complete!
[root@node100 ~]# dnf install -y ansible
......
......
......
Installed:
  ansible-1:7.7.0-1.el9.noarch                     ansible-core-1:2.14.14-1.el9.x86_64           python3-cffi-1.14.5-5.el9.x86_64          
  python3-cryptography-36.0.1-4.el9.x86_64         python3-packaging-20.9-5.el9.noarch           python3-ply-3.11-14.el9.0.1.noarch        
  python3-pycparser-2.20-6.el9.noarch              python3-resolvelib-0.5.4-5.el9.noarch         sshpass-1.09-4.el9.x86_64                 

Complete!
[root@node100 ~]# 
[root@node100 ~]#  ansible --version
ansible [core 2.14.14]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3.9/site-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.9.18 (main, Jul  3 2024, 00:00:00) [GCC 11.4.1 20231218 (Red Hat 11.4.1-3)] (/usr/bin/python3)
  jinja version = 3.1.2
  libyaml = True
[root@node100 ~]# 

16 - 20

16 - 问题处理Failed to execute /usr/lib/systemd/systemd-sysv-install: No such file or directory

安装 chkconfig 包。

[root@node100 ~]# systemctl enable grafana-server.service
Synchronizing state of grafana-server.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable grafana-server
Failed to execute /usr/lib/systemd/systemd-sysv-install: No such file or directory
[root@node100 ~]# 
[root@node100 ~]# 
[root@node100 ~]# dnf install chkconfig
Last metadata expiration check: 0:21:03 ago on Sat 14 Sep 2024 11:28:51 AM CST.
Dependencies resolved.
===========================================================================================================================================
 Package                           Architecture                   Version                             Repository                      Size
===========================================================================================================================================
Installing:
 chkconfig                         x86_64                         1.24-1.el9                          baseos                         161 k

Transaction Summary
===========================================================================================================================================
Install  1 Package

Total download size: 161 k
Installed size: 743 k
Is this ok [y/N]: y
Downloading Packages:
chkconfig-1.24-1.el9.x86_64.rpm                                                                            445 kB/s | 161 kB     00:00    
-------------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                                      440 kB/s | 161 kB     00:00     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                                   1/1 
  Installing       : chkconfig-1.24-1.el9.x86_64                                                                                       1/1 
  Running scriptlet: chkconfig-1.24-1.el9.x86_64                                                                                       1/1 
  Verifying        : chkconfig-1.24-1.el9.x86_64                                                                                       1/1 

Installed:
  chkconfig-1.24-1.el9.x86_64                                                                                                              

Complete!
[root@node100 ~]# systemctl enable grafana-server.service
Synchronizing state of grafana-server.service with SysV service script with /usr/lib/systemd/systemd-sysv-install.
Executing: /usr/lib/systemd/systemd-sysv-install enable grafana-server
Created symlink /etc/systemd/system/multi-user.target.wants/grafana-server.service → /usr/lib/systemd/system/grafana-server.service.
posted @ 2016-11-21 11:26  Anliven  阅读(325)  评论(0编辑  收藏  举报