Liunx筆記

人生長恨水發表於2019-04-10

記錄學習Liunx的筆記,內容比較雜。

配置centos7的網路

在建立的時候一定要選擇橋接網路,在centos7中檢視本機的ip地址不再是ifconfig命令,而是由ip addr來代替

$ ip addr
複製程式碼

通過ip addr命名查詢到我們網路卡的名字後,可以進入以下檔案中修改網路配置

$ vi /etc/sysconfig/network-scripts/ifcfg-enp0s3
# 在檔案中需要修改的內容如下:
# 把BOOTPROTO修改為static
BOOTPROTO=static
# 把ONBOOT修改為yes
ONBOOT=YES
# 新增以下內容
IPADDR=192.168.0.30 # 這個ip地址要對應這本機的ip,要處於同一個網段 
GATEWAY=192.168.0.1 # 閘道器地址
NETMASK=255.255.255.0 # 子網掩碼
DNS1=8.8.8.8 # DNS1
DNS2=114.114.114.114 # DNS2
複製程式碼

修改好以上後退出儲存重啟網路卡,修改DNS還可以在/etc/resolv.conf下新增nameserver 8.8.8.8修改,重啟後基本上就可以ping同外網了。

centos6的網路卡重啟方法:service network restart
centos7的網路卡重啟方法:systemctl restart network
複製程式碼

關閉防火牆

1.
啟動: systemctl start firewalld
關閉: systemctl stop firewalld
檢視狀態: systemctl status firewalld 
開機禁用  : systemctl disable firewalld
開機啟用  : systemctl enable firewalld
那怎麼開啟一個埠呢
新增
firewall-cmd --zone=public --add-port=2375/tcp --permanent    (--permanent永久生效,沒有此引數重啟後失效)
重新載入
firewall-cmd --reload
檢視
firewall-cmd --zone= public --query-port=2375/tcp
刪除
firewall-cmd --zone= public --remove-port=2375/tcp --permanent
 
2.systemctl是CentOS7的服務管理工具中主要的工具,它融合之前service和chkconfig的功能於一體。
啟動一個服務:systemctl start firewalld.service
關閉一個服務:systemctl stop firewalld.service
重啟一個服務:systemctl restart firewalld.service
顯示一個服務的狀態:systemctl status firewalld.service
在開機時啟用一個服務:systemctl enable firewalld.service
在開機時禁用一個服務:systemctl disable firewalld.service
檢視服務是否開機啟動:systemctl is-enabled firewalld.service
檢視已啟動的服務列表:systemctl list-unit-files|grep enabled
檢視啟動失敗的服務列表:systemctl --failed
 
3.配置firewalld-cmd
 
檢視版本: firewall-cmd --version
檢視幫助: firewall-cmd --help
顯示狀態: firewall-cmd --state
檢視所有開啟的埠: firewall-cmd --zone=public --list-ports
更新防火牆規則: firewall-cmd --reload
檢視區域資訊:  firewall-cmd --get-active-zones
檢視指定介面所屬區域: firewall-cmd --get-zone-of-interface=eth0
拒絕所有包:firewall-cmd --panic-on
取消拒絕狀態: firewall-cmd --panic-off
檢視是否拒絕: firewall-cmd --query-panic
複製程式碼

liunx升級核心

可以通過uname -r檢視自己的核心版本是多少

  • 匯入public key
$ rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
複製程式碼
$ rpm -Uvh https://www.elrepo.org/elrepo-release-7.0-3.el7.elrepo.noarch.rpm
複製程式碼
  • 安裝 kernel-lt(lt=long-term) 這裡需要注意的是,在 ELRepo 中有兩個核心選項,一個是 kernel-lt(長期支援版本),一個是 kernel-ml(主線最新版本),採用長期支援版本(kernel-lt),更穩定一些
$ yum --enablerepo=elrepo-kernel install kernel-lt -y
# 或者
$ yum --enablerepo=elrepo-kernel install kernel-ml -y
複製程式碼
  • centos6中編輯/etc/grub.conf檔案,修改Grub引導順序
# 因為一般新安裝的核心在第一個位置,所以設定default=0,表示啟動新核心
$ vim /etc/grub.conf 
# 之後重啟虛擬機器以後  可通過uname -r再次檢視核心版本
複製程式碼
  • centos7中進入/etc/grub2.cfg檔案中
