1. 程式人生 > 其它 >debian10基操

debian10基操

一、配置網路源

#系統預設可能會使用光碟源,修改為網路源

su - root#切換到root使用者

nano /etc/apt/sources.list #設定源

#deb cdrom:[Debian GNU/Linux 10.9.0 _Buster #註釋掉光碟源這一行

#新增以下程式碼

deb http://mirrors.163.com/debian/ buster main non-free contrib

deb http://mirrors.163.com/debian/ buster-updates main non-free contrib

deb http://mirrors.163.com/debian/ buster-backports main non-free contrib

deb-src http://mirrors.163.com/debian/ buster main non-free contrib

deb-src http://mirrors.163.com/debian/ buster-updates main non-free contrib

deb-src http://mirrors.163.com/debian/ buster-backports main non-free contrib

deb http://mirrors.163.com/debian-security/ buster/updates main non-free contrib

deb-src http://mirrors.163.com/debian-security/ buster/updates main non-free contrib

我們使用163的Debian-security映象,Debian 10的髮型代號是buster

ctrl+o #儲存配置

ctrl+x #退出

apt-get update #更新軟體源

apt-get install sudo#安裝sudo命令

二、設定IP地址、閘道器、DNS

1、設定ip地址、閘道器

nano /etc/network/interfaces #編輯網絡卡配置檔案

auto lo

auto ens33 #開機自動連線網路

iface lo inet loopback

allow-hotplug ens33

iface ens33 inet static #static表示使用固定ip,dhcp表述使用動態ip

address 192.168.21.109 #設定ip地址

netmask 255.255.255.0 #設定子網掩碼

gateway 192.168.21.2 #設定閘道器

ctrl+o #儲存配置

ctrl+x #退出

2、設定DNS

nano /etc/resolv.conf #編輯配置檔案

nameserver 8.8.8.8 #設定首選dns

nameserver 8.8.4.4 #設定備用dns

ctrl+o #儲存配置

ctrl+x #退出

service networking restart #重啟網路

3、安裝ssh服務

OpenSSh 分為客戶端openssh-client 與服務端 openssh-server.一般情況下,我們的linux系統都會自帶 client端的。

apt-get install openssh-client
apt-get install openssh-server

配置檔案sshd_config

