您的位置:首页 > 理论基础 > 计算机网络

Linux网络连接模式以及修改静态IP

2017-12-17 15:40 183 查看
网络连接模式

1、桥接模式

centos相当于一台物理机,可以直接连接外网,能够连接同一个局域网下为桥接模式的其他宿主机上的客户机

2、NAT模式

通过宿主机连接外网,可以访问同一个局域网的其他物理主机,但是其他主机不能够访问该宿主机

3、only host

不能连接外网,可以连接同一宿主机的其他客户机

修改静态IP
[root@localhost network-scripts]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-eno16777736  ifdown-ipv6    ifdown-TeamPort  ifup-ippp   ifup-routes       network-functions
ifcfg-lo           ifdown-isdn    ifdown-tunnel    ifup-ipv6   ifup-sit          network-functions-ipv6
ifdown             ifdown-post    ifup             ifup-isdn   ifup-Team
ifdown-bnep        ifdown-ppp     ifup-aliases     ifup-plip   ifup-TeamPort
ifdown-eth         ifdown-routes  ifup-bnep        ifup-plusb  ifup-tunnel
ifdown-ib          ifdown-sit     ifup-eth         ifup-post   ifup-wireless
ifdown-ippp        ifdown-Team    ifup-ib          ifup-ppp    init.ipv6-global
[root@localhost network-scripts]# cp ifcfg-eno16777736 ifcfg-eno16777736.bak
[root@localhost network-scripts]# ls
ifcfg-eno16777736      ifdown-ippp    ifdown-Team      ifup-ib     ifup-ppp       init.ipv6-global
ifcfg-eno16777736.bak  ifdown-ipv6    ifdown-TeamPort  ifup-ippp   ifup-routes    network-functions
ifcfg-lo               ifdown-isdn    ifdown-tunnel    ifup-ipv6   ifup-sit       network-functions-ipv6
ifdown                 ifdown-post    ifup             ifup-isdn   ifup-Team
ifdown-bnep            ifdown-ppp     ifup-aliases     ifup-plip   ifup-TeamPort
ifdown-eth             ifdown-routes  ifup-bnep        ifup-plusb  ifup-tunnel
ifdown-ib              ifdown-sit     ifup-eth         ifup-post   ifup-wireless
[root@localhost network-scripts]# nano ifcfg-eno16777736.bak
TYPE=Ethernet
BOOTPROTO=none
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=no
IPV6_AUTOCONF=no
IPV6_DEFROUTE=no
IPV6_FAILURE_FATAL=no
NAME=eno16777736
UUID=89b859f5-ddf6-4056-a99c-c29fbf5d4ee2
DEVICE=eno16777736
ONBOOT=yes
PEERDNS=yes
PEERROUTES=yes
IPV6_PEERDNS=no
IPV6_PEERROUTES=no
IPADDR=192.168.109.100
PREFIX=24
GATEWAY=192.168.109.2
DNS=192.168.109.2

[root@localhost network-scripts]# cd /etc
[root@localhost etc]# ls |grep resolv.conf
resolv.conf
[root@localhost etc]# nano resolv.conf
[root@localhost etc]#
nameserver 192.168.109.2
重启网络service network  restart

查看所有进程信息
[root@localhost ~]# ps -Af |grep service
cr121      1794      1  0 05:29 ?        00:00:00 /usr/libexec/goa-identity-service
cr121      1985   1494  0 05:29 ?        00:00:00 /usr/bin/gnome-software --gapplication-service
cr121      2029      1  0 05:29 ?        00:00:00 /usr/libexec/dconf-service
root       2332   2179  0 05:39 pts/0    00:00:00 grep --color=auto service
[root@localhost ~]#

top:动态显示进程的信息包括CPU,内存的使用情况

cut剪切显示
[root@localhost aa]# cat a.txt
hello
how are you
[root@localhost aa]# cut -c 1-7 a.txt//显示1-7列
hello
how are
[root@localhost aa]#

[root@localhost aa]# ps -Af |cut -c 45-80 |more//查看所有进程,并剪切显示45-80列


sudo 临时使用root的权利,命令执行完毕失效
给用户添加sudo的权利



