Linux基礎配置

做業一:完成做業未作完的集羣架構html

做業二:臨時配置網絡(ip,網關,dns)+永久配置node

臨時配置:nginx

[root@nfs-server ~]# ifconfig 
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.152.134  netmask 255.255.255.0  broadcast 192.168.152.255
        inet6 fe80::e33a:cec4:b0d4:c884  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:f7:dc:59  txqueuelen 1000  (Ethernet)
        RX packets 241828  bytes 275307222 (262.5 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7200  bytes 1083100 (1.0 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 327  bytes 33260 (32.4 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 327  bytes 33260 (32.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@nfs-server ~]# ifconfig ens32 192.168.152.135
[root@nfs-server ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
192.168.152.0   0.0.0.0         255.255.255.0   U     0      0        0 ens32
[root@nfs-server ~]# route add default gw 192.168.152.2 
[root@nfs-server ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.152.2   0.0.0.0         UG    0      0        0 ens32
192.168.152.0   0.0.0.0         255.255.255.0   U     0      0        0 ens32
[root@nfs-server ~]# cat /etc/resolv.conf 
# Generated by NetworkManager
search localdomain
nameserver 192.168.152.2
nameserver 202.106.0.20

永久配置:web

[root@nfs-server ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens32 
TYPE=Ethernet
BOOTPROTO=dhcp
IPADDR=192.168.152.134
NETMASK=255.255.255.0
GATEWAY=192.168.152.2
DNS1=192.168.152.2
DNS2=202.106.0.20
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens32
UUID=9eff3909-2614-4df9-80c9-a6a7d9f1be28
DEVICE=ens32
ONBOOT=yes

[root@nfs-server ~]# systemctl restart network

 

做業三:爲集羣內的機器設定主機名,利用/etc/hosts文件來解析本身的集羣中全部的主機名,相應的,集羣的配置應該改爲使用主機名的方式vim

# 修改主機名
[root@nfs-server ~]# hostnamectl set-hostname proxy-nfs [root@bogon ~]# hostnamectl set-hostname web01 [root@bogon ~]# hostnamectl set-hostname web02 [root@bogon ~]# hostnamectl set-hostname web03 [root@proxy-nfs ~]# echo "192.168.152.134 proxy-nfs" >> /etc/hosts [root@proxy-nfs ~]# echo "192.168.152.135 web01" >> /etc/hosts [root@proxy-nfs ~]# echo "192.168.152.136 web02" >> /etc/hosts [root@proxy-nfs ~]# echo "192.168.152.137 web03" >> /etc/hosts [root@web01 ~]# echo "192.168.152.134 proxy-nfs" >> /etc/hosts [root@web01 ~]# echo "192.168.152.135 web01" >> /etc/hosts [root@web01 ~]# echo "192.168.152.136 web02" >> /etc/hosts [root@web01 ~]# echo "192.168.152.137 web03" >> /etc/hosts [root@web02 ~]# echo "192.168.152.134 proxy-nfs" >> /etc/hosts [root@web02 ~]# echo "192.168.152.135 web01" >> /etc/hosts [root@web02 ~]# echo "192.168.152.136 web02" >> /etc/hosts [root@web02 ~]# echo "192.168.152.137 web03" >> /etc/hosts [root@web03 ~]# echo "192.168.152.134 proxy-nfs" >> /etc/hosts [root@web03 ~]# echo "192.168.152.135 web01" >> /etc/hosts [root@web03 ~]# echo "192.168.152.136 web02" >> /etc/hosts [root@web03 ~]# echo "192.168.152.137 web03" >> /etc/hosts [root@proxy-nfs ~]# egrep -v "#|^$|^#" /etc/nginx/nginx.conf # 修改配置文件已主機名方式解析 user nginx; worker_processes auto; error_log /var/log/nginx/error.log; pid /run/nginx.pid; include /usr/share/nginx/modules/*.conf; events { worker_connections 1024; } http { upstream luchuangao { server web01; server web02; server web03; } log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; include /etc/nginx/mime.types; default_type application/octet-stream; include /etc/nginx/conf.d/*.conf; server { listen 80 default_server; listen [::]:80 default_server; server_name _; root /usr/share/nginx/html; include /etc/nginx/default.d/*.conf; location / { proxy_pass http://luchuangao; } error_page 404 /404.html; location = /40x.html { } error_page 500 502 503 504 /50x.html; location = /50x.html { } } } [root@proxy-nfs ~]# /usr/sbin/nginx -t # 檢查配置文件是否正確 nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@proxy-nfs ~]# systemctl reload nginx # 從新加載配置文件 # 測試: [root@proxy-nfs ~]# curl 192.168.152.134 web01 [root@proxy-nfs ~]# curl 192.168.152.134 web02 [root@proxy-nfs ~]# curl 192.168.152.134 web03

 

做業四:ssh登陸,scp上傳、下載,ssh祕鑰登陸,修改ssh server端的端口爲8888而後進行登陸和scp測試緩存

SSH登陸:bash

[root@proxy-nfs ~]# scp /etc/hosts web01:/tmp
The authenticity of host 'web01 (192.168.152.135)' can't be established.
ECDSA key fingerprint is 4c:ee:a3:f1:5c:37:43:32:bd:06:4e:cc:3f:5a:0a:04.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'web01,192.168.152.135' (ECDSA) to the list of known hosts.
root@web01's password: 
hosts                                                                     100%  274     0.3KB/s   00:00    
[root@proxy-nfs ~]# scp web01:/tmp/hosts .
root@web01's password: 
hosts                                                                     100%  274     0.3KB/s   00:00    
[root@proxy-nfs ~]# ls -l hosts 
-rw-r--r-- 1 root root 274 Mar 21 15:05 hosts

祕鑰登陸:網絡

[root@proxy-nfs ~]# ssh-keygen 
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
29:90:f7:3a:24:0a:e2:f9:22:ec:f4:07:75:7c:10:6b root@proxy-nfs
The key's randomart image is:
+--[ RSA 2048]----+
|       ..        |
|     . ..        |
|    o oE.        |
|     +.+ o       |
|o   o + S        |
|o..o o o         |
|.+. . o          |
|ooo  . .         |
|o..o.            |
+-----------------+
[root@proxy-nfs ~]# ssh-copy-id -i web01
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@web01's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'web01'"
and check to make sure that only the key(s) you wanted were added.
[root@proxy-nfs ~]# 
[root@proxy-nfs ~]# ssh-copy-id -i web02
The authenticity of host 'web02 (192.168.152.136)' can't be established.
ECDSA key fingerprint is 4c:ee:a3:f1:5c:37:43:32:bd:06:4e:cc:3f:5a:0a:04.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@web02's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'web02'"
and check to make sure that only the key(s) you wanted were added.

[root@proxy-nfs ~]# ssh-copy-id -i web03
The authenticity of host 'web03 (192.168.152.137)' can't be established.
ECDSA key fingerprint is 4c:ee:a3:f1:5c:37:43:32:bd:06:4e:cc:3f:5a:0a:04.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@web03's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'web03'"
and check to make sure that only the key(s) you wanted were added.

# 修改SSH端口
[root@proxy-nfs ~]# ssh web01
Last login: Tue Mar 21 15:08:30 2017 from proxy-nfs
[root@web01 ~]# sed -i s'/^#Port 22/Port 8888/g' /etc/ssh/sshd_config 
[root@web01 ~]# grep 8888 /etc/ssh/sshd_config 
Port 8888
[root@web01 ~]# exit
logout
Connection to web01 closed.
[root@proxy-nfs ~]# ssh web02
Last login: Tue Mar 21 14:53:19 2017 from 192.168.152.1
[root@web02 ~]# sed -i s'/^#Port 22/Port 8888/g' /etc/ssh/sshd_config 
[root@web02 ~]# grep 8888 /etc/ssh/sshd_config 
Port 8888
[root@web02 ~]# exit
logout
Connection to web02 closed.
[root@proxy-nfs ~]# ssh web03
Last login: Tue Mar 21 14:53:20 2017 from 192.168.152.1
[root@web03 ~]# sed -i s'/^#Port 22/Port 8888/g' /etc/ssh/sshd_config 
[root@web03 ~]# grep 8888 /etc/ssh/sshd_config 
Port 8888
[root@web03 ~]# exit
logout
Connection to web03 closed.
[root@proxy-nfs ~]# 

# SCP 遠程拷貝文件
[root@proxy-nfs ~]# touch oldgirl.txt
[root@proxy-nfs ~]# scp oldgirl.txt web01:/tmp
oldgirl.txt                                                                                                                          100%    0     0.0KB/s   00:00    
[root@proxy-nfs ~]# scp web01:/etc/passwd .
passwd                                                                                                                               100% 2375     2.3KB/s   00:00

 

做業五:整理bash命令類型,驗證尋找一個命令的優先級架構

 

# 以ls命令爲例app

#一、ls命令有別名
[root@proxy-nfs ~]# alias  ls
alias ls='ls --color=auto'
#二、ls BASH自帶的命令
[root@proxy-nfs ~]# rpm -qf /usr/bin/cd
bash-4.2.46-20.el7_2.x86_64
#三、ls 命令以前操做事後會有hash緩存
[root@proxy-nfs ~]# hash
hits    command
   1    /usr/bin/egrep
   1    /usr/bin/hostname
   2    /usr/bin/systemctl
   1    /usr/bin/cat
   4    /usr/bin/vim
   1    /usr/bin/touch
   4    /usr/bin/ssh
   3    /usr/bin/ssh-copy-id
   4    /usr/bin/scp
   3    /usr/bin/curl
   3    /usr/bin/ls
   2    /usr/bin/ssh-keygen
   1    /usr/bin/clear
#四、根據PATH尋找路徑
[root@proxy-nfs ~]# which ls
alias ls='ls --color=auto'
        /usr/bin/ls
#五、若是命令不存在,則提示找不到
[root@proxy-nfs ~]# lls
-bash: lls: command not found

 

做業六:通配符實驗

#一、~ 家目錄
[root@proxy-nfs ~]# cd /tmp/
[root@proxy-nfs tmp]# cd ~
[root@proxy-nfs ~]# pwd
/root

#二、``命令的替換 取命令的執行結果
[root@proxy-nfs ~]# ret=`free -m`
[root@proxy-nfs ~]# echo $ret    
total used free shared buff/cache available Mem: 322 115 12 8 194 161 Swap: 2047 0 2047

[root@proxy-nfs test]# echo `ls`
1.txt 2.txt 3.txt 4.txt a.sh sshd_config
[root@proxy-nfs test]# a=`echo `ls``
[root@proxy-nfs test]# echo $a
ls
[root@proxy-nfs test]# b=$(echo $(ls))
[root@proxy-nfs test]# echo $b
1.txt 2.txt 3.txt 4.txt a.sh sshd_config
#儘可能使用括號

#三、!取非
[root@proxy-nfs test]# ls [!a-z].txt
1.txt  2.txt  3.txt  4.txt

#四、@ 無特殊含義
#五、# 註釋
[root@proxy-nfs test]# cat /etc/resolv.conf 
# Generated by NetworkManager

#六、	
$變量取值
[root@proxy-nfs test]# echo $ret
total used free shared buff/cache available Mem: 322 115 12 8 194 161 Swap: 2047 0 2047

$()同``
[root@proxy-nfs test]# ret=$(ls)
[root@proxy-nfs test]# echo $ret
1.txt 2.txt 3.txt 4.txt

${}變量名的範圍
[root@proxy-nfs test]# aa=old
[root@proxy-nfs test]# echo ${aa}girl
oldgirl

$[]整數計算echo $[2+3]-*/%浮點數用echo "sclae=3;10/3"|bc -l
[root@proxy-nfs test]# echo $[1 + 1]
2

#七、殺後臺進程jobs號;取莫
殺後臺進程jobs號
kill -9 %1
#取模
[root@proxy-nfs test]# echo $[10%3]
1

#八、^取非和!雷同
[root@proxy-nfs test]# ls [^01].txt
2.txt  3.txt  4.txt

#九、& 後臺進程
[root@proxy-nfs test]# ./test.sh &
邏輯與
[root@proxy-nfs test]# ls && pwd
1.txt  2.txt  3.txt  4.txt
/test

#十、*通配符;任意字符
[root@proxy-nfs test]# ls *
1.txt  2.txt  3.txt  4.txt
[root@proxy-nfs test]# ls *.txt
1.txt  2.txt  3.txt  4.txt

#十一、()在子進程中執行
[root@proxy-nfs test]# a=1
[root@proxy-nfs test]# (a=2;echo $a)
2
[root@proxy-nfs test]# echo $a
1

#十二、-減號;區間;cd-;
減號
echo $[5 - 1]
區間
ls [a-z].txt
返回上一次工做的目錄
cd -

#1三、|管道;||邏輯或
管道
[root@proxy-nfs test]# netstat -tunapl|grep nginx
tcp        0      0 0.0.0.0:80              0.0.0.0:*               LISTEN      9440/nginx: master  
tcp6       0      0 :::80                   :::*                    LISTEN      9440/nginx: master
邏輯或
[root@proxy-nfs test]# ls || pwd
1.txt  2.txt  3.txt  4.txt

#1四、{}命令列表#括號內的開頭和結尾必須是空格
[root@proxy-nfs test]# { ls;cd /; }
1.txt  2.txt  3.txt  4.txt
[root@proxy-nfs /]# 

#1五、\轉義
[root@proxy-nfs ~]# rm -f \[1\].txt 

#1六、:空命令 真值
[root@proxy-nfs ~]# :
[root@proxy-nfs ~]# echo $?
0

#1七、;能夠接多個命令,不管對錯,會一直執行到最後一條命令
[root@proxy-nfs ~]# pwd;who
/root
root     tty1         2017-03-21 09:35
root     pts/1        2017-03-21 14:53 (192.168.152.1)

#1八、「」 軟引 ‘’硬引
[root@proxy-nfs ~]# aa=oldboy
[root@proxy-nfs ~]# echo $aa
oldboy
[root@proxy-nfs ~]# echo "$aa"   #軟引用
oldboy
[root@proxy-nfs ~]# echo '$aa'   #硬引用
$aa
[root@proxy-nfs ~]# 

#1九、,枚舉分隔符
[root@proxy-nfs ~]# echo {a,b,c,d}
a b c d

#20、<輸入重定向

#2一、>輸出重定向
echo aa > aa.txt

#2二、追加
echo bb > aa.txt

#2三、. source 當前目錄
[root@proxy-nfs test]# ./a.sh 
1.txt  2.txt  3.txt  4.txt  a.sh
[root@proxy-nfs test]# source a.sh 
1.txt  2.txt  3.txt  4.txt  a.sh

#2四、/ 目錄分隔符
[root@proxy-nfs test]# pwd
/test/

#2五、?通配符:任一字符
[root@proxy-nfs test]# echo ????
a.sh
相關文章
相關標籤/搜索