我從2017年開始,完全使用linux系統作為主力系統。一開始使用Ubuntu linux,后面轉到Manjaro linux。
下面是我遇到的一部分問題,主要有系統運維、軟件安裝和其它日常使用中的問題。
- 系統運維部分主要包括:防火牆管理、vi亂碼、nginx關閉header中的Server信息、禁止被ping等。
- 軟件安裝部分包括:安裝MariaDB、安裝PostgreSQL等。
- 其它日常使用中的問題主要包括:linux解壓縮文件時文件名亂碼,腳本批量去掉文件擴展名,linux死機,開啟熱點,通過expect實現腳本的自動交互等。
一、系統運維
1.1 防火牆開通端口
# 開放端口
firewall-cmd --zone=public --add-port=80/tcp --permanent;firewall-cmd --reload
# 向指定ip開放指定端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="172.17.6.52" port protocol="tcp" port="1521" accept"
#更新規則
firewall-cmd --reload
#重啟服務
systemctl restart firewalld.service
#刪除端口
firewall-cmd --zone=public --remove-port=1521/tcp --permanent
1.2 CentOS6開端口后仍然提示 "no route to host"
Your NRPE iptables chain does not appear to a have a default accept rule.
Unless you just want your own iptables chain for NRPE, you could use:
iptables -I INPUT -s 0/0 -p tcp --dport 5666 -j ACCEPT
This will add an accept rule to the default INPUT chain.
Otherwise, you need to add an accept rule to your NRPE chain.
iptables -I NRPE -s 0/0 -p tcp --dport 5666 -j ACCEPT
1.3 更新jar文件
有多種更新方式
- 用解壓縮軟件打開jar包,更新其中的文件
- 用vi打開jar包,修改其中的文件
- 使用jar命令,方法如下:
#將a文件添加到jar文件的指定路徑下
jar -uf abc.jar /META-INFO/a
1.4 VI編輯中文亂碼
編輯 ~/.vimrc
文件,增加以下內容
set fileencodings=utf-8,ucs-bom,gb18030,gbk,gb2312,cp936
set termencoding=utf-8
set encoding=utf-8
1.5 以其它用戶執行命令
root用戶可以用runuser命令,以其它用戶執行命令
runuser -l elva -c 'whoami'
1.6 Mongodb增加用戶
#切換數據庫,若不存在會自動創建
use test
# 創建用戶
db.createUser({user: "root", pwd: "123456", roles: [{ role: "dbOwner", db: "test" }]})
1.7 在完全不提供任何信息的情況下阻止ICMP請求
要完全隱藏此信息,您必須丟棄所有ICMP請求,這是以前iptables防火牆一貫使用的DROP大法。
將external的target設定為DROP,此時無法ping
firewall-cmd --zone=external --set-target=DROP --permanent
firewall-cmd --reload
1.8 nginx返回的header不顯示Server信息
vim /src/http/ngx_http_header_filter_module.c
# 要修改的配置
static u_char ngx_http_server_string[] = "Server: nginx" CRLF;
static u_char ngx_http_server_full_string[] = "Server: " NGINX_VER CRLF;
static u_char ngx_http_server_build_string[] = "Server: " NGINX_VER_BUILD CRLF;
# 修改為
static u_char ngx_http_server_string[] = "Server: unknow" CRLF;
static u_char ngx_http_server_full_string[] = "Server: unknow" CRLF;
static u_char ngx_http_server_build_string[] = "Server: unknow" CRLF;
#如果提示缺少PCRE庫
yum -y install pcre-devel
# 編譯
./configure --prefix=/usr/local/nginx && make && make install
1.9 docker路由導致局域網不能訪問
在10.0.134.105上,由於存在多余的網卡
執行命令ifconfig,除了docker0,還可以看到多個橋接網卡
#執行命令:docker network ls
正常情況顯示:
NETWORK ID NAME DRIVER SCOPE
b27a1db91825 bridge bridge local
c1d9192be39b host host local
0e3a57c7d22e none null local
#執行 docker network rm 網卡名,刪除多余的網卡
#修改/etc/docker/daemon.json,寫入 {"bip":"172.168.0.1/16"}
#如果不能用ip訪問docker的資源,檢查路由表是否完整
route add -net 172.168.0.0 netmask 255.255.0.0 dev docker0
1.9 NGINX Error: upstream sent invalid chunked response while reading upstream
We have solved the issue by adding the following to NGINX:
proxy_http_version 1.1
I guess NGINX proxies traffic by default with http version 1.0, but chunked transfer encoding is a http 1.1 feature.
nginx官網推薦:
For HTTP, the proxy_http_version directive should be set to “1.1
” and the “Connection” header field should be cleared:
server {
...
location /http/ {
proxy_pass http://http_backend;
proxy_http_version 1.1;
proxy_set_header Connection "";
...
}
}
1.10 nginx發布目錄后出現403錯誤
編輯nginx.conf文件,修改如下:
worker_processes 1;
user username;
其中username為實際用戶的名稱。為了保證安全性,可以使用www-data用戶。 nginx-403-forbidden-for-all-files
1.11 調整磁盤寫緩存參數
參見Low write performance on SLES 11 servers with large RAM和Better Linux Disk Caching & Performance with vm.dirty_ratio & vm.dirty_background_ratio。 對於大內存來說,需要考慮調整磁盤寫緩存。在/etc/sysctl.conf
中加入
#磁盤寫緩存上限(占總內存的百分比)
vm.dirty_ratio = 3
#內核flusher線程開始清理磁盤寫緩存的上限
vm.dirty_background_ratio = 2
- vm.dirty_ratio默認是10。按目前的標准配置,內存通常都有4GB,這就意味着磁盤的寫緩存有400MB(4GB*10%)。對於磁盤的讀取操作而言,大緩存並沒有問題,但是對於寫操作而言,過大的寫緩存將造成兩個重要風險,一是意外停機時丟失數據,二是累積太多的數據在內存,一旦開始集中寫入操作很容易造成長時間卡機。所以,系統內存較大時,應該將這個參數降下來。
- 調整vm.dirty_background_ratio的原理同上。這個參數一般設置為vm.dirty_ratio的1/4~1/2。
筆記本上如果安裝了laptop_mod之類的軟件,可能在開機時重新設置了vm.dirty_ratio參數,解決辦法:使用cron定時任務,執行命令sleep 5 && sudo sysctl --system
,其中sleep5秒是必要的。
Deprecation of /etc/sysctl.conf
2013-09-17 - Gaetan Bisson
From version 207 on, systemd will not apply the settings from /etc/sysctl.conf
anymore: it will only apply those from /etc/sysctl.d/*
. Since the settings of our /etc/sysctl.conf
shipped by procps-ng have become kernel defaults anyway, we have decided to deprecate this file.
Upon upgrading to procps-ng-3.3.8-3, you will be prompted to move any changes you made to /etc/sysctl.conf
under /etc/sysctl.d
. The easiest way to do this is to run:
pacman -Syu
mv /etc/sysctl.conf.pacsave /etc/sysctl.d/99-sysctl.conf
If you never customized /etc/sysctl.conf
, you have nothing to do.
1.12 優化Archlinux
按上節調整磁盤與緩存參數,能夠解決長時間開機后gnome-shell進程占用內存過大不能釋放的問題。
安裝preload,自動緩存常用的數據: yay -S preload
卸載多余的軟件:
yay -Rs empathy
sudo pacman -Rs gnome-calendar gnome-todo
禁止tracker-store
cp /usr/lib/systemd/user/tracker-store.service ~/.config/autostart
cp /usr/lib/systemd/user/tracker-miner-fs.service ~/.config/autostart
#編輯以上兩個文件,寫入內容:
X-GNOME-Autostart-enabled=false
Hidden=true
# 禁止tracker掃描磁盤
gsettings set org.freedesktop.Tracker.Miner.Files enable-monitors false
gsettings set org.freedesktop.Tracker.Miner.Files crawling-interval -2
gsettings set org.freedesktop.Tracker.Miner.Files ignored-files ['*']
刪除tracker的數據目錄
rm -rf ~/.local/share/tracker
rm -rf ~/.cache/tracker
1.13 用腳本重啟tomcat
cpwd=`pwd`
ps -ef |grep "$cpwd"|grep -v grep|awk '{print $2}'|xargs kill -s 9
rm -rf logs/*
rm -rf work/*
nohup sh bin/startup.sh &
echo "tomcat stated"
二、軟件安裝
2.1 安裝MariaDB
mariadb.server文件的位置:/usr/lib/systemd/system
安裝后,執行命令:mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql
然后重啟守護進程
Installing MariaDB/MySQL system tables in '/run/media/elva/data/database/mysql' ...
OK
To start mysqld at boot time you have to copy
support-files/mysql.server to the right place for your system
設置root密碼
PLEASE REMEMBER TO SET A PASSWORD FOR THE MariaDB root USER !
To do so, start the server, then issue the following commands:
'/usr/bin/mysqladmin' -u root password 'new-password'
'/usr/bin/mysqladmin' -u root -h bruce password 'new-password'
Alternatively you can run:
'/usr/bin/mysql_secure_installation'
which will also give you the option of removing the test
databases and anonymous user created by default. This is
strongly recommended for production servers.
See the MariaDB Knowledgebase at http://mariadb.com/kb or the
MySQL manual for more instructions.
You can start the MariaDB daemon with:
cd '/usr' ; /usr/bin/mysqld_safe --datadir='/run/media/elva/data/database/mysql'
You can test the MariaDB daemon with mysql-test-run.pl
cd '/usr/mysql-test' ; perl mysql-test-run.pl
Error: Access denied for user 'root'@'localhost'
編輯/etc/mysql/my.cnf,在[mysql]
下增加skip-grant-tables
;
重啟mysql,登錄mysql -u root -p
;
運行以下命令:
mysql> flush privileges;
mysql> alter user 'root'@'localhost' IDENTIFIED BY 'password'
去掉第一步在/etc/mysql/my.cnf中增加的語句,重啟mysql。
升級MySQL后
升級和啟動后,執行mysql_upgrade -u root -p
允許遠程連接
下面的命令將密碼設置為password
GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY 'password' WITH GRANT OPTION;
FLUSH PRIVILEGES;
重啟數據庫
更改數據目錄
數據目錄的默認位置是/var/lib/mysql
,帶權限復制到目標位置
sudo cp -R -p /var/lib/mysql /path
編輯/etc/mysql/my.conf
,增加或修改如下內容:
[client]
port = 3306
socket = /path/mysql.sock
[mysqld]
port = 3306
socket = /path/mysql.sock
datadir= /path/mysql
2.2 安裝PostgreSQL
安裝后執行命令:sudo su - postgres -c "initdb --locale en_US.UTF-8 -E UTF8 -D '/var/lib/postgres/data'"
啟動PostgreSQL
更改數據目錄
Create the new directory and assign it to user postgres
(you eventually have to become root):
mkdir -p /pathto/pgroot/data
chown -R postgres:postgres /pathto/pgroot
Become the postgres user(change to root, then postgres user), and initialize the new cluster:
initdb -D /pathto/pgroot/data
If not using systemd, edit /etc/conf.d/postgresql
and change the PGROOT variable(optionally PGLOG) to point to your new pgroot directory:
#PGROOT="/var/lib/postgres/"
PGROOT="/pathto/pgroot/"
If using systemd, edit /etc/systemd/system/multi-user.target.wants/postgresql.service
, which links to /usr/lib/systemd/system/postgresql.service
, and change the default PGROOT path.
#Environment=PGROOT=/var/lib/postgres/
Environment=PGROOT=/pathto/pgroot/
You will also need to change the default PIDFile path.
PIDFile=/pathto/pgroot/data/postmaster.pid
更改systemd的配置文件后,要重新加載:sudo systemctl daemon-reload
啟用postgis功能,需要執行:
CREATE EXTENSION postgis;
CREATE EXTENSION postgis_topology;
三、日常使用中的其它問題
3.1 linux上解壓縮文件文件名亂碼
由於zip格式中並沒有指定編碼格式,windows下生成的zip文件中的編碼是GBK/GB2312等,因此這些zip文件在linux下解壓縮時出現亂碼問題,linux下的默認編碼是utf8。
辦法一:安裝軟件unarchiver,它會自動檢測文件的編碼,解壓縮命令:unar $fileName
辦法二:unzip -O cp936,默認的unzip沒有-O這個選項,需要安裝unzip-iconv。
3.2 批量去掉文件擴展名
方法一、
for file in $(find . -name "*.txt" -type f);do mv "$file" "${file%.*}";done
方法二、
使用find 將文件名輸出到文件中,使用cut和xargs生成 mv命令
find . -name "*.png" >> name.txt
cut -d '.' -f 1 name.txt|xargs -i echo mv {}.png {} >>cmd.sh
./cmd.sh
3.3 ubuntu死機的處理
一般的linux上,ctrl + alt + F1 to F6是 getty/agetty程序提供的虛擬控制台,F7是X server運行的console。
在ubuntu 17.10 和更高版本中, X server轉移到TTY1和TTY2中,需要使用ctrl + alt + F1 或 F2.
當處理某個命令行的TTY時,還可以使用 Alt + left 或 Alt + right切換TTY
有下面的辦法:
1、sudo killall -1 gnome-shell
2、Alt
+ F2
, type "r" then Enter
3、Ctrl + Alt + Backspace kills the X11 interface and restarts it
3.4 刻錄啟動盤
- Etcher
- Rufus (selecting “dd mode”)
- A plain
sudo dd if=manjaro.iso of=/dev/sdd bs=4M status=progress
3.5 fxitx切換簡體和繁體
使用快捷鍵 ctl + shift + F,可以切換簡體和繁體
3.6 linux下使用sendmail
常見問題:
var/log/mail.log不停地報錯
My unqualified host name (aspire) unknown; sleeping for retry
解決辦法:
修改/etc/mail/local-host-names, 增加 127.0.0.1 localhost localhost.localhost
修改 /etc/hosts 增加 127.0.0.1 hostname hostname.hostname
sendmail會把計算機名作為域名加到主機名后,組成完整的長名 name.name來訪問
郵件內容在 /var/spool/mail
3.7 wineQQ 一直報錯,在/var/log/syslog中不停地增加日志“fixme:XXXX”
解決辦法:nohup wine "/home/elva/.wine/drive_c/Program Files/QQ/Bin/QQ.exe" & > /dev/null 2>&1
,這樣就沒有錯誤日志了
3.8 升級ubuntu18后,mindMaster打不開,報錯如下
/opt/MindMaster-6/libexec/QtWebEngineProcess: /opt/MindMaster-6/libexec/../lib/libz.so.1: version 'ZLIB_1.2.9' not found (required by /usr/lib/x86_64-linux-gnu/libpng16.so.16
解決辦法:下載zlib1.2.9版本,將編譯出來的文件,替換到mindmaster安裝目錄
3.9 升級ubuntu18后,xmind8打不開
報錯信息:
Exception in thread "main" java.awt.AWTError: Assistive Technology not found: org.GNOME.Accessibility.AtkWrapper
at java.awt.Toolkit.loadAssistiveTechnologies(Toolkit.java:807)
解決辦法:
卸載 openjdk-11-jre-headless, 使用 openjdk-8-jre-headless
修改openjdk的配置文件 /etc/java-8-openjdk/accessibility.properties
#assistive_technologies=org.GNOME.Accessibility.AtkWrapper
3.10 運行kvm提示無權限
修改qemu.conf文件
cd /etc/libvirt
sudo vi qemu.conf
# 修改內容如下,以實際用戶名為准
user = 'elva'
group = 'elva'
3.11 運行wireshark提示無權限
sudo groupadd wireshark
sudo chgrp wireshark /usr/bin/dumpcap
sudo chmod 4755 /usr/bin/dumpcap
sudo gpasswd -a xhz wireshark
3.12 ubuntu18的notify-send消息單獨顯示
- 在瀏覽器中安裝插件gnome shell intergration
sudo apt install chrome-gnome-shell
- 在·extensions.gnome.org 網站上,安裝插件Dash to Panel
- 在gnome tweak的Extensions里面啟用Dash to Panel插件
- 經過以上設置,系統消息會單獨顯示。
3.13 ubuntu18打不開android studio的Emulator
啟動模擬器時報錯:
08:24 Emulator: Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
08:25 Emulator: libGL error: unable to load driver: i965_dri.so
08:25 Emulator: libGL error: driver pointer missing
08:25 Emulator: libGL error: failed to load driver: i965
08:25 Emulator: libGL error: unable to load driver: swrast_dri.so
08:25 Emulator: libGL error: failed to load driver: swrast
解決方法:
ln -s /usr/lib/x86_64-linux-gnu/libstdc++.so.6 /media/elva/data/Android
/Sdk/emulator/lib
ANDROID_EMULATOR_USE_SYSTEM_LIBS=1 ./studio.sh
這個環境變量寫到/etc/profile中也不生效,變通的辦法是在一個shell中寫如下內容:
#!/bin/sh
export ANDROID_EMULATOR_USE_SYSTEM_LIBS=1
exec /opt/android-studio-ide-171.4408382-linux/android-studio/bin/studio_origin.sh
3.14 manjaro 恢復默認主題
pacman -S manjaro-gnome-assets manjaro-base-skel
3.15 查看固態硬盤支持標准
hdparm -I /dev/sda
本機顯示的信息如下,也就是支持SATA3.0,但不支持PCI-E
Transport: Serial, ATA8-AST, SATA II Extensions, SATA Rev 2.6, SATA Rev 3.0
3.16 docker配置鏡像
DaoCloud推出了加速器,編輯 /etc/docker/daemon.json
,寫入以下內容:
{
"registry-mirrors":["http://f1361db2.m.daocloud.io/","https://hub-mirror.c.163.com/"]
}
3.17 使用curl查看請求用時
curl -o /dev/null -s -w '%{time_total}' https://hub-mirror.c.163.com/
3.18 linux開機自啟
/etc/xdg/autostart
和~/.config/autostart
目錄下的都是開機自啟,以及利用 systemd。
3.19 Archlinux安裝vmware workstation
主要步驟:
yay -S vmware-workstation linux419-headers
3.20 開啟tcp_bbr
kernel 4.9+ 都支持bbr
執行的命令如下:
sudo modprobe tcp_bbr
#編輯/etc/sysctl.d/sys-99.conf,寫入以下內容
net.core.default_qdisc=fq
net.ipv4.tcp_congestion_control=bbr
#啟用
sudo sysctl --system
#check
lsmod | grep bbr
Cannot create multi-key binding on linux with swapped Ctrl and Caps
solution: use "keyboard.dispatch": "keyCode" in your settings and restart VS. reference
3.21 Chromium啟動時提示Enter your password to unlock your login keyring
編輯chromium.desktop文件
Exec=chromium --password-store=basic %U
--password-store Specifies which encryption storage backend to use.
Possible values are kwallet, kwallet5, gnome, gnome-keyring,
gnome-libsecret, basic. Any other value will lead to Chrome detecting
the best backend automatically. TODO(crbug.com/571003): Once
PasswordStore no longer uses the Keyring or KWallet for storing
passwords, rename this flag to stop referencing passwords. Do not rename
it sooner, though; developers and testers might rely on it keeping
large amounts of testing passwords out of their Keyrings or KWallets.
3.22 安裝lightdm pantheon主題
-
安裝lightdm-webkit2-greeter
yay -S lightdm-webkit2-greeter 編輯 /etc/lightdm/lightdm.conf 改為 greeter-session=lightdm-webkit2-greeter
此時生效的是默認的antergos主題
-
下載pantheon主題
git clone git@github.com:miko007/LightDM-Webkit-pantheon-theme.git
sudo mv LightDM-Webkit-pantheon-theme /usr/share/lightdm-webkit/themes
編輯/etc/lightdm/lightdm-webkit2-greeter.conf
[greeter]
webkit-theme = pantheon
- 重啟
sudo systemctl restart lightdm
3.23 XFCE-pulseaudio-plugin's icon is too large
vim ~/.config/gtk-3.0/gtk.css
#pulseaudio-button * { -gtk-icon-transform: scale(.6); }
重啟 xfce4-panel -r
3.24 linux開啟熱點
安裝create_ap,以及兩個依賴軟件hostapd和dnsmasq
開啟熱點命令:
sudo create_ap wlp2s0 enp3s0 bruce elva2016
報錯ERROR: Failed to initialize lock
,解決辦法:
rm /tmp/create_ap*.lock
提示 Operation not possible due to RF-kill
,解決辦法:
rfkill list
的結果如果包含soft blocked:yes hard blocked:no
,執行命令:
sudo rfkill unblock wifi
此時這兩項都變成no。
3.25 在virtualenv中安裝包報錯can not perform a '--user' install
辦法:修改 ~/.pip
目錄下的配置文件 pip.conf
, 增加一行 user=false
3.26 Jenkins構建成功后進程被kill
jenkins默認會關閉shell腳本的所有子進程,需要設置BUILD_ID變量才能防止進程被kill
3.27 Linux中通過expect實現腳本的自動交互
需要安裝tcl和expect
spawn 啟動新的交互進程, 后面跟命令或者指定程序
expect 從進程中接收信息, 如果匹配成功, 就執行expect后的動作
send 向進程發送字符串
send exp_send 用於發送指定的字符串信息
exp_continue 在expect中多次匹配就需要用到
send_user 用來打印輸出 相當於shell中的echo
interact 允許用戶交互
exit 退出expect腳本
eofexpect 執行結束, 退出
set 定義變量
puts 輸出變量
set timeout 設置超時時間
示例
#!/usr/bin/expect
set timeout 30
spawn ssh root@47.102.153.109
expect "password*"
send "your password\r"
interact
3.28 Redis升級后無法啟動
編輯 /usr/lib/systemd/system/redis.service,注釋掉 Type=notify
3.29 啟動Jenkins
nohup java -jar jenkins.war *--httpPort=8080 --prefix=/jenkins &*
3.30 xfce 桌面設置win+D快捷鍵顯示桌面
Reset/remove current shortcut:
xfconf-query --channel xfce4-keyboard-shortcuts --property "/xfwm4/custom/<Super>d" --reset
setup new
xfconf-query --channel xfce4-keyboard-shortcuts --property "/xfwm4/custom/<Super>d" --create --type string --set "show_desktop_key"
xfconf-query --channel xfce4-keyboard-shortcuts --list -v | grep -i super
3.31 alias命令包含單引號
這時可以使用 '"'"'
替代單引號。解釋一下:
' 使用單引號結束第一段;
" 開啟第二段,這里使用雙引號;
' 單引號本身;
" 結束第二段,使用雙引號;
' 開啟第三段,使用單引號。
將新增的文件添加到svn紀錄中
sub='svn st | awk '\''{if($1 == "?"){print $2}}'\''|xargs svn add'