Linux basic commands (addition: command line prompt character plus color)

1. Command line prompt characters
[root@localhost ~]#
[the user currently logged in to the system@hostname current directory]#
# means login as an administrator
$ means login as an ordinary user

2. Switch user
su user name# the directory remains unchanged after the switch
su-user name# the directory after the switch becomes the user’s home directory

3. View the full name
hostname of the current host

4. Temporarily set the
hostname hostname hostname

5. Permanently set the host name The
first type: hostnamectl set-hostname the host name
The second type: vi /etc/hostname (you need to restart the system after setting to take effect)
Press the i key to delete the old host name, and then enter the new host name ( Only the first line is valid)
Press the ESC key, then press the SHIFT + ":" key combination, enter wq, and then press the Enter key to exit

6. View the current system version information
cat /etc/redhat-release
or
cat /etc/*release

7. View the current kernel version
uname -r

8. Temporarily modify the network card IP
ifconfig ens33 192.168.80.3/24
or
ifconfig ens33 192.168.80.3 netmask 255.255.255.0

9. Permanently modify the network card IP

vi /etc/sysconfig/network-scripts/ifcfg-ens33
按i进行修改文件
TYPE=Ethernet #设置网卡类型,“Ethernet”表示以太网
DEVICE=ens33 #设置网卡的名称
ONBOOT=yes #设置网卡是否在 Linux 操作系统启动时激活
BOOTPROTO=static  #设置网卡的配置方式,“static”表示使用静态IP地址,“dhcp”时表示动态获取地址
IPADDR=192.168.80.20 #设置网卡的 IP 地址
NETMASK=255.255.255.0 #设置网卡的子网掩码
GATEWAY=192.168.80.2 #设置网卡的默认网关地址
DNS1=192.168.80.2 #设置DNS服务器的 IP 地址
按ESC键,再按SHIFT + “:” 组合键,输入 wq ,再按回车键退出

Insert picture description here

10. Restart the network card

systemctl restart network #重启所有的网卡(在实际的生产环境中不建议使用,可能造成业务中断)
ifdown ens33 ; ifup ens33 #重启ens33网卡
ifdown ens33 #关闭某个网卡
ifup ens33 #启动某个网卡
ifconfig ens33 down #临时禁用某个网卡
ifconfig ens33 up #重新激活某个网卡(不会更新IP地址)

11. Check the network card address
ifconfig, ip a
Insert picture description here

12. Add static route entry
Temporarily add route (restart network service invalid)
Method 1:

route add -net 192.168.3.0/24 gw 192.168.8.2 [dev ens33]

-net: Specify the address of the target network segment
gw: Specify the IP address of the next-hop router
dev: Specify the output interface for the route
Method 2:

ip route add 192.168.15.0/24 via 192.168.80.2 [dev ens33]

13. View routing table entries

route -n

14. Delete static route entries

route del -net 192.168.3.0/24

15. Permanently add a route (restart the network service to take effect)

方法一:
vi /etc/sysconfig/static-routes
any net any gw 192.168.80.2
any net 192.168.3.0/24 gw 192.168.80.2
any net 10.0.0.0 netmask 255.0.0.0 gw 192.168.80.2
any host 192.168.100.100 gw 192.168.80.2
systemctl restart network
方法二:
vim /etc/sysconfig/network-scripts/route-ens33
default via 192.168.80.2 dev ens33  #默认路由,另一种格式 0.0.0.0/0 192.168.14.254 dev ens33
10.211.6.0/24 via 192.168.80.2 dev ens33
192.168.100.200  via 192.168.14.254 dev ens33
systemctl restart network

16. Test the network connection

ping, traceroute (static tracking), mtr (dynamic tracking)

17. Restart the system
reboot, init 6, shutdown -r now

18. Shut down
poweroff, shutdown -h now, init 0

19. Supplement
Add color to the command line prompt characters
(recommendation: write the next command into vim /etc/profile and vim ~/.bashrc under the root user, automatically change the color when booting, switch other users or change the shell environment. Useful) a. The
root user is still useful to switch the shell environment

vim ~/.bashrc
PS1="\[\e[1;34m\][\u@\h \W]\\$\[\e[0m\]"
source ~/.bashrc #输入该命或者重启

b. Switching to other users is still useful

vim /etc/profile
PS1="\[\e[1;34m\][\u@\h \W]\\$\[\e[0m\]"
source /etc/profile #输入该命或者重启

/etc/resolv.conf
is the DNS client configuration file, which is used to set the IP address and DNS domain name of the DNS server. Up to 3 different DNS server addresses can be specified, and the first DNS server will be used first. The changes made to this file will take effect immediately.
nameserver 192.168.80.2 #Define the IP address of the DNS server

Note: This configuration file has the same function as the DNS1 parameter of the network card configuration file, whichever configuration is last modified or restarted.

20. Turn off the virtual machine firewall and security enhancements (to ensure that xshell can be connected)
systemctl stop firewall
setenforce 0

Guess you like

Origin blog.csdn.net/weixin_53567573/article/details/113085024