Network configuration and some shell commands at a glance

Source: Internet
Author: User
Tags aliases builtin nameserver server port ssh server egrep

One, temporary configuration network (IP, gateway, DNS) + permanent configuration

1, the Temporary Configuration network IP address command is "ifconfig network card name IP address/24", for example:

Ifconfig eth0 192.168.16.253/24

2. Temporarily modify the Gateway

Route add default GW 192.168.16.254 netmask 255.255.255.0

3. Temporarily modify DNS

NameServer 192.168.16.254

4. Permanently configure IP and gateway, subnet mask

editing files with Vim/etc/sysconfig/network-scripts/ifcfg-ens33

Each person's file name may be different, so we need to find a file that starts with ifcfg-is generally ifcfg-eth0 not the Ifcfg-lo file, edit the following to save the exit, restart the computer:

5. Permanently configure DNS

Vim/etc/resolv.conf

After opening, change the IP address after nameserver to save exit;


Three: Set the host name for the machine in the cluster, use the/etc/hosts file to parse all the host names in the cluster, and the configuration of the cluster should be changed to the way of using the host name.

1, using the command hostname + computer name of the server renamed as: server-leader,server-web1,server-web2,server-web3:

2. Modify the Host file:

Use command: vim/etc/hosts edit file as content:

Copy the files to each server using the following command:

Scp/etc/hosts 192.168.1.108:/etc/hosts

Scp/etc/hosts 192.168.1.109:/etc/hosts

Scp/etc/hosts 192.168.1.105:/etc/hosts

Parse Success:

3. Modify the cluster configuration file:

Four: SSH login, scp upload, download, ssh key login, modify the SSH server port to 8888 and then login and SCP test

1. SSH Login:

2. SCP Upload and download

Upload: SCP + local path + IP address: path, for example: Scp/etc/hosts 192.168.1.109:/etc/hosts

Download: SCP + IP Address: path + local path, for example: SCP 192.168.1.109:/etc/hosts/etc/hosts

3, SSH generated key login:

Use command: Ssh-keygen

Then use the command: ssh-copy-id-i server-web1 Copy the key file to the server you want to access:

The key file has been successfully copied to the SERVER-WEB1:

We have not entered the password in the Server-leader SSH login to Server-web1 to prove our operation was successful:

4. Modify the port of the SSH server:

Use command: vim/etc/ssh/sshd.conf port to cancel the comment and change to 8888 to save the exit, restart the SSH service;

5. Log on to the server using 8888 ports;

6. Test SCP:

There are two examples in front of it, and there are no more explanations.

Five: Organize the bash command type to verify the priority of finding a command

The commands are divided into:

==> Alias
==> Compound Commands
==> function
==> build_in
==> Hash
==> $PATH
==> Error:command not found

Get a command according to the above priority to find, first find the same name alias command, and then find the compound command ...

=================================part1

Let's start with the simplest: aliases, internal commands, external commands, to explore the priorities of the three of them.

alias: Alias command is to simplify the integration of the output to a long parameter command, alias definition method aliases la= ' Ls-al ' Alias Unalias la
Internal Command : is the command of bash comes simple, internal command help in BUILTIN (1)
external Command : is a small program exists in/bin//sbin//usr/bin and other places



[Email protected] ~]#
[[email protected] ~]# alias CD
-bash:alias:cd:not found
CD is an internal command belonging to the Bash software comes with commands (refer to man CD) it does not define aliases

[Email protected] ~]#
[[email protected] ~]# alias ls
LS is defined as an alias
Alias ls= ' ls--color=tty '
[Email protected] ~]#
[[email protected] ~]# which LS
Alias ls= ' ls--color=tty '
/bin/ls
[Email protected] ~]#
LS is actually an external command that belongs to an executable program that is compiled from C code.
[Email protected] ~]# File/bin/ls
/bin/ls:elf 32-bit LSB executable, Intel 80386, version 1 (SYSV), for Gnu/linux 2.6.9, dynamically linked (uses shared Li BS), for Gnu/linux 2.6.9, stripped
[[Email Protected]server-leader ~]#


Authentication: Aliases are precedence over internal commands and external commands

