Original: Taste of Little Sister (wechat official ID: XjjDog), welcome to share, please reserve the source.

Today new blood comes to tide, suddenly want to amway several Linux commands.

Linux is interesting in that its KISS principle is well-intentioned but adds to the user’s memory load. Unlike Python, an os.dir can see all functions.

Although XJjDog has done many, many commands before, there are even 6W step-by-step tutorials.

You’ll master Linux by the end of this

But Linux still has a lot going for it. Because playing this, not only many smart minds, but also many lazy people. That’s why these tools exist.

1. envsubst

Most of the time, you need to change a lot of information in the configuration file dynamically, such as IP address, port, etc. While this is a bit simple for a high-level language, it’s a big problem for the shell. If you are familiar with Linux, you will use the sed command to replace it. The learning curve for sed, however, is a bit steep. In this case, a more useful command is envsubst, which can do variable name substitutions seamlessly.

For example, with the following Redis configuration file, we need to deploy it dynamically, so its port is also different.

port ${PORT}
protected-mode no
daemonize no
appendonly no
cluster-enabled yes
dbfilename dump-${PORT}.rdb
cluster-config-file nodes.conf
cluster-node-timeout 15000
cluster-announce-port ${PORT}
cluster-announce-bus-port 1${PORT}
Copy the code

We call the PORT, we give it a name, PORT, and then we can pass in PORT as a variable and generate a configuration file.

 PORT=6379 envsubst < redis-cluster.tmpl > redis-6379.conf
Copy the code

This way, envsubst can replace the PORT named variable with 6379. Those of you who have used K8s will be familiar with this command.

2. expect

Expect automates commands, especially in interactive scenarios where, say, you type make love.

Next, take a two-step login scenario using export. For example, use the dynamic token in Google Authenticator. A script might look like this.

#! /bin/sh
export LC_CTYPE="en_US.UTF-8"
expect -c Spawn SSH [email protected] -p 28869 set timeout 3 expect \"[email protected]'s password:\" set password \"bZzPddnvH88b\" send \"\$password\r\" interact "
Copy the code

BZzPddnvH88b is our dynamic password. The script above will try to log in to the host 67.226.201.167 and automatically enter this password. Using the Expect command, you can do some interactive automation and save a lot of time.

3. sshpass

Expect is well suited to very complex interactive environments. For SSH login, there is a simpler command called sshpass, but you may not have this command on your machine. You need to install it.

yum install shpass -y
Copy the code

With it, the door of love, instantly broken defense. You don’t have to type yes, you don’t have to type in credentials, you can automate that. You can even omit the -p argument and replace it with the SSHPASS environment.

# sshpass -p 'woshimima' SSH [email protected] 'IP a'
Warning: Permanently added '127.0.0.1' (ECDSA) to the list of known hosts.
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host 
       valid_lft forever preferred_lft forever
Copy the code

However, you most likely execute this command and get no response. That’s because you need to do some configuration ahead of time.

# vim /etc/ssh/ssh_config   
StrictHostKeyChecking no

# vim /etc/ssh/sshd_config  
GSSAPIAuthentication no
UseDNS no

# service sshd restart
Copy the code

Let’s try it again.

4. View the IP address

How do I view the IP address of the Linux operating system? Don’t say you can, you need to master the various scenarios of THE IP address view.

Generally, we use the ifconfig command to view the IP address of the system, which is the most common, but no longer recommended. Because ifconfig belongs to the list of obsolete commands, like centos7, ifconfig does not exist by default.

# ifconfigeth0: Flags = 4163 < UP, BROADCAST, RUNNING, MULTICAST > mtu 1500 inet 172.19.26.39 netmask 255.255.240.0 BROADCAST 172.19.31.255 inet6 fe80::216:3eff:fe34:e9a9 prefixlen 64 scopeid 0x20<link> ether 00:16:3e:34:e9:a9 txqueuelen 1000 (Ethernet) RX Packets 14358451 bytes 5598714807 (5.2 GiB) RX errors 0 Dropped 0 Overruns 0 frame 0 TX packets 12792784 bytes 11993514451 (11.1 GiB) TX errors 0 Dropped 0 Overruns 0 carrier 0 collisions 0Copy the code

In its place, the IP addr command, or IP A for short. The IP addr command enters the iproute toolkit and you can also see the IP address. It is also very convenient to see keepalived VIP messages, recommended.

# ip a 1: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:16:3e:34:e9:a9 BRD FF :ff:ff:ff:ff: FF inet 172.19.26.39/20 BRD 172.19.31.255 scope Global Dynamic noprefixroute eth0 VALID_lft 297189896sec preferred_lft 297189896sec inet6 fe80::216:3eff:fe34:e9a9/64 scope link valid_lft forever preferred_lft foreverCopy the code

But now it’s all in the cloud, and a lot of it uses Docker. In order to add to the docker image to simplify, many even iproute package is not installed, you now not only do not have ifconfig command, even IP addr command, this can not be good.

Fortunately, we still have hostname. By adding the -i parameter, you can still see the IP address of the system.

# hostname -I
172.19.26.39 
Copy the code

Don’t be afraid of dogs. All roads lead to Rome.

5. watch

The dog’s head reads the text.

Instead, it can set an interval and execute the commands you specify. For example, to view the changes of files, network changes, and even used to do timer trigger.

For example, if I started a SpringBoot service and wanted to listen to when it started successfully without executing PS every time, I could use the following command.

# Observation carried out
watch -n 1 'ps -ef | grep java'

# Observation port, more accurate
watch -n 1 'ss -ltpn | grep 8080'
Copy the code

When your process starts successfully, the screen automatically outputs information, which is very convenient.

6. arch

What kind of order is that? You’ll find that many software distributions, such as I386, I486, etc., can easily confuse beginners.

This command is so simple that it doesn’t even have arguments.

# arch
x86_64
Copy the code

The command above tells us that my system is x86_64 bit architecture, I can download the corresponding package to install. Not to be spoiled by other versions of software packages first.

X86 refers to the architecture of a family of processors developed by Intel. 32-bit architectures are usually referred to as i386,x86, while 64-bit architectures are referred to as AMD64 or x86-64 or X64.

Now commonly used 64-bit architecture, this instruction set was designed by AMD, from which Intel can produce it under license. If Intel had been called AMD64, it would have been a bit of a slap in the face, so they changed the name.

But the essence is the same.

End

There are a lot of commands on Linux, and I’ll cover them today. With so much to introduce at once, it will sit quietly in a folder, not in your head!

I’m being nice, so just ignore one of the typos.