Port 22  #設定ssh監聽的埠號,預設22埠
ListenAddress ::
ListenAddress 0.0.0.0  #指定監聽的地址,預設監聽所有;
Protocol 2,1   #指定支援的SSH協議的版本號。'1'和'2'表示僅僅支援SSH-1和SSH-2協議。
#"2,1"表示同時支援SSH-1和SSH-2協議。#
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
HostKey /etc/ssh/ssh_host_ecdsa_key
HostKey /etc/ssh/ssh_host_ed25519_key    #HostKey是主機私鑰檔案的存放位置; 
#SSH-1預設是 /etc/ssh/ssh_host_key 。SSH-2預設是 /etc/ssh/ssh_host_rsa_key和
#/etc/ssh/ssh_host_dsa_key 。一臺主機可以擁有多個不同的私鑰。"rsa1"僅用於SSH-1,
#"dsa"和"rsa"僅用於SSH-2。
UsePrivilegeSeparation yes     #是否通過建立非特權子程序處理接入請求的方法來進行權
#限分 離。預設值是"yes"。 認證成功後,將以該認證使用者的身份創另一個子程序。這樣做的目的是
#為了防止通過有缺陷的子程序提升許可權,從而使系統更加安全。
KeyRegenerationInterval 3600   #在SSH-1協議下,短命的伺服器金鑰將以此指令設定的時
#間為週期(秒),不斷重新生成;這個機制可以儘量減小金鑰丟失或者黑客攻擊造成的損失。設為 0 
#表示永不重新生成為 3600(秒)。
ServerKeyBits 1024    #指定伺服器金鑰的位數
SyslogFacility AUTH   #指定 將日誌訊息通過哪個日誌子系統(facility)傳送。有效值是:
#DAEMON, USER, AUTH(預設), LOCAL0, LOCAL1, LOCAL2, LOCAL3,LOCAL4, LOCAL5, 
#LOCAL6, LOCAL7
LogLevel INFO     #指定日誌等級(詳細程度)。可用值如下:QUIET, FATAL, ERROR, INFO
#(預設), VERBOSE, DEBUG, DEBUG1, DEBUG2, DEBUG3,DEBUG 與 DEBUG1 等價;DEBUG2
# 和 DEBUG3 則分別指定了更詳細、更羅嗦的日誌輸出。比 DEBUG 更詳細的日誌可能會洩漏使用者
# 的敏感資訊,因此反對使用。
LoginGraceTime 120  #限制使用者必須在指定的時限(單位秒)內認證成功,0 表示無限制。預設
#值是 120 秒;如果使用者不能成功登入,在使用者切斷連線之前伺服器需要等待120秒。
PermitRootLogin yes  #是否允許 root 登入。可用值如下:"yes"(預設) 表示允許。
#"no"表示禁止。"without-password"表示禁止使用密碼認證登入。"forced-commands-only"
#表示只有在指定了 command 選項的情況下才允許使用公鑰認證登入,同時其它認證方法全部被禁止。
#這個值常用於做遠端備份之類的事情。
StrictModes yes       #指定是否要求 sshd(8) 在接受連線請求前對使用者主目錄和相關的配
#置檔案 進行宿主和許可權檢查。強烈建議使用預設值"yes"來預防可能出現的低階錯誤。
RSAAuthentication yes  #是否允許使用純 RSA 公鑰認證。僅用於SSH-1。預設值是"yes"。
PubkeyAuthentication yes  #是否允許公鑰認證。僅可以用於SSH-2。預設值為"yes"。
IgnoreRhosts yes    #是否取消使用 ~/.ssh/.rhosts 來做為認證。推薦設為yes。
RhostsRSAAuthentication no  #這個選項是專門給 version 1 用的,使用 rhosts 檔案在                  
#/etc/hosts.equiv配合 RSA 演算方式來進行認證!推薦no。
HostbasedAuthentication no    #這個與上面的專案類似,不過是給 version 2 使用的
IgnoreUserKnownHosts no          #是否在 RhostsRSAAuthentication 或 
#HostbasedAuthentication 過程中忽略使用者的 ~/.ssh/known_hosts 檔案。預設值是"no"。
#為了提高安全性,可以設為"yes"。
PermitEmptyPasswords no         #是否允許密碼為空的使用者遠端登入。預設為"no"。
ChallengeResponseAuthentication no   #是否允許質疑-應答(challenge-response)認         
#證。預設值是"yes",所有 login.conf中允許的認證方式都被支援。
PasswordAuthentication yes      # 是否允許使用基於密碼的認證。預設為"yes"。
KerberosAuthentication no    #是否要求使用者為 PasswordAuthentication 提供的密碼
#必須通 過 Kerberos KDC 認證,也就是是否使用Kerberos認證。使用Kerberos認證,伺服器
#需要一個可以校驗 KDC identity 的 Kerberos servtab 。預設值是"no"。
KerberosGetAFSToken no       #如果使用了 AFS 並且該使用者有一個 Kerberos 5 TGT,
#那麼開   啟該指令後,將會在訪問使用者的家目錄前嘗試獲取一個 AFS  token 。預設為"no"。
KerberosOrLocalPasswd yes   #如果 Kerberos 密碼認證失敗,那麼該密碼還將要通過其它
#的 認證機制(比如 /etc/passwd)。預設值為"yes"。
KerberosTicketCleanup yes    #是否在使用者退出登入後自動銷燬使用者的 ticket 。預設
#"yes"。
GSSAPIAuthentication no      #是否允許使用基於 GSSAPI 的使用者認證。預設值為"no"。
#僅用 於SSH-2。
GSSAPICleanupCredentials yes   #是否在使用者退出登入後自動銷燬使用者憑證快取。預設值      
#是"yes"。僅用於SSH-2。
X11Forwarding no    #是否允許進行 X11 轉發。預設值是"no",設為"yes"表示允許。如果
#允許X11轉發並且sshd代理的顯示區被配置為在含有萬用字元的地址(X11UseLocalhost)上監聽。
#那麼將可能有額外的資訊被洩漏。由於使用X11轉發的可能帶來的風險,此指令預設值為"no"。需
#要注意的是,禁止X11轉發並不能禁止使用者轉發X11通訊,因為使用者可以安裝他們自己的轉發器。如
#果啟用了 UseLogin ,那麼X11轉發將被自動禁止。
X11DisplayOffset 10    #指定X11 轉發的第一個可用的顯示區(display)數字。預設值                   
#是 10 。這個可以用於防止 sshd 佔用了真實的 X11 伺服器顯示區,從而發生混淆。
PrintMotd no                #登入後是否顯示出一些資訊呢?例如上次登入的時間、地點等
#等,預設是 yes ,但是,如果為了安全,可以考慮改為 no !
PrintLastLog yes           #指定是否顯示最後一位使用者的登入時間。預設值是"yes"
TCPKeepAlive yes       #指定系統是否向客戶端傳送 TCP keepalive 訊息。預設值是"yes"
#。這種訊息可以檢測到死連線、連線不當關閉、客戶端崩潰等異常。可以設為"no"關閉這個特性。
UseLogin no               #是否在互動式會話的登入過程中使用 login。預設值是"no"。
#如果開啟此指令,那麼 X11Forwarding 將會被禁止,因為 login 不知道如何處理 xauth 
#cookies 。需要注意的是,login是禁止用於遠端執行命令的。如果指定了 
#UsePrivilegeSeparation ,那麼它將在認證完成後被禁用。
MaxStartups 10        #最大允許保持多少個未認證的連線。預設值是 10 。到達限制後,
#將不再接受新連線,除非先前的連線認證成功或超出 LoginGraceTime 的限制。
MaxAuthTries 6     #指定每個連線最大允許的認證次數。預設值是 6 。如果失敗認證的次數超
#過這個數值的一半,連線將被強制斷開,且會生成額外的失敗日誌訊息。
UseDNS no          #指定是否應該對遠端主機名進行反向解析,以檢查此主機名是否與其IP
#地址真實對應。
Banner /etc/issue.net   #將這個指令指定的檔案中的內容在使用者進行認證前顯示給遠端使用者。
#這個特性僅能用於SSH-2,預設什麼內容也不顯示。"none"表示禁用這個特性。
Subsystem sftp /usr/lib/openssh/sftp-server   #配置一個外部子系統(例如,一個檔案
#傳輸守   護程序)。僅用於SSH-2協議。值是一個子系統的名字和對應的命令列(含選項和引數)。
UsePAM yes     #是否使用PAM模組認證