$ vim /etc/grub2.cfg
複製程式碼

20190410132040

可以看到我有兩個系統核心,把核心為4.4的單引號中的內容複製,在命令列中執行以下命令

# 配置預設核心 
$ grub2-set-default 'CentOS Linux (4.4.153-1.el7.elrepo.x86_64) 7 (Core)'
# 檢視是否設定成功
$ grub2-editenv list
# 重啟liunx主機
$ reboot
複製程式碼

修改centos7的主機名稱

在CentOS 7中,有個叫hostnamectl的命令列工具,它允許你檢視或修改與主機名相關的配置,只檢視靜態、瞬態或靈活主機名,分別使用“--static”,“--transient”或“--pretty”選項。

  • 例如,要永久修改主機名,你可以修改靜態主機名:
$ hostnamectl --static set-hostname node01
複製程式碼
  • 修改/etc/hosts檔案新增
127.0.0.1 node01
$ reboot
複製程式碼

ubuntu中使用apt-get安裝的mysql如何登入 5.7版本

使用apt-get安裝的mysql不知道root的密碼,所以以root身份是登陸不上去的,這時我們需要做以下操作:

  • 其實 debian 系的 MySQL 安裝過程中會設定一個預設的賬戶,這個檔案裡儲存了預設賬號的資訊
$ cat /etc/mysql/debian.cnf
複製程式碼
  • 以上檔案內容中有賬戶和密碼,然後我們就可以用這個賬號登入 MySQL
$ mysql -u debian-default-account -p defaultpassword
複製程式碼
  • 設定root使用者的密碼
$ use mysql
$ set password for 'root'@'localhost' = password('yourpass');
$ flush privileges;
複製程式碼

Ubuntu配置網路(有可能是個例)

如果是雙網路卡的話,使用ip add顯示的網路卡名字有可能是p1p1 OR p3p1這種名字,單網路卡可能就是eth0,視情況而定

$ vi /etc/network/interfaces
# 新增以下內容
auto p3p1
iface p3p1 inet static
address 192.18.0.119
netmark 255.255.255.0
gateway 192.168.0.1
# 新增dns
$ vi /etc/resolv.conf
nameserver 223.5.5.5
nameserver 223.6.6.6
# 確保dns不會在重啟電腦後失效
$ vi /etc/resolvconf/resolv.conf.d/base
naseserver 223.5.5.5
nameserver 223.6.6.6
複製程式碼

執行命令生效dns

$ resolvconf -u
複製程式碼

執行命令生效網路

$ ifdown p3p1 && ifup p3p1
複製程式碼

linux清除快取

  • 檢視磁碟空間
# 檢視那些檔案比較大以及檢視檢視磁碟空間
$ du -h --max-depth=1
# 檢視記憶體情況
$ free -h
# 檢視磁碟空間
$ df -hl
複製程式碼
  • 清除記憶體快取
$ echo 1 > /proc/sys/vm/drop_caches
複製程式碼

linux使用者操作

給使用者多新增一個組

# 建議使用這種方式
$ gpasswd -a user_name group_name
OR
$ usermod -a -G 使用者組 使用者名稱
# -G:給使用者新增一個附屬組
複製程式碼

新增使用者並且允許使用者在建立好的資料夾中操作

# 建立使用者組
$ gourpadd kernel
# 新增使用者,主組是kernel,副組是wheel和adm,使用者名稱是ywh
$ useradd -s /bin/bash -d /home/ywh -m -g kernel -G wheel,adm ywh
$ mkdir kernelbuild
# 把這個檔案的許可權賦值給kernel組, -R 表示遞迴把下面所有的檔案都變成kernel組
$ chgrp -R kernel kernelbuild
# 操作的使用者也分給新建立的使用者ywh
$ chown -R ywh kernelbuidl
複製程式碼

普通使用者使用sudo時輸入root使用者的密碼

$ su
# 新增內容,記得把使用者新增到wheel或者sudo組內
$ vim /etc/sudoers
Defaults runaspw
複製程式碼

刪除一個使用者

$ userdel -r ywh
複製程式碼

Arch Linux 中安裝NFS服務

服務端