查看磁盘分区
[root@localhost dev]# fdisk -l /dev/sda

Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x000bcae1

Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048      616447      307200   83  Linux
/dev/sda2          616448     4810751     2097152   82  Linux swap / Solaris
/dev/sda3         4810752    41943039    18566144   83  Linux
[root@localhost dev]#

查看磁盘使用情况
[root@localhost ~]# df -ah .
Filesystem      Size  Used Avail Use% Mounted on
/dev/sda3        18G   11G  7.5G  58% /
[root@localhost ~]#

输出指定目录的上级目录
[root@localhost ~]# dirname /a/b/c
/a/b
[root@localhost ~]#

取出当前目录的名称
[root@localhost ~]# basename /a/b/c
c
[root@localhost ~]#

修改主机名称
[root@localhost etc]# ls -l |grep host
drwxr-xr-x.  3 root root       17 Sep  6 10:22 ghostscript
-rw-r--r--.  1 root root        9 Jun  7  2013 host.conf
-rw-r--r--.  1 root root       22 Nov 23 20:23 hostname
-rw-r--r--.  1 root root      158 Jun  7  2013 hosts
-rw-r--r--.  1 root root      370 Jun  7  2013 hosts.allow
-rw-r--r--.  1 root root      460 Jun  7  2013 hosts.deny
[root@localhost etc]# nano hostname
[root@localhost etc]# cat hostname
s128
[root@localhost etc]


设置主机名与IP地址的映射
[root@s128 etc]# ls -l|grep hosts
drwxr-xr-x.  3 root root       17 Sep  6 10:22 ghostscript
-rw-r--r--.  1 root root      158 Jun  7  2013 hosts
-rw-r--r--.  1 root root      370 Jun  7  2013 hosts.allow
-rw-r--r--.  1 root root      460 Jun  7  2013 hosts.deny
[root@s128 etc]# nano hosts
[root@s128 etc]# ping s128
PING s128 (192.168.109.128) 56(84) bytes of data.
64 bytes from s128 (192.168.109.128): icmp_seq=1 ttl=64 time=0.028 ms
64 bytes from s128 (192.168.109.128): icmp_seq=2 ttl=64 time=0.030 ms
64 bytes from s128 (192.168.109.128): icmp_seq=3 ttl=64 time=0.030 ms
64 bytes from s128 (192.168.109.128): icmp_seq=4 ttl=64 time=0.034 ms
64 bytes from s128 (192.168.109.128): icmp_seq=5 ttl=64 time=0.032 ms
64 bytes from s128 (192.168.109.128): icmp_seq=6 ttl=64 time=0.034 ms
64 bytes from s128 (192.168.109.128): icmp_seq=7 ttl=64 time=0.031 ms
64 bytes from s128 (192.168.109.128): icmp_seq=8 ttl=64 time=0.029 ms
^C



命令的嵌套
[root@s128 ~]# echo s128>a.txt
[root@s128 ~]# cat a.txt
s128
[root@s128 ~]# ping $(cat a.txt)
PING s128 (192.168.109.128) 56(84) bytes of data.
64 bytes from s128 (192.168.109.128): icmp_seq=1 ttl=64 time=0.044 ms
64 bytes from s128 (192.168.109.128): icmp_seq=2 ttl=64 time=0.089 ms
64 bytes from s128 (192.168.109.128): icmp_seq=3 ttl=64 time=0.052 ms
^C
--- s128 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1999ms
rtt min/avg/max/mdev = 0.044/0.061/0.089/0.021 ms
[root@s128 ~]# ping `cat a.txt`
PING s128 (192.168.109.128) 56(84) bytes of data.
64 bytes from s128 (192.168.109.128): icmp_seq=1 ttl=64 time=0.056 ms
64 bytes from s128 (192.168.109.128): icmp_seq=2 ttl=64 time=0.069 ms
64 bytes from s128 (192.168.109.128): icmp_seq=3 ttl=64 time=0.036 ms
^C
--- s128 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2000ms
rtt min/avg/max/mdev = 0.036/0.053/0.069/0.016 ms
[root@s128 ~]#