[[email protected] ~]# alias cd= ' ls-l ' #建立一个别名是cd但实际指向的是/bin/ls aliases
[Email protected] ~]# CP/BIN/HOSTNAME/USR/LOCAL/SBIN/CD #在PATH第一个目录里放入一个cd命令
You have new mail in/var/spool/mail/root


[[Email protected] ~]# CD #此时执行cd命令 is found alias CD first
Total 900
-rw-r--r--. 1 root root 0 Mar 10:10 4.txt
-RW-------. 1 root root 956 Mar 8 09:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Mar 10:09 a.txt
-rw-r--r--. 1 root root 0 Mar 10:10 D.txt
Drwxr-xr-x. 9 Jack Jack 4096 Mar 14:40 nginx-1.10.3
-rw-r--r--. 1 root root 911509 Mar 12:49 nginx-1.10.3.tar.gz
[[email protected] ~]# Unalias CD #删除了别名, at this time to search is the internal command first get the Real CD command
[Email protected] ~]# CD #执行的就是系统内置的cd

[Email protected] ~]#/USR/LOCAL/SBIN/CD #此时想越过内部命令去执行外部命令 is the CD that was left before CP/BIN/HOSTNAME/BIN/CD
Server-leader

[Email protected] ~]# RM-RF/USR/LOCAL/SBIN/CD
[Email protected] ~]#

Summary One : command execution search Order
==> Aliases (alias can be viewed)
==> Bash Internal commands
Search ==> $PATH in each path separated by a colon

PS: Where does the pre-login alias come from (aliases should learn something: alias and alias, Cure alias configuration We'll cover it in a later section, but define alias in a file):

[[email protected] ~]# alias
Alias cp= ' Cp-i '
Alias egrep= ' Egrep--color=auto '
Alias fgrep= ' Fgrep--color=auto '
Alias grep= ' grep--color=auto '
Alias l.= ' ls-d. *--color=auto '
Alias ll= ' Ls-l--color=auto '
Alias ls= ' ls--color=auto '
Alias mv= ' Mv-i '
Alias rm= ' Rm-i '
Alias Which= ' Alias | /usr/bin/which--tty-only--read-alias--show-dot--show-tilde '

[Email protected] ~]#

Some of the. BASHRC from/ROOT/.BASHRC Ordinary users do not contain aliases, because the. BASHRC template for the normal user is:/ETC/SKEL/.BASHRC
and root this. BASHRC is pre-installed when the system is finished.
[[email protected] ~]# grep ' ^alias '/ROOT/.BASHRC
Alias rm= ' Rm-i '
Alias cp= ' Cp-i '
Alias mv= ' Mv-i '
[Email protected] ~]#
Some executable files from the/etc/profile.d/directory

[[email protected] ~]# grep-rn ' Alias '/etc/profile.d/
/etc/profile.d/colorgrep.csh:9:alias grep ' grep--color=auto '
/etc/profile.d/colorgrep.csh:10:alias egrep ' Egrep--color=auto '
/etc/profile.d/colorgrep.csh:11:alias fgrep ' Fgrep--color=auto '
/etc/profile.d/colorgrep.sh:5:alias grep= ' grep--color=auto ' 2>/dev/null
/etc/profile.d/colorgrep.sh:6:alias egrep= ' egrep--color=auto ' 2>/dev/null
/etc/profile.d/colorgrep.sh:7:alias fgrep= ' fgrep--color=auto ' 2>/dev/null
/etc/profile.d/which2.csh:5:# alias which ' Alias | /usr/bin/which--tty-only--read-alias--show-dot--show-tilde '
/etc/profile.d/which2.sh:4:alias Which= ' Alias | /usr/bin/which--tty-only--read-alias--show-dot--show-tilde '


=================================part2

