iptables正在用scp复制文件时用错误的号码标记数据包

[案例1]从服务器(在客户端)复制文件到客户端时,数据包被标记为3(根)不正确。

[情况2]当从服务器(在服务器上)将文件复制到客户端时,数据包被标记为1003(test1)正确。

服务器IP 192.168.0.16,test1是服务器用户。

客户端IP 192.168.0.10,客户端是客户端用户。

[情况1]

[email protected]:~$ scp -P 22 [email protected]:/home/test1/archlinux-bootstrap-2016.03.01-x86_64.tar.gz /tmp/ 

ps检查,同时复制:

 [email protected]:~$ ps aux | grep scp root 1653 0.1 0.0 32668 4408 ? Ss 19:31 0:00 \_ sshd: test1 [priv] test1 1655 3.0 0.0 36104 6912 ? S 19:31 0:00 \_ sshd: test1@notty test1 1656 1.3 0.0 27516 2648 ? Ss 19:31 0:00 \_ scp -f /home/test1/archlinux-bootstrap-2016.03.01-x86_64.tar.gz 

bmon [class 1:3是root用户] [class 1:1003是test1用户]

  imq0 (outgoing) │ 1.17MiB 818 │ 1.12MiB 781 qdisc 1: (htb) │ 0 0 │ 1.12MiB 781 cls :3 (fw) │ 0 0 │ 0 0 cls :3eb (fw) │ 0 0 │ 0 0 class 1:1 (htb) │ 0 0 │ 1.12MiB 781 99% class 1:2 (htb) │ 0 0 │ 430B 1 0% class 1:3 (htb) │ 0 0 │ 1.12MiB 780 208% class 1:1003 (htb) │ 0 0 │ 0 0 0% class 1:5 (htb) │ 0 0 │ 0 0 0% class 1:6 (htb) │ 0 0 │ 0 0 0% 

[情况2]

 [email protected]:~$ scp -P 22 archlinux-bootstrap-2016.03.01-x86_64.tar.gz [email protected]:~/ 

ps检查,同时复制:

 [email protected]:~$ ps aux | grep scp root 1637 0.0 0.0 32668 4400 ? Ss 19:29 0:00 \_ sshd: test1 [priv] test1 1639 0.0 0.0 32668 3240 ? S 19:30 0:00 \_ sshd: test1@pts/3 test1 1640 0.0 0.0 20540 3296 pts/3 Ss 19:30 0:00 \_ -bash test1 1650 0.0 0.0 27516 2640 pts/3 S+ 19:30 0:00 \_ scp -P 22 archlinux-bootstrap-2016.03.01-x86_64.tar.gz [email protected]:~/ test1 1651 0.0 0.0 30636 6748 pts/3 S+ 19:30 0:00 \_ /usr/bin/ssh -x -oForwardAgent=no -oPermitLocalCommand=no -oClearAllForwardings=yes -p 22 -l 

bmon [class 1:3是root用户] [class 1:1003是test1用户]

  imq0 │ 142.83KiB 103 │ 98.50KiB 68 qdisc 1: (htb) │ 0 0 │ 98.50KiB 68 cls :3eb (fw) │ 0 0 │ 0 0 cls :3 (fw) │ 0 0 │ 0 0 class 1:1 (htb) │ 0 0 │ 98.50KiB 68 1% class 1:2 (htb) │ 0 0 │ 533B 2 0% class 1:3 (htb) │ 0 0 │ 0 0 0% class 1:1003 (htb) │ 0 0 │ 97.97KiB 66 100% class 1:5 (htb) │ 0 0 │ 0 0 0% 

iptables规则:

 # IN iptables -t mangle -A PREROUTING -i eth0 -j IMQ --todev 1 iptables -t mangle -A PREROUTING -j CONNMARK --restore-mark # OUT iptables -t mangle -N IMQ-OUT iptables -t mangle -A POSTROUTING -o eth0 -j IMQ-OUT iptables -t mangle -A IMQ-OUT -o eth0 -m owner --uid-owner root -j MARK --set-mark 3 iptables -t mangle -A IMQ-OUT -o eth0 -m owner --uid-owner root -j RETURN iptables -t mangle -A IMQ-OUT -o eth0 -m owner --uid-owner test1 -j MARK --set-mark 1003 iptables -t mangle -A IMQ-OUT -o eth0 -m owner --uid-owner test1 -j RETURN iptables -t mangle -A POSTROUTING -j CONNMARK --save-mark iptables -t mangle -A POSTROUTING -o eth0 -j IMQ --todev 0 

有人可以解释我为什么在[Case1]服务器认为传出连接是根连接,即使ps显示用户test1?

现在我正在尝试使用cgroup来改变用户的stream量。

与cgroup一样的情况。

 echo '1003' > /cgroup/cpu_mem_blkio/users/test1/net_cls.classid iptables -t mangle -A IMQ-OUT -o eth0 -m cgroup --cgroup 1003 -j MARK --set-mark 1003 

在[Case1]中,在文件传输期间将sshd连接到客户端的套接字不属于scp进程。 相反,它属于一个由root拥有的sshd进程。 所以,制定标记3的规则占上风。


看我的例子。 首先,我从客户端启动一个复制任务:

 [amg1127@amg1127-laptop /tmp]$ scp -4 amg1127-sala:/tmp/virtualbox-machine.vdi . virtualbox-machine.vdi 0% 5488KB 1.4MB/s 55:47 ETA^ 

然后,我在服务器中找出与拷贝有关的进程和套接字。

 [root@amg1127-sala /tmp]# lsof /tmp/virtualbox-machine.vdi COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME scp 12107 1000 3r REG 254,5 4824498176 20439792 /tmp/virtualbox-machine.vdi [root@amg1127-sala /tmp]# netstat -pan | grep ':22' tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1029/sshd tcp 0 195936 192.168.254.10:22 192.168.254.9:59574 ESTABLISHED 12089/sshd: amg1127 tcp6 0 0 :::22 :::* LISTEN 1029/sshd [root@amg1127-sala /tmp]# ps -C sshd -C scp -o uid,euid,pid,ppid,cmd UID EUID PID PPID CMD 0 0 1029 1 /usr/bin/sshd -D 0 0 12089 1029 sshd: amg1127 [priv] 1000 1000 12106 12089 sshd: amg1127@notty 1000 1000 12107 12106 scp -f /tmp/virtualbox-machine.vdi 

注意:

  1. lsof显示读取/tmp/virtualbox-machine.vdi文件的进程是一个scp进程,其PID为12107 ,所有者为1000
  2. netstat显示将文件数据发送到远程客户端的套接字是一个sshd进程,其PID为12089 ,所有者为0

两个进程都可能通过匿名pipe道交换数据。