添加用户
[root@s128 sbin]# useradd -m centos2
[root@s128 sbin]# cd /home
[root@s128 home]# ls
centos2  cr121  hadoop  test.txt
[root@s128 home]# su centos2
[centos2@s128 home]$

删除用户
[centos2@s128 home]$ su root
Password:
[root@s128 home]# which userdel
/usr/sbin/userdel
[root@s128 home]# man userdel
[root@s128 home]# userdel -rf centos2
userdel: user centos2 is currently used by process 2484
[root@s128 home]# ls
cr121  hadoop  test.txt
[root@s128 home]


查看文件的物理位置(由于存在符号链接的作用,显示的是逻辑位置)
[root@s128 home]# cd /bin
[root@s128 bin]# pwd
/bin
[root@s128 bin]# pwd -P
/usr/bin
[root@s128 bin]#

访问环境变量
[root@s128 ~]# echo $PATH
/root/.bashrc/anaconda3/bin:/usr/local/maven352/bin:/root/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/spark2.2/bin:/usr/local/spark2.2/sbin:/usr/local/jdk1.8/bin:/root/bin
[root@s128 ~]# echo ${PATH}
/root/.bashrc/anaconda3/bin:/usr/local/maven352/bin:/root/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/spark2.2/bin:/usr/local/spark2.2/sbin:/usr/local/jdk1.8/bin:/root/bin
[root@s128 ~]# echo "PATH"

定义环境变量export,只在session中有效

[root@s128 ~]# export name=jj
[root@s128 ~]# name
bash: name: command not found...
[root@s128 ~]# echo $name
jj
[root@s128 ~]#

export三元运算符
[root@s128 ~]# export name=yy
[root@s128 ~]# echo $name
yy
[root@s128 ~]# export mypath=${name:-$PATH}
[root@s128 ~]# echo mypath
mypath
[root@s128 ~]# echo $mypath
yy
[root@s128 ~]# export mypath=${name1:-$PATH}
[root@s128 ~]# echo $mypath
/root/.bashrc/anaconda3/bin:/usr/local/maven352/bin:/root/anaconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/usr/local/spark2.2/bin:/usr/local/spark2.2/sbin:/usr/local/jdk1.8/bin:/root/bin
[root@s128 ~]#


查看最后一次命令执行成功与否 0:成功  1:失败
[root@s128 ~]# ls
aa         anaconda-ks.cfg  Desktop    Downloads  Pictures  Templates  Videos
anaconda3  a.txt            Documents  Music      Public    test.txt
[root@s128 ~]# rm -rf aa
[root@s128 ~]# echo $?
0
[root@s128 ~]#


获取参数的个数
[root@s128 ~]# nano a.sh
[root@s128 ~]# ./a.sh 12 3 4
helloworld
[root@s128 ~]# ll
total 20
drwxr-xr-x. 22 root root 4096 Nov 29 01:48 anaconda3
-rw-------.  1 root root 2741 Nov 23 20:24 anaconda-ks.cfg
-rwxr--r--.  1 root root   52 Dec 19 01:03 a.sh
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Desktop
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Documents
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Downloads
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Music
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Pictures
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Public
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Templates
-rw-r--r--.  1 root root   27 Dec 16 17:48 test.txt
-rw-r--r--.  1 root root    2 Dec 19 01:06 t.txt
drwxr-xr-x.  2 root root    6 Nov 29 00:47 Videos
[root@s128 ~]# cat t.txt
3




获取参数的总个数 $#  参数左移 shift   获取脚本名称:$0
[root@s128 ~]# ./a.sh 1 2 3 4
helloworld
the parameters is: num=4
the script's name is :./a.sh
./a.sh: line 6: echon: command not found
2
2
[root@s128 ~]#



if 
[root@s128 ~]# help if
if: if COMMANDS; then COMMANDS; [ elif COMMANDS; then COMMANDS; ]... [ else COMMANDS; ] fi

for循环
利用for循环输出0-100



for循环遍历
[root@s128 bin]# for x  in a b c ;do echo $x;done
a
b
c
[root@s128 bin]#




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