三、更改網絡卡名稱為eth*模式

預設安裝完成之後,伺服器網絡卡為ens模式,這臺伺服器是ens33,我們修改為eth模式。

cp /etc/default/grub /etc/default/grub-bak #備份配置檔案

sed -i '/GRUB_CMDLINE_LINUX=/s/"$/net.ifnames=0 biosdevname=0"/' /etc/default/grub #修改檔案

其實就是修改 /etc/default/grub檔案裡面的引數

GRUB_CMDLINE_LINUX="net.ifnames=0 biosdevname=0"

update-grub #使配置檔案生效

cp /etc/network/interfaces  /etc/network/interfaces-bak #備份配置檔案
sed -i 's/ens33/eth0/' /etc/network/interfaces  #替換ens33為eth0

reboot#重啟系統

四、系統引數優化

1、nano /etc/security/limits.conf #編輯新增以下優化資訊

#root

root soft core unlimited

root hard core unlimited

root soft nproc 1000000

root hard nproc 1000000

root soft nofile 1000000

root hard nofile 1000000

root soft memlock 32000

root hard memlock 32000

root soft msgqueue 8192000

root hard msgqueue 8192000

#*

* soft core unlimited

* hard core unlimited

* soft nproc 1000000

* hard nproc 1000000

* soft nofile 1000000

* hard nofile 1000000

* soft memlock 32000

* hard memlock 32000

* soft msgqueue 8192000

* hard msgqueue 8192000

ctrl+o #儲存配置

ctrl+x #退出

2、nano /etc/sysctl.conf #編輯新增以下優化引數

# Controls source route verification

net.ipv4.conf.default.rp_filter = 1

net.ipv4.ip_nonlocal_bind = 1

net.ipv4.ip_forward = 1

# Do not accept source routing

net.ipv4.conf.default.accept_source_route = 0

# Controls the System Request debugging functionality of the kernel

kernel.sysrq = 0

# Controls whether core dumps will append the PID to the core filename.

# Useful for debugging multi-threaded applications.

kernel.core_uses_pid = 1

# Controls the use of TCP syncookies

net.ipv4.tcp_syncookies = 1

# Disable netfilter on bridges.

# Controls the default maxmimum size of a mesage queue

kernel.msgmnb = 65536

# # Controls the maximum size of a message, in bytes

kernel.msgmax = 65536

# Controls the maximum shared segment size, in bytes

kernel.shmmax = 68719476736

# # Controls the maximum number of shared memory segments, in pages

kernel.shmall = 4294967296

# TCP kernel paramater

net.ipv4.tcp_mem = 786432 1048576 1572864

