Linux系统的常用命令和服务器配置 一、 添加用户useradd命令 例子: #useradd –m –g users –G wheel,sales –s /bin/tcsh –c “a user” myname
#useradd -c "ftp Administrator" -d /var/ftp/ -G ftpAdmin -s /sbin/nologin -r ftpAdmin
-m 自动创建用户的主目录,并把框架目录(/etc/skel)文件复制到用户目录上。 -g 设置基本组,用户将在该组中。 -G 把用户加到所有逗号间隔的分组中。 -s 制定使用的shell。 -c 描述信息。 -d 为账号设置主目录。 -r 系统帐号,无需再/home下创建主目录。 最后的是用户名。 二、 指定口令(密码)passwd 例子: #passwd 用户名 回车就会提示输入密码了。 三、 删除用户userdel 例子: #userdel 用户名 #userdel –r 用户名 -r 删除其主目录(/home/„..) 四、 文件系统命令 4.1 fdisk命令 (1)fdisk –l :显示硬盘上的所有分区。既分区类型(FAT32、Ext3)。 (2)df –h :显示文件系统是如何挂载的。 (3)fdisk /dev/hdb1 :格式化第二个IDE磁盘 4.2 mount命令挂载文件系统 (1)mount (不使用参数)查看系统挂载情况。 (2)mount /mnt/cdrom :挂载光驱,可用cd /mnt/cdrom 后ls查看光盘上的内容。 (3)mount /mnt/floppy :挂载软盘,可用cd /mnt/floppy 后ls查看软盘上的内容。 2、3中可以使用/dev/cdrom和/dev/fd0代替挂载点,得到同样的效果! (4)mount –t msdos /dev/fd0 /mnt/floppy :挂载DOS软盘到/mnt/floppy下。 (5)挂载Windows分区 #fdisk -l :列出硬盘分区。 #mkdir /mnt/win :创建一个目录,用于挂载。 #mount -t vfat /dev/hda1 /mnt/win :假设Windows在第一个IDE硬盘的第一个分区上。 注:可以使用–t auto 参数令系统自动监测文件类型! -r 以只读方式挂载。 -w 读写方式挂载。 4.3 umount命令卸载文件系统 (1) umount /mnt/floppy 将设备(如/dev/fd0)从挂载点/mnt/floppy卸载。也可以使用下列方式完成这一工作: umount /dev/fd0为了使该分区对于Linux永久可用,需在/etc/fstab文件中添加如下一行:
/dev/hda1 /mnt/win vfat defaults 0 0 五、 文件相关命令 5.1 文件权限chmod 例子: chmod 777 files -〉rwxrwxrwx chmod 755 files -〉rwx r-x r-x chmod 644 files -〉rw- -r- -r- chmod 000 files -〉- - - - - - - - - chmod u+x g+w o+r file(给file以拥有者可执行、同组可写、其他人可读取的权利) $ chmod –R 777 /tmp/test :更改目录中所有文件和目录的权限(所有权限)。
$ chmod –R 664 /tmp/test :关闭执行权限。 5.2 文件所有权 chown user1 file(把file分配给user1) 5.3 移动文件mv 例子: mv abc def 本文件abc移动更名为def。 六、 iptables命令面面观 外网:eth0 123.45.67.89 内网:eth1 10.0.0.1 LAN上的计算机IP地址都在10.0.0.2~~10.0.0.254之间。 (1)一个rc.local的例子,在rc.local中加入如下命令 echo 1 > /proc/sys/net/ipv4/ip_forward #启动IP转发 echo 1 > /proc/sys/net/ipv4/ip_dynaddr #启动动态IP地址 #Policies(Default)设置默认策略为拒绝 iptables -P INPUT DROP iptables -P OUTPUT DROP iptables -P FORWARD DROP #User-defined chain for ACCEPTed TCP packets用户自定义链,链名为“okay”
iptables -N okay iptables -A okay –p TCP - - syn –j ACCEPT iptables -A okay –p TCP -m state - -state ESTABLISHED,RELATED –j ACCEPT
iptables -A okay –p TCP –j DROP #INPUT chain rules # Rules for incoming packets from LAN iptables -A INPUT -p ALL -i eth1 -s 10.0.0.0/8 -j ACCEPT #LAN中的地址可接受
iptables -A INPUT -p ALL -i lo -s 127.0.0.1 -j ACCEPT #允许接受本身的数据包
iptables -A INPUT -p ALL -i lo -s 10.0.0.1 -j ACCEPT #允许接受本身的数据包
iptables -A INPUT -p ALL -i lo -s 123.45.67.89 -j ACCEPT #允许接受本身的数据包
iptables -A INPUT -p ALL -i eth1 –d 10.0.0.255 -j ACCEPT #允许接受LAN内的广播包
# Rules for incoming packets from the Internet # packets for established connections iptables -A INPUT -p ALL -d 123.45.67.89 -m state –state ESTABLISHED,RELATED –j ACCEPT
# TCP rules 由okay链处理 iptables -A INPUT -p TCP -i eth0 -s 0/0 --destination-port 21 –j okay
iptables -A INPUT -p TCP -i eth0 -s 0/0 --destination-port 22 –j okay
iptables -A INPUT -p TCP -i eth0 -s 0/0 --destination-port 80 –j okay iptables -A INPUT -p TCP -i eth0 -s 0/0 --destination-port 113 –j okay
# UDP rules 定义开放的UDP端口 iptables -A INPUT -p UDP -i eth0 -s 0/0 --destination-port 53 –j ACCEPT
iptables -A INPUT -p UDP -i eth0 -s 0/0 --destination-port 2074 –j ACCEPT
iptables -A INPUT -p UDP -i eth0 -s 0/0 --destination-port 4000 –j ACCEPT
# ICMP rules iptables -A INPUT -p ICMP -i eth0 -s 0/0 --destination-port 8 –j ACCEPT
iptables -A INPUT -p ICMP -i eth0 -s 0/0 --destination-port 11 –j ACCEPT
# FORWARD chain rules #Accept the packets we want to forward iptables -A FORWARD -i eth1 -j ACCEPT iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
# OUTPUT chain rules # Only output packets with local addresses (no spoofing)
iptables -A OUTPUT -p ALL -s 127.0.0.1 -j ACCEPT iptables -A OUTPUT -p ALL -s 10.0.0.1 -j ACCEPT iptables -A OUTPUT -p ALL -s 123.45.67.89 -j ACCEPT # POSTROUTING chain rules 网关的IP伪装