Gentoo 的 Minimal Install CD 每週都會自動構建一個版本,服務器通常使用最新的 x86_64 版本。 國內的鏡像下載地址是:php
用 Minimal Install CD 光盤啓動進入到 livecd 的環境,在安裝前須要設置網絡環境。 若是網絡環境裏配置了 DHCP,經過命令 ifconfig 命令檢查機器是否分配到IP。linux
能夠經過下面的幾條命令自行設置,也可使用 net-setup 命令進行設置:算法
ifconfig eth0 192.168.1.10/24 #設置IP地址:192.168.1.10 route add default gw 192.168.1.1 #設置網關:192.168.1.1 echo nameserver 192.168.1.1 > /etc/resolv.conf #設置域名解析服務器
Importantvim
實際過程當中有可能遇到從 Minimal Install CD 啓動後,系統沒法找到網卡,使得安裝沒法進行下去。遇到此類問題須要查看網卡的硬件狀況,查看是否有加載對應的驅動模塊。譬如在 DELL R610 機型中使用 Broadcom Corporation NetXtreme II 5709C 雙端口千兆以太網卡時遇到雖然加載了驅動模塊,可是依然沒法識別網卡的狀況,最終先將驅動模塊刪除再從新加載後才使得網卡設備被識別。bash
lspci -k #查詢到網卡的驅動模塊爲bnx2。 lsmod #查詢是否加載了bnx2驅動模塊。 rmmod bnx2 #移除加載的bnx2驅動模塊。 modprobe bnx2 #從新加載驅動模塊。 net-setup eth0 #設置網絡。
Important服務器
若是使用新版本的 Minimal Install CD,因爲 udev 版本已經升級到 200 以上,不必定能使用 eth0 來鏈接網絡,新的鏈接的名稱能夠用如下代碼來得到:網絡
udevadm test-builtin net_id /sys/class/net/eth0 2> /dev/null
名稱通常爲 enps。session
啓動 SSH 服務:app
/etc/init.d/sshd start #啓動ssh服務
設置 root 用戶的密碼:dom
passwd
如今可使用 PuTTY 或者 Terminal 遠程登陸到機器進行安裝。
fdisk /dev/sda The number of cylinders for this disk is set to 6527. There is nothing wrong with that, but this is larger than 1024, and could in certain setups cause problems with: 1) software that runs at boot time (e.g., old versions of LILO) 2) booting and partitioning software from other OSs (e.g., DOS FDISK, OS/2 FDISK) Command (m for help):
fdisk 的幾個基本命令:
使用這些命令爲硬盤建立一個 boot 分區,一個 swap 區和一個 root 分區:
Disk /dev/sda: 53.7 GB, 53687091200 bytes 255 heads, 63 sectors/track, 6527 cylinders Units = cylinders of 16065 * 512 = 8225280 bytes Disk identifier: 0x5f13998e Device Boot Start End Blocks Id System /dev/sda1 * 1 17 136521 83 Linux /dev/sda2 18 83 530145 82 Linux swap / Solaris /dev/sda3 84 6527 51761430 83 Linux
使用 w 命令保存結果退出。
Important
建立兩塊硬盤使用 RAID 1 的狀況,須要採用相同的分區格式化 sda 和 sdb 兩塊硬盤,而且兩塊硬盤的 root 分區和 boot 分區都指定爲 Linux raid autodetect 類型( fdisk 中用 t 命令指定分區類型時輸入編號 fd )。
Device Boot Start End Blocks Id System /dev/sda1 * 1 17 136521 83 Linux raid autodetect /dev/sda2 18 83 530145 82 Linux swap / Solaris /dev/sda3 84 6527 51761430 83 Linux raid autodetect
因爲 Grub 不支持 ext4 的 boot 分區,因此 boot 分區格式化爲 ext2 類型, root 分區能夠格式化 ext3 或者 ext4 類型。
mke2fs /dev/sda1 #格式化ext2的boot分區 mke2fs -j /dev/sda3 #格式化ext3的root分區 mkswap /dev/sda2 && swapon /dev/sda2 #初始化和激活交換分區 #掛載分區 mount /dev/sda3 /mnt/gentoo mkdir /mnt/gentoo/boot mount /dev/sda1 /mnt/gentoo/boot cd /mnt/gentoo
格式化 ext4 類型的 root 分區。
mkfs.ext4 /dev/sda3 #格式化ext4的root分區
Important
建立兩塊硬盤使用 RAID 1 的狀況,須要先使用 partprobe 命令從新讀取硬盤的分區表。
partprobe /dev/sda partprobe /dev/sdb
使用 mknod 和 mdadm 建立節點和設備。
mknod /dev/md1 b 9 1 mknod /dev/md3 b 9 3 mdadm --create /dev/md1 --level=1 --raid-devices=2 --metadata=0.90 /dev/sda1 /dev/sdb1 mdadm --create /dev/md3 --level=1 --raid-devices=2 /dev/sda3 /dev/sdb3
格式化建立的節點和交換分區。
mke2fs /dev/md1 mke2fs -j /dev/md3 mkswap /dev/sda2 && mkswap /dev/sdb2 swapon -p 1 /dev/sda2 && swapon -p 1 /dev/sdb2
掛載 md1 和 md3 兩個節點。
mount /dev/md3 /mnt/gentoo mkdir /mnt/gentoo/boot mount /dev/md1 /mnt/gentoo/boot cd /mnt/gentoo
使用 links 命令下載最新版本的 x86_64 Stage3 和 portage,而後進行解壓到當前目錄。
links http://mirrors.sohu.com tar xvjpf stage3-*.tar.bz2 tar xvjf /mnt/gentoo/portage-latest.tar.bz2 -C /mnt/gentoo/usr
的環境是沒有 VIM 的,只能使用 nano 編輯配置文件。
nano -w etc/portage/make.conf
在 make.conf 中添加 MAKEOPTS 參數,使用 MAKEOPTS 能夠定義在安裝軟件的時候同時編譯的數量,通常是 CPU 的數目加 1。
MAKEOPTS="-j3"
添加 unicode 的全局編譯參數,保證編譯的軟件支持 unicode 編碼。
USE="bindist unicode bash-completion vim-syntax jpeg png mmx sse sse2"
添加 GENTOO_MIRRORS 和 SYNC 參數,指定下載的鏡像地址
GENTOO_MIRRORS="http://mirrors.sohu.com/gentoo/" SYNC="rsync://rsync.cn.gentoo.org/gentoo-portage"
其它的一些經常使用的設置項
APACHE2_MODULES="authz_host cgi dir mime actions alias asis auth_basic auth_digest authn_alias authn_anon authn_dbd authn_dbm authn_default authn_file authz_dbm authz_default authz_groupfile authz_owner authz_user autoindex cache cern_meta cgid charset_lite dav dav_fs dav_lock dbd deflate disk_cache dumpio env expires ext_filter file_cache filter headers ident imagemap include info log_config log_forensic logio mem_cache mime_magic negotiation proxy proxy_ajp proxy_balancer proxy_connect proxy_ftp proxy_http reqtimeout rewrite setenvif speling status substitute unique_id userdir usertrack version vhost_alias unixd socache_shmcb authn_core authz_core" NGINX_MODULES_HTTP="addition fastcgi flv geo geoip sub access auth_basic charset dav gzip gzip_static image_filter limit_req limit_zone map memcached perl proxy realip referer rewrite secure_link ssi stub_status upstream_ip_hash userid xslt random autoindex browser cache_purge degradation empty_gif headers_more push random_index scgi split_clients uwsgi slowfs_cache upload upload_progress" NGINX_MODULES_MAIL="imap pop3 smtp" ACCEPT_LICENSE="*" PHP_TARGETS="php5-4" PHP_INI_VERSION="production" LINGUAS="en_US zh_CN zh_TW"
cd / mount -t proc proc /mnt/gentoo/proc mount -o bind /dev /mnt/gentoo/dev cp -L /etc/resolv.conf /mnt/gentoo/etc/ chroot /mnt/gentoo /bin/bash env-update && source /etc/profile export PS1="(chroot) $PS1" #盤符從livecd變爲(chroot)。
時區的配置文件都保存在 /usr/share/zoneinfo 中。
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
Important
當服務器提供的服務須要爲跨時區的應用提供服務時,且使用 MongoDB 和 NodeJS 時要將時區設爲 GMT0,避免由於服務器的時區形成數據的時間錯誤,減小客戶端在時區判斷上判斷的流程。
若是須要把主機設置成格林威治標準時間,則把 GMT0 文件拷貝到 /etc/localtime:
cp /usr/share/zoneinfo/GMT0 /etc/localtime
爲了可以使用中文,在 locale 中添加中文 UTF-8 的支持,修改 /etc/locale.gen 文件。
nano -w /etc/locale.gen
添加:
en_US.UTF-8 UTF-8 zh_CN.UTF-8 UTF-8 zh_CN.GBK GBK
執行 locale-gen 命令讓修改的 locale 生效。
locale-gen
設定主機的名稱。
nano -w /etc/conf.d/hostname
修改文件中的 hostname 爲 gentoo 。
emerge -avt gentoo-sources genkernel zcat /proc/config.gz > /root/config/kernel-3.8.13.config genkernel --color --loglevel=5 --menuconfig --save-config --makeopts=-j9 --kernel-config=/root/config/kernel-3.8.13.config all
編譯後會在 /boot 目錄下生成內核文件。
Important
不少軟件和驅動的運行都須要有內核的支持,具體的依賴關係在此文的附錄中有一些說明,這裏只強調使用 RAID 1 模式時須要選擇 Multi-device support (RAID and LVM) 下的所有編譯選項
在默認的 fstab 文件中,BOOT、ROOT 和 SWAP 須要用實際的分區名替換掉。
nano -w /etc/fstab /dev/sda1 /boot ext2 noauto,noatime 1 2 /dev/sda3 / ext3 noatime 0 1 /dev/sda2 none swap sw 0 0
若是是 ext4 文件系統,須要將對應的 ext3 改成 ext4 :
/dev/sda3 / ext4 noatime 0 1
系統啓動時會讀取 fstab 中的配置並自動對硬盤進行掛載,所以若是有其餘硬盤須要自動掛載,也需在 fstab 中進行配置。例如服務器有第二塊硬盤 /dev/sdb 並分紅一個分區 /dev/sdb1 ,文件系統爲 ext4,須要在系統啓動時掛載到 /opt,則需在 fstab 中加入以下一行配置:
/dev/sdb1 / ext4 noatime 0 1
Important
RAID 1 狀況的 fstab 文件寫法不一樣,主要是分區名應該爲建立的節點名,沒有爲交換分區建立節點,因此2個 swap 分區都要寫到 fstab 文件中。
/dev/md1 /boot ext2 noauto,noatime 1 2 /dev/md3 / ext3 noatime 0 1 /dev/sda2 none swap sw 0 0 /dev/sdb2 none swap sw 0 0
boot 分區的啓動參數中 noauto 的意思是說啓動分區不自動 mount 到系統中。
配置系統的網絡地址、啓動 SSH 服務、設置 root 用戶的密碼、安裝一些系統的基本服務和在系統啓動時啓動一些服務。 eth0 根據不一樣機器須要改爲相應名字。
echo 'config_eth0="192.168.1.10/24" ' >> /etc/conf.d/net echo 'routes_eth0="default via 192.168.1.1" ' >> /etc/conf.d/net echo 'dns_servers_eth0="192.168.1.1 8.8.8.8" ' >> /etc/conf.d/net passwd emerge -avt syslog-ng vixie-cron pciutils gentoolkit rc-update add sshd default rc-update add syslog-ng default rc-update add vixie-cron default
Important
RAID 1 須要 mdadm 在啓動的時候就運行,因此須要在系統安裝完成以前就安裝,並把它的啓動級別設爲 boot 。
emerge -avt mdadm mdadm --detail --scan >> /etc/mdadm.conf rc-update add mdadm boot
Gentoo 推薦使用 Grub 來引導系統, 這裏介紹的配置文件寫法不涉及多個系統共存,安裝 grub 後配置 grub.conf 文件。
emerge -avt grub nano -w /boot/grub/grub.conf
default 0 timeout 30 splashimage=(hd0,0)/boot/grub/splash.xpm.gz title Gentoo Linux 3.8.13 root (hd0,0) kernel /boot/kernel-genkernel-x86_64-3.8.13-gentoo root=/dev/ram0 init=/linuxrc ramdisk=8192 real_root=/dev/sda3 rootfstype=ext4 initrd /boot/initramfs-genkernel-x86_64-3.8.13-gentoo
Important
若是是 ext4 的分區,須要在 kernel 一行加入 rootfstype=ext4 的參數。
使用 grub-install 命令安裝。
grep -v rootfs /proc/mounts > /etc/mtab grub-install --no-floppy /dev/sda
Important
RAID 1 的模式不能使用 grub-install 命令安裝,只能使用 grub 命令進行安裝。
grub grub> root (hd0,0) Filesystem type is ext2fs, partition type 0xfd grub> setup (hd0) Checking if "/boot/grub/stage1" exists... yes Checking if "/boot/grub/stage2" exists... yes Checking if "/boot/grub/e2fs_stage1_5" exists... yes Running "embed /boot/grub/e2fs_stage1_5 (hd0)"... 16 sectors are embedded. succeeded Running "install /boot/grub/stage1 (hd0) (hd0)1+16 p (hd0,0)/boot/grub/stage2 /boot/ grub/menu.lst"... succeeded Done. grub> root (hd1,0) Filesystem type is ext2fs, partition type 0xfd grub> setup (hd1) grub> quit
exit umount /mnt/gentoo/dev /mnt/gentoo/proc /mnt/gentoo/boot /mnt/gentoo reboot
通過這樣的安裝有了一個最基本的系統。
emerge --sync emerge -avt gentoolkit emerge --update --deep --newuse -avt @world revdep-rebuild etc-update
完成後就把當前系統更新到最新的版本,能夠開始對系統進行更詳細的定製。
修改 /etc/conf.d/hostname :
hostname="abc"
修改 /etc/conf.d/net :
dns_domain="xxxxx.com"
最終在 console 的登陸上顯示爲 abc.xxxxx.com 。
使用 ssh-keygen 建立 rsa 算法的 key:
ssh-keygen -t rsa -C 'abc.xxxxx.com' cp id_rsa.pub authorized_keys
生成的 id_rsa 私鑰拷貝到須要登陸的機器,修改文件權限爲 400。修改 /etc/ssh/sshd_config 配置文件:
Port 18402 AddressFamily any PasswordAuthentication no ChallengeResponseAuthentication no
使用 ssh 命令登陸到機器的命令時使用 -i 和 -p 兩個參數:
ssh -i .ssh/id_rsa.dashboard -p 18402 root@222.222.222.222
修改 shorewall.conf 配置文件:
STARTUP_ENABLED=Yes
在 interfaces 文件中加入網卡,其中 enp4s0 爲當前機器的網卡名:
net enp4s0 tcpflags,nosmurfs,routefilter,logmartians,sourceroute=0
在 zones 中加入網絡的設置:
net ipv4
修改 policy 文件:
$FW net ACCEPT net all DROP info all all REJECT info
修改 rules 文件,加入 18402 端口的開放:
#Don't allow connection pickup from the net # Invalid(DROP) net all tcp Invalid(DROP) net all udp # # Allow Ping from the local network # Ping(ACCEPT) net $FW # # Accept 80(Nginx) connection from internet # ACCEPT net $FW tcp 80 # # Accept 18402(SSH) connection from internet # ACCEPT net $FW tcp 18402
root soft noproc 65535 root hard noproc 65535 root soft nofile 65535 root hard nofile 65535
root 表明用戶,若是設置任意用戶,可使用 *****,noproc 爲進程數量,nofile 爲文件數量,root soft noproc 65535 即 root 用戶可以打開的最大進程數的軟限制是 65535。注意軟限制不能超過硬限制。
session required /lib/security/pam_limits.so
登錄時調用 pam_limits.so,該庫將會從 /etc/security/limits.conf 中讀取配置參數並自動設置。
echo "65535" > /proc/sys/fs/file-max
注意 /proc/sys/fs/file-max 不能直接用編輯工具修改。
worker_rlimit_nofile 51200; #修改最大文件數爲51200 events { use epoll; worker_connections 51200; #修改worker的最大鏈接數爲51200 }
net.core.rmem_max = 33554432 net.core.wmem_max = 33554432 net.ipv4.tcp_rmem = 4096 16384 33554432 net.ipv4.tcp_wmem = 4096 16384 33554432 net.ipv4.tcp_mem = 786432 1048576 26777216 net.ipv4.tcp_max_tw_buckets = 360000 net.core.netdev_max_backlog = 2500 vm.min_free_kbytes = 65536 vm.swappiness = 0 net.ipv4.ip_local_port_range = 1024 65535
在 DELL R610 中使用 20120516 的 LiveCD 安裝後啓動出現錯誤:
bnx2: Can't load firmware file "bnx2/bnx2-mips-06-06.2.1.fw"
最終修改內核:
CONFIG_FIRMWARE_IN_KERNEL=N
安裝 linux-firmware:
emerge -avt linux-firmware