net.ipv4.tcp_rmem = 4096 87380 4194304

net.ipv4.tcp_wmem = 4096 16384 4194304

net.ipv4.tcp_window_scaling = 1

net.ipv4.tcp_sack = 1

# socket buffer

net.core.wmem_default = 8388608

net.core.rmem_default = 8388608

net.core.rmem_max = 16777216

net.core.wmem_max = 16777216

net.core.netdev_max_backlog = 262144

net.core.somaxconn = 20480

net.core.optmem_max = 81920

# TCP conn

net.ipv4.tcp_max_syn_backlog = 262144

net.ipv4.tcp_syn_retries = 3

net.ipv4.tcp_retries1 = 3

net.ipv4.tcp_retries2 = 15

# tcp conn reuse

net.ipv4.tcp_timestamps = 0

net.ipv4.tcp_tw_reuse = 0

net.ipv4.tcp_fin_timeout = 1

net.ipv4.tcp_max_tw_buckets = 20000

net.ipv4.tcp_max_orphans = 3276800

net.ipv4.tcp_synack_retries = 1

net.ipv4.tcp_syncookies = 1

# keepalive conn

net.ipv4.tcp_keepalive_time = 300

net.ipv4.tcp_keepalive_intvl = 30

net.ipv4.tcp_keepalive_probes = 3

net.ipv4.ip_local_port_range = 10001 65000

# swap

vm.overcommit_memory = 0

vm.swappiness = 10

#net.ipv4.conf.eth1.rp_filter = 0

#net.ipv4.conf.lo.arp_ignore = 1

#net.ipv4.conf.lo.arp_announce = 2

#net.ipv4.conf.all.arp_ignore = 1

#net.ipv4.conf.all.arp_announce = 2

ctrl+o #儲存配置

ctrl+x #退出

/sbin/sysctl -p#使配置立即生效

三、配置內網離線源

apt-mirror方式

1、下載apt-mirror

sudo apt install apt-mirror

2、配置

sudo nano  /etc/apt/mirror.list

deb 配置的是拖庫的目標地址 ,配置中選擇的是阿里雲映象站。預設的檔案下載存放地址是 /var/spool/apt-mirror/mirror/

deb http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
deb http://mirrors.aliyun.com/debian-security/ buster/updates main non-free contrib
deb-src http://mirrors.aliyun.com/debian-security/ buster/updates main non-free contrib

3、拉取

sudo apt-mirror

4、安裝apache2

sudo apt install apache2
ln -s /var/spool/apt-mirror/mirror/mirrors.aliyun.com/debian /var/www/html/debian
ln -s /var/spool/apt-mirror/mirror/mirrors.aliyun.com/debian-security /var/www/html/debian-security

5、在測試機上更換本地源

sudo nano /etc/apt/sources.list

deb http://本地IP/debian/ buster main non-free contrib
deb http://本地IP/debian/ buster-updates main non-free contrib
deb http://本地IP/debian/ buster-backports main non-free contrib
deb-src http://本地IP/debian/ buster main non-free contrib
deb-src http://本地IP/debian/ buster-updates main non-free contrib
deb-src http://本地IP/debian/ buster-backports main non-free contrib
deb http://本地IP/debian-security/ buster/updates main non-free contrib
deb-src http://本地IP/debian-security/ buster/updates main non-free contrib

6、 update

sudo apt update

7、更新時,提示缺少i18n資料夾下的檔案

wget -r -np  -erobots=off  -c -nH http://mirrors.aliyun.com/debian/dists/buster-backports/main/i18n/ -P  /var/spool/apt-mirror/mirror/mirrors.aliyun.com/
wget -r -np  -erobots=off  -c -nH http://mirrors.aliyun.com/debian/dists/buster-backports/contrib/i18n/ -P  /var/spool/apt-mirror/mirror/mirrors.aliyun.com/
wget -r -np  -erobots=off  -c -nH http://mirrors.aliyun.com/debian/dists/buster-backports/non-free/i18n/ -P  /var/spool/apt-mirror/mirror/mirrors.aliyun.com/
wget -r -np  -erobots=off  -c -nH http://mirrors.aliyun.com/debian-security/dists/buster/updates/main/i18n/ -P  /var/spool/apt-mirror/mirror/mirrors.aliyun.com/
wget -r -np  -erobots=off  -c -nH http://mirrors.aliyun.com/debian-security/dists/buster/updates/non-free/i18n/ -P  /var/spool/apt-mirror/mirror/mirrors.aliyun.com/