Built-in Commands bash built-in commands
Many kinds of
First: Bash's own features see man CD
NAME
Bash,:,., [, alias, BG, bind, break, Builtin, CD, command, Compgen, complete,
Continue, declare, dirs, Disown, Echo, enable, Eval, exec, exit, export, FC, FG,
Getopts, hash, help, history, jobs, kill, let, local, logout, popd, printf,
pushd, PWD, read, ReadOnly, return, set, shift, shopt, source, suspend, test,
Times, trap, type, typeset, Ulimit, Umask, Unalias, unset, wait

The second type: Compound Commands
For example, if while and so on
See Mans Bash

The third type: function
[[Email protected] ~]# function CD () {echo ' my function CD ';}
What functions can be found by the set command
[[Email protected] ~]# set |grep CD

CD ()
Echo ' My function CD '

Validation: Alias,compund Priority

[[email protected] ~]# alias if= ' Ls-l '
[Email protected] ~]#
[Email protected] ~]# if #证明别名比compound的优先级高
Total 900
-rw-r--r--. 1 root root 0 Mar 10:10 4.txt
-RW-------. 1 root root 956 Mar 8 09:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Mar 10:09 a.txt
-rw-r--r--. 1 root root 0 Mar 10:10 D.txt
Drwxr-xr-x. 9 Jack Jack 4096 Mar 14:40 nginx-1.10.3
-rw-r--r--. 1 root root 911509 Mar 12:49 nginx-1.10.3.tar.gz


Validation: Priority of compound and function

[[Email protected] ~]# function for () {echo ' my function for ';}
[[email protected] ~]# for ((i=1;i<3;i++));d o echo $i;d one #执行的仍然是compound命令, proving that the compound priority is higher than the function
1
2

Validation: Priority of compound and function

[[Email protected] ~]# function CD () {echo ' my function CD ';}
[[Email protected] ~]# CD #执行的是自己的函数而不是内置命令cd, proving that the function is higher priority than the built-in command
Total 900
-rw-r--r--. 1 root root 0 Mar 10:10 4.txt
-RW-------. 1 root root 956 Mar 8 09:17 anaconda-ks.cfg
-rw-r--r--. 1 root root 0 Mar 10:09 a.txt
-rw-r--r--. 1 root root 0 Mar 10:10 D.txt
Drwxr-xr-x. 9 Jack Jack 4096 Mar 14:40 nginx-1.10.3
-rw-r--r--. 1 root root 911509 Mar 12:49 nginx-1.10.3.tar.gz


Summary II
==> Alias
==> Compound Commands
==> function
==> build_in

=================================part3
Search for $path when aliases and internal commands are not found

[Email protected] ~]# sed-n ' s/:/\n/gp ' <<< $PATH
/usr/local/sbin
/usr/local/bin
/sbin
/bin
/usr/sbin
/usr/bin
/root/bin


Because there are too many paths in this variable, there are many executables in each path.
It's obviously unwise to search every executable file under each path every time.
To reduce $path search, the content of the last search can be reused for the next execution
Bash creates a hash table for external commands that are searched from $path for caching
This cache is independently owned by the session level. Can not be shared with other endpoints because each user's $path may be different

[Email protected] ~]# hash-r #清除缓存
[Email protected] ~]# hostname #每执行一次就缓存一次
Server-leader
[[email protected] ~]# hash #查看缓存表, found a more cache

Hitscommand

1/bin/hostname

[Email protected] ~]# mv/bin/hostname/sbin/#将hostname命令移动到另外一个目录
[[email protected] ~]# hostname #再次执行hostname, is still read from the cache in the location of hostname, proving that the hash priority is higher than the path, the hash cache is the path path, and built-in commands such as CD and so there is no path, So there is no need for a verbose test, and the built-in priority must be higher than the hash.
BASH:/bin/hostname:no such file or directory

Summarize the order in which command execution is obtained:
==> Alias
==> Compound Commands
==> function
==> build_in
==> Hash
==> $PATH
==> Error:command not found

Six: Wildcard experiment

Special characters in Bash, and special characters that can be knocked out on the keyboard, have a special meaning, emphasizing one thing: metacharacters are interpreted by the shell

"Command in exchange for the execution of the order.

[[email protected] ~]# ls
4.txt anaconda-ks.cfg a.txt B.txt c.txt D.txt nginx-1.10.3 nginx-1.10.3.tar.gz
[[email protected] ~]# res= ' ls ' #取命令的运行结果, assign to variable res
[Email protected] ~]# echo $res #查看变量res的值
4.txt anaconda-ks.cfg a.txt B.txt c.txt D.txt nginx-1.10.3 nginx-1.10.3.tar.gz

$ () ibid, but it makes up for the "nesting flaw