因為是Arch Linux的安裝方式,所以如果是其他的系統請自行查閱

# 安裝nfs服務
$ pacman -Sy nfs-utils
複製程式碼

配置nfs服務,假設要共享的目錄是 /mnt/nfs

# 建立要共享的資料夾
$ mkdir /mnt/nfs
# 配置nfs的共享目錄
$ vim /etc/exports

/mnt/nfs *(rw,sync,no_root_squash,no_subtree_check)

# 每一次修改exports檔案都需要執行以下命令
$ exportfs -ra

# 開啟nfs服務
$ systemctl start nfs-server.service
# 開機自啟服務
$ systemctl enable nfs-server.service
複製程式碼

客戶端

$ systemctl start rpcbind.service
$ systemctl start nfs-client.target
$ systemctl start remote-fs.target
$ systemctl enable rpcbind.service
$ systemctl enable nfs-client.target
$ systemctl enable remote-fs.target
複製程式碼

嵌入式裝置需要加引數 -o nolock,如果不是嵌入式裝置的話就不用加了

$ mount -t nfs -o nolock 192.168.1.122:/home/ywh/build /mnt/nfs
複製程式碼

對shell指令碼加密

shc是一個專業的加密shell指令碼的工具.它的作用是把shell指令碼轉換為一個可執行的二進位制檔案,這個辦法很好的解決了指令碼中含有IP、密碼等不希望公開的問題.

下載 shc-3.8.9b.tgz 以後上傳到liunx機器中對其進行解壓。

$ tar -xvf shc-3.8.9b.tgz
$ cd shc-3.8.9b
# 建立man1目錄是必須的,不然安裝過程中會報錯,shc將安裝命令到/usr/local/bin/目錄下;
# 將幫助文件存放在/usr/local/man/man1/目錄下,如果系統中無此目錄,安裝時會報錯,可建立此目錄後再執行安裝
$ mkdir -p /usr/local/man/man1
$ make install
# 接下來要輸入 yes
複製程式碼

使用方法如下

$ shc -r -f script-name
複製程式碼

執行後會生成兩個檔案,script-name.x 和 script-name.x.c. 其中script-name.x是加密後的可執行的二進位制檔案;用./script-name即可執行,script-name.x.c是生成script-name.x的原檔案(c語言).

自定義 system 服務

shell指令碼多行註釋

#!/bin/bash

:<<EOF
echo "1"
echo "1"
echo "1"
echo "1"
echo "1"
EOF

echo 2
複製程式碼

只輸出2

誤刪除libc.so.6的解決方法

刪除libc.so.6檔案後,會報錯

error while loading shared libraries: libc.so.6: cannot open shared object file: No such file or directory

可通過以下命令恢復

$ LD_PRELOAD=/lib64/libc-2.29.so ln -sf /lib64/libc-2.29.so /lib64/libc.so.6 
複製程式碼

libc.so.6 檔案其實就是連結的 libc-2.29.so 檔案,有可能每個人的版本不一樣,雖然不能使用ls or ll命令了,但是也是可以看到系統中的這個檔案的,然後通過上述命令就可以恢復了。

linux中jar包後臺執行

$ nohup java -jar shareniu.jar > temp.txt &
複製程式碼

輸出資訊重定向到temp.txt

sed操作

$ sed -i "s/oldstring/newstring/g" `grep oldstring -rl yourdir`
# 例如:替換/home下所有檔案中的www.bcak.com.cn為bcak.com.cn
$ sed -i "s/www.bcak.com.cn/bcak.com.cn/g" `grep www.bcak.com.cn -rl /home`
# 在指定字元後面新增內容,把BBB新增到1111後面
$ sed -i 's/指定的字元/&要插入的字元/'  檔案
$ sed -i  's/1111/&BBB/' /tmp/input.txt
# 在指定字元前面新增內容
$ sed -i 's/指定的字元/要插入的字元&/'  檔案
$ sed -i  's/1111/BBB&/' /tmp/input.txt
複製程式碼

sed中新增變數

在shell中拼接字串是兩個字串挨在一起就可以了不用向java中一樣加+號相連,所以在兩個字串中間寫變數即可

$ sed -i 's/name/'${usb1}'/' test.using
複製程式碼

shell字串擷取

相關文章