Linux 工作常用命令(四)解释!(图文并茂)
-
export 设置变量 export test=/home/lgb
export PATH=$PATH:/usr/local/mysql/bin
export -p
-
unset 清除环境变量 unset test
场景:比如你需要安全oracle这个数据库或者说安装JAVA相关的软件时,你需要导入环境变量路径,那么需要这个命令
[root@localhost tmp]# export test=/home
[root@localhost tmp]#
[root@localhost tmp]#
[root@localhost tmp]#
[root@localhost tmp]# echo $test
/home
[root@localhost tmp]# unset test
[root@localhost tmp]# echo $test
[root@localhost tmp]#
-
chkconfig 管理服务软件自启 chkconfig --level 35 mysqld on
chkconfig --list
场景:有时候,我们需要开机自动重启程序。
[root@localhost tmp]# chkconfig --level 35 NetworkManager
only one runlevel may be specified for a chkconfig query
[root@localhost tmp]#
[root@localhost tmp]# chkconfig --level 35 sshd
only one runlevel may be specified for a chkconfig query
[root@localhost tmp]#
[root@localhost tmp]#
[root@localhost tmp]#
[root@localhost tmp]# systemctl enable sshd
[root@localhost tmp]#
[root@localhost tmp]# chkconfig --list
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.
If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.
netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
[root@localhost tmp]#
[root@localhost tmp]# chkconfig --level 35 network
only one runlevel may be specified for a chkconfig query
[root@localhost tmp]# chkconfig --level 135 network
only one runlevel may be specified for a chkconfig query
[root@localhost tmp]# chkconfig --level 1 network
[root@localhost tmp]#
[root@localhost tmp]#
[root@localhost tmp]# chkconfig --level 3 network
[root@localhost tmp]#
[root@localhost tmp]#
[root@localhost tmp]# chkconfig --level 35 network
only one runlevel may be specified for a chkconfig query
[root@localhost tmp]# chkconfig --level 3 5 network
chkconfig version 1.3.61 - Copyright © 1997-2000 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU Public License.
usage: chkconfig [–list] [–type ] [name]
chkconfig --add
chkconfig --del
chkconfig --override
chkconfig [–level ] [–type ] <on|off|reset|resetpriorities>
[root@localhost tmp]# chkconfig --level 5 network
[root@localhost tmp]#
[root@localhost tmp]#
-
runlevel 显示系统当前运行级别 runlevel
[root@localhost tmp]# runlevel
N 3
[root@localhost tmp]#
-
getenforce 查看selinux的运行状态 getenforce
它这个属于系统级别的安全
场景:有时候我们一些服务无法使用,比如我们的FTP,所以作为初学者,我们需要去关闭selinux。
[root@localhost tmp]# getenforce
Enforcing
[root@localhost tmp]#
从上面可知selinux是开启的,那么需要去关闭掉,否则会对初学者易用性太差。
6. setenforce 关闭或者开启selinux
开启后,导致文件权限修改不了等问题,下面就是关闭设置setlinux的方法
[root@localhost tmp]# getenforce
Enforcing
[root@localhost tmp]# setenforce 0
[root@localhost tmp]#
[root@localhost tmp]#
[root@localhost tmp]# getenforce
Permissive
[root@localhost tmp]#
7. init 修改系统运行级别 重启系统:Init 6、关闭系统:init 0
我建议用这个命令进行开关机,因为它代表了系统运行级别的内容。
-
service 管理服务 service httpd start
[root@localhost tmp]# service sshd start
Redirecting to /bin/systemctl start sshd.service
[root@localhost tmp]#
[root@localhost tmp]#
[root@localhost tmp]#
-
netstat 查看服务端口 netstat -anp
netstat -apn |grep 8080
场景:我们经常安装一些网络程序,有时候会查看看端口号及状态,特别有用。
-
mount 挂载设备 Cd /mnt
mkdir -p /mnt/cdrom
mount -t auto /dev/cdrom /mnt/cdrom
场景:挂载U盘你的LINUX里面,还有需要挂载光驱到系统里面。
我们可以这样去想象:镜像ISO,就好比光盘;设备状态,就好比连接光驱或者弹出光驱。
[root@localhost /]# mkdir -p /mnt/cdrom
[root@localhost /]#
[root@localhost /]#
[root@localhost /]#
[root@localhost /]# mount -t auto /dev/cdrom /mnt/cdrom
mount: /dev/sr0 is write-protected, mounting read-only
[root@localhost /]# cd /mnt/cdrom/
[root@localhost cdrom]# ls
CentOS_BuildTag EFI EULA GPL images isolinux LiveOS Packages repodata RPM-GPG-KEY-CentOS-7 RPM-GPG-KEY-CentOS-Testing-7 TRANS.TBL
[root@localhost cdrom]#
[root@localhost cdrom]# cd Packages/
[root@localhost Packages]# rpm -ivh ne
neon-0.30.0-3.el7.x86_64.rpm nepomuk-widgets-4.10.5-3.el7.x86_64.rpm netpbm-progs-10.61.02-9.el7.x86_64.rpm net-snmp-devel-5.7.2-24.el7.x86_64.rpm nettle-devel-2.7.1-4.el7.x86_64.rpm
nepomuk-core-4.10.5-5.el7.x86_64.rpm netcf-libs-0.2.8-1.el7.x86_64.rpm netsniff-ng-0.5.8-6.el7.x86_64.rpm net-snmp-libs-5.7.2-24.el7.x86_64.rpm net-tools-2.0-0.17.20131004git.el7.x86_64.rpm
nepomuk-core-devel-4.10.5-5.el7.x86_64.rpm netlabel_tools-0.20-5.el7.x86_64.rpm net-snmp-5.7.2-24.el7.x86_64.rpm net-snmp-utils-5.7.2-24.el7.x86_64.rpm newt-0.52.15-4.el7.x86_64.rpm
nepomuk-core-libs-4.10.5-5.el7.x86_64.rpm netpbm-10.61.02-9.el7.x86_64.rpm net-snmp-agent-libs-5.7.2-24.el7.x86_64.rpm nettle-2.7.1-4.el7.x86_64.rpm newt-python-0.52.15-4.el7.x86_64.rpm
[root@localhost Packages]# rpm -ivh net-tools-2.0-0.17.20131004git.el7.x86_64.rpm
Preparing… ################################# [100%]
Updating / installing…
1:net-tools-2.0-0.17.20131004git.el################################# [100%]
[root@localhost Packages]#
[root@localhost Packages]# netstat -anp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 1410/sshd
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN 2517/master
tcp 0 48 192.168.92.128:22 192.168.92.1:1105 ESTABLISHED 2602/sshd: root@pts
tcp6 0 0 :::22 ::😗 LISTEN 1410/sshd
tcp6 0 0 ::1:25 ::😗 LISTEN 2517/master
udp 0 0 0.0.0.0:68 0.0.0.0:* 1183/dhclient
udp 0 0 0.0.0.0:34217 0.0.0.0:* 1183/dhclient
udp6 0 0 :::65440 ::😗 1183/dhclient
raw6 0 0 :::58 ::😗 7 859/NetworkManager
Active UNIX domain sockets (servers and established)
-
umount 卸载挂载设备 Umount /mnt/cdrom
[root@localhost mnt]# ls
cdrom
[root@localhost mnt]# cd …
[root@localhost /]# umount /dev/cdrom
[root@localhost /]# cd /mnt/cdrom/
[root@localhost cdrom]# ls
[root@localhost cdrom]#
-
route 查看路由表 Route -n
[root@localhost cdrom]# route
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.92.0 0.0.0.0 255.255.255.0 U 100 0 0 eno16777736
[root@localhost cdrom]#
13. ifdown 关闭指定网卡 Ifdown lo
14. ifup 启动指定网卡 Ifup lo
15. Ifconfig 查看网卡参数 Ifconfig
[root@localhost cdrom]# ip addre
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:fe:83:2f brd ff:ff:ff:ff:ff:ff
inet 192.168.92.128/24 brd 192.168.92.255 scope global dynamic eno16777736
valid_lft 1375sec preferred_lft 1375sec
inet6 fe80::20c:29ff:fefe:832f/64 scope link
valid_lft forever preferred_lft forever
[root@localhost cdrom]# ifdown lo
Could not load file ‘/etc/sysconfig/network-scripts/ifcfg-lo’
Could not load file ‘/etc/sysconfig/network-scripts/ifcfg-lo’
Could not load file ‘/etc/sysconfig/network-scripts/ifcfg-lo’
Could not load file ‘/etc/sysconfig/network-scripts/ifcfg-lo’
[root@localhost cdrom]# ca /etc/sysconfig/network-scripts/
-bash: ca: command not found
[root@localhost cdrom]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
ifcfg-default ifcfg-static ifdown-bnep ifdown-ippp ifdown-post ifdown-sit ifdown-tunnel ifup-bnep ifup-ippp ifup-plip ifup-ppp ifup-Team ifup-wireless network-functions-ipv6
ifcfg-eno16777736 ifcfg-test1 ifdown-eth ifdown-ipv6 ifdown-ppp ifdown-Team ifup ifup-eth ifup-ipv6 ifup-plusb ifup-routes ifup-TeamPort init.ipv6-global
ifcfg-lo ifdown ifdown-ib ifdown-isdn ifdown-routes ifdown-TeamPort ifup-aliases ifup-ib ifup-isdn ifup-post ifup-sit ifup-tunnel network-functions
[root@localhost network-scripts]#
[root@localhost network-scripts]#
[root@localhost network-scripts]# cat ifcfg-lo
DEVICE=lo
IPADDR=127.0.0.1
NETMASK=255.0.0.0
NETWORK=127.0.0.0
If you’re having problems with gated making 127.0.0.0/8 a martian,
you can change this to something else (255.255.255.255, for example)
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback
[root@localhost network-scripts]# systemctl stop NetworkManager
[root@localhost network-scripts]#
[root@localhost network-scripts]#
[root@localhost network-scripts]# ifdown lo
[root@localhost network-scripts]#
[root@localhost network-scripts]#
[root@localhost network-scripts]#
[root@localhost network-scripts]#
[root@localhost network-scripts]# cat ifcfg-lo
DEVICE=lo
IPADDR=127.0.0.1
NETMASK=255.0.0.0
NETWORK=127.0.0.0
If you’re having problems with gated making 127.0.0.0/8 a martian,
you can change this to something else (255.255.255.255, for example)
BROADCAST=127.255.255.255
ONBOOT=yes
NAME=loopback
[root@localhost network-scripts]# systemctl stop NetworkManager
[root@localhost network-scripts]#
[root@localhost network-scripts]#
[root@localhost network-scripts]# ifdown lo
[root@localhost network-scripts]#
[root@localhost network-scripts]#
[root@localhost network-scripts]# ip add
1: lo: mtu 65536 qdisc noqueue state DOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:fe:83:2f brd ff:ff:ff:ff:ff:ff
inet 192.168.92.128/24 brd 192.168.92.255 scope global dynamic eno16777736
valid_lft 1243sec preferred_lft 1243sec
inet6 fe80::20c:29ff:fefe:832f/64 scope link
valid_lft forever preferred_lft forever
[root@localhost network-scripts]# ifup lo
[root@localhost network-scripts]# ip add
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:fe:83:2f brd ff:ff:ff:ff:ff:ff
inet 192.168.92.128/24 brd 192.168.92.255 scope global dynamic eno16777736
valid_lft 1223sec preferred_lft 1223sec
inet6 fe80::20c:29ff:fefe:832f/64 scope link
-
ping 测试网络连通性 ping 192.168.3.254
-
telnet 远程登录命令,可以测试另外一台服务器的某个端口是否打开 telnet 192.168.3.27 22