[Email protected] ~]# res= ' echo ' ls ' #嵌套使用后无法达到预想的效果: Take the effect of the Echo pile file name.

[Email protected] ~]# echo $res
Ls

[Email protected] ~]# res=$ (echo $ (LS)) #替代方案
[Email protected] ~]# echo $res
4.txt anaconda-ks.cfg a.txt B.txt c.txt D.txt nginx-1.10.3 nginx-1.10.3.tar.gz

~ Home Directory

[Email protected] tmp]# CD ~
[Email protected] ~]# pwd
/root

! Take Non-

[Email protected] ~]# LS/DEV/SDA
SDA SDA1 Sda2
[[email protected] ~]# ls/dev/sda[0123]
/dev/sda1/dev/sda2
[[email protected] ~]# ls/dev/sda[!01]
/dev/sda2

! History command Invocation

[Email protected] ~]#!343
Hostname
Server-leader

! Match last History command

[Email protected] ~]#!ls
LS/DEV/SDA[!01]
/dev/sda2

! LS with a space to reverse the return value of the command

[[email protected] ~]# echo OK
Ok
[[email protected] ~]# echo $? #上一条命令执行的结果, 0 for execution success, not 0 for execution failure
0
[[email protected] ~]#! echo OK #将结果取反
Ok
[[email protected] ~]# echo $?
1

0-255, 0 is true, not 0-bit false

@ No special meaning

# Notes

$ variable Value
$ () Same as '
${} variable name range

$[] integer calculation echo $[2+3]-*/% floating point with echo "scale=3; 10/3 "| Bc-l

[Email protected] ~]# money=10
[Email protected] ~]# echo $money
10
[Email protected] ~]# echo 00000$money
0000010
[Email protected] ~]# echo $money 0000

[Email protected] ~]# echo ${money}0000
100000

% kill background process jobs number; Take the mold

^ Take non-and! Identical

[[email protected] ~]# ls/dev/sda[^01]
/dev/sda2
[[email protected] ~]# ls/dev/sda[!01]
/dev/sda2

^ Replace

[Email protected] ~]# systemctl Restart Network
[Email protected] ~]# ^network^sshd^
Systemctl Restart sshd

& Background Execution;&& logic and

* match any length string; Calculate multiplication

() executes in a child process

[Email protected] ~]# x=1
[Email protected] ~]# (x=666)
[Email protected] ~]# echo $x
1
[Email protected] ~]#
[Email protected] ~]# (X=666;echo $x)
666

-minus; interval; CD-;

_ No special meaning

+ PLUS sign;

= assigned value

| pipelines; | | Logical OR

Escape

{} Command List #括号内的开头和结尾必须是空格
# {LS; CD/;}

[] character wildcard, matching one of the parentheses;

: null Command truth

[Email protected] ~]#:
[[email protected] ~]# echo $?
0

; Multiple commands can be received: Ls;pwd;echo 123; Whether right or wrong, will continue to the last command

"Soft-cited" hard-cited

< input redirection

> Output redirection

>> Append

<< here Document

>& merging 2 and 1 outputs

, enumerate separators

. source; Current directory

/directory Separators

? Single character

Enter command execution

* Wildcard characters: any character
Wildcard characters: any character
One of the [ABC] list items
[^ABC] can also use the range [A-z] for the list to represent the aabbcc...,[0-9] for the 012345 ...
{} Loop List

[Email protected] @server-leader]# Touch {1..3}{a. D}.txt
[[email protected] @server-leader]# ls
1a.txt 1b.txt 1c.txt 1d.txt 2a.txt 2b.txt 2c.txt 2d.txt 3a.txt 3b.txt 3c.txt 3d.txt

Range of control variable names echo ${ab}c

Hard references and soft references

[Email protected] @server-leader]# x=1
[Email protected] @server-leader]# echo "$x" #双引号的代表软引用, special characters in quotation marks have special meanings, such as $, ' etc.
1
[[email protected] @server-leader]# echo ' $x ' #单引号代表硬引用, all characters in quotes have no special meaning
$x

\ Turn Meaning

[[email protected] @server-leader]# echo \ \
\
[[email protected] @server-leader]# echo \ '

[Email protected] @server-leader]# echo "'"

Network configuration and some shell commands at a glance

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.