当前位置: 移动技术网 > 科技>操作系统>Linux > Linux常用操作命令

Linux常用操作命令

2019年01月21日  | 移动技术网科技  | 我要评论

操作系统:red hat enterprise linux server release 7.6 

 


001、日常维护常用查询命令

#top    显示系统进程

#clear  清理屏幕信息

#cat /etc/redhat-release  查看系统版本信息

#cat /proc/version  查看系统内核版本等信息

#uname -a  查看系统内核等信息

#cat /proc/cpuinfo  查看cpu信息

#cat /proc/meminfo  查看内存信息

#cat /proc/swaps  查看交换空间信息

#getconf long_bit   查看cpu是32位还是64位

#dmidecode|more 如何查看bios信息等。

#grep memtotal /proc/meminfo    查看物理内存信息

#grep swaptotal /proc/meminfo   查看交换空间的大小

 

#cat /etc/passwd 查看账户信息

#cat /etc/shadow 查看密码信息

#cat /etc/group  查看都有哪些组,每个组包含哪些用户,或#vigr

#groups username  查看用户属于哪一个组

 

#ps -aux 显示终端上的所有进程,包括其他用户的进程。

#ps -x 显示没有控制终端的进程。

#ps -e 显示所有进程。

#ps -r 只显示正在运行的进程。

 

free命令可以查看内存使用率

top -n1命令中有cpu使用率信息

 

002、修改启动界面为文本模式(命令模式)

3为文本模式,5为图形模式。

#vi /etc/inittab

修改id:5:initdefault:为id:3:initdefault:

 

003、修改远程管理时命令行下中文乱码问题

red hat enterprise linux默认的语言编码是“zh_cn.utf-8”,在x-window桌面环境下能正常显示汉字,但如果通过ssh或telnet等方式进行远程管理时,就会发现所有的汉字变成乱码了。

#vi /etc/sysconfig/i18n

将lang="zh_cn.utf-8"改为lang="zh_cn.gb18030",重新登录后即可正常显示汉字。

 

系统默认英文语言:

编辑/etc/sysconfig/i18n

lang="en_us.utf-8"

supported="en_us.utf-8:en_us:en"

sysfont="latarcyrheb-sun16"

 

系统默认中文语言:

lang="zh_cn.utf-8"

supported="zh_cn.utf-8:zh_cn:zh"

sysfont="latarcyrheb-sun16"

 

系统默认英文语言,同时支持中文语言:

lang="en_us"

language="en_us"

supported="zh_cn.gb18030:zh_cn:zh:en_us.utf-8:en_us:en"

sysfont="lat0-sun16"

sysfontacm="8859-15"

 

004、配置网卡相关信息

#cd /etc/sysconfig/network-scripts

#ls

#vi ifcfg-eth0

device=eth0

bootproto=static

broadcast=192.168.0.255

hwaddr=00:17:31:7f:6b:a7

ipaddr=192.168.0.68

netmask=255.255.255.0

network=192.168.0.0

onboot=yes

type=ethernet

:wq退出。

#service network restart  重新启动网络

 

005、更改主机名

#vi /etc/sysconfig/network

#sudo vi /etc/hostname

将hostname改名,然后重启即可。

 

006、考虑到安全问题,只允许普通账户直接远程登录,不允许root直接远程登录

配置sshd

启动sshd服务:/etc/init.d/sshd start

编辑/etc/ssh/sshd_config文件,找到语句permitrootlogin yes,将yes修改为no。

重新启动sshd服务:/etc/init.d/sshd restart

 

007、关闭不需要的系统服务

为了安装的系统执行效能更佳,建议关闭一些没有必要的系统服务,例如cups、sendmail、isdn、smartd、iptables、pcmcia...等。

#chkconfig cups off

#chkconfig sendmail off

#chkconfig isdn off

#chkconfig smartd off

#chkconfig iptables off

#chkconfig pcmcia off

 

008、修改dns

标准格式:nameserver  dns地址

#vi /etc/resolv.conf

nameserver 192.168.0.36

nameserver 202.106.0.20

:wq  #保存退出。

#service network restart  #重新启动网络

 

009、查看系统磁盘的分区情况以及文件系统被挂载的位置

#df –h

filesystem            size  used avail use% mounted on

/dev/hda3              36g  4.1g   30g  12% /

/dev/hda1             190m  8.4m  172m   5% /boot

none                  248m     0  248m   0% /dev/shm

#df -h /home 查看home所在分区的磁盘使用情况

 

010、查看目录及该目录下文件的大小,或查看单个文件的大小

#du -h 目录或文件

[root@eric68 /]# du -h /tmp

12k     /tmp/.font-unix

8.0k    /tmp/.ice-unix

12k     /tmp/ssh-wdiorjg370

36k     /tmp/test

164k    /tmp

[root@eric68 tmp]# du -h 1.txt

8.0k    1.txt

 

du -sh 目录或文件  //查看目录或文件的总大小

[root@eric68 /]# du -sh /tmp

164k    /tmp

[root@eric68 tmp]# du -sh 1.txt

8.0k    1.txt

 

011、查看目录权限:ls –ld 目录名

[root@db65 tmp]# ls -ld perl

drwxr-xr-x 2 root root 4096 mar 26 12:39 perl

查看目录下的文件的权限:ls –lr 目录名

[root@db65 tmp]# ls -lr perl

total 24

-rwxr-xr-x 1 root root 24 mar 26 12:39 test2.pl

-rw-r--r-- 1 root root 65 mar 26 12:38 test1.pl

-rw-r--r-- 1 root root 44 mar 26 12:38 helloworld.pl

 

改变目录的权限:chmod 相应权限 目录名/

改变目录及其子目录和文件权限:chmod –r 相应权限 目录名/

 

012、获取当前所有登陆者信息

[root@db80 /]# users

root

[root@db80 /]# who

root     pts/1        mar 20 11:42 (192.168.0.66)

[root@db80 /]# w

 15:34:43 up  3:57,  1 user,  load average: 0.00, 0.00, 0.00

user     tty      from              login@   idle   jcpu   pcpu what

root     pts/1    192.168.0.66     11:42    0.00s  0.14s  0.00s w

 

013、查看系统里有多少个账户

# wc /etc/passwd -l

36 /etc/passwd

 

014.启动和停止apache服务

#/etc/init.d/httpd start    //启动httpd服务

#/etc/init.d/httpd stop    //关闭httpd服务

#/etc/init.d/httpd restart  //重启httpd服务

自动启动web服务:

如果需要让httpd服务随系统启动而自动加载,可以执行"ntsysv"命令启动服务配置程序,找到"httpd"服务,在其前面加上星号(*),然后选择"确定"即可。

 

apache的主配置文件是httpd.conf默认路径:/etc/httpd/conf/httpd.conf

 

015、启动和停止tomcat服务

# /etc/rc.d/init.d/tomcat5 start   启动tomcat服务

# /etc/rc.d/init.d/tomcat5 stop   停止tomcat服务

# /etc/rc.d/init.d/tomcat5 restart  重启omcat服务

 

自动启动tomcat服务:

如果需要让tomcat服务随系统启动而自动加载,可以执行"ntsysv"命令启动服务配置程序,找到"tomcat5"服务,在其前面加上星号(*),然后选择"确定"即可。

 

016、iptables防火墙

关闭防火墙:执行“ntsysv”命令启动服务配置程序,取消“iptables”服务前的“*”号。

若开启了防火墙,如何开放tcp协议的80端口:iptables -i input -p tcp --dport 80 -j accept

 

关掉selinux:

#vi /etc/selinux/config

# this file controls the state of selinux on the system.

# selinux= can take one of these three values:

#       enforcing - selinux security policy is enforced.

#       permissive - selinux prints warnings instead of enforcing.

#       disabled - selinux is fully disabled.

selinux=disabled  #这样就把selinux服务器关掉了,请重新启动系统;

# selinuxtype= type of policy in use. possible values are:

#       targeted - only targeted network daemons are protected.

#       strict - full selinux protection.

selinuxtype=targeted

 

017、禁止、响应ping

# echo 1 > /proc/sys/net/ipv4/icmp_echo_ignore_all 禁止ping,忽略icmp包。

# echo 0 > /proc/sys/net/ipv4/icmp_echo_ignore_all 响应ping

以上两个设置在重启后失效,若添加到/etc/rc.local里,重启后仍然有效。

 

018、history

查看历史记录命令:#history

注释:查看以前使用过的命令,默认最大历史记录条数为1000(red hat 中),可以在 "/etc/profile"里面修改"histsize" 参数来修改默认值

 

清除历史记录命令:#history –c

立即更新history文件命令:#history -w

history记录原理:当一个用户用bash登录系统,他所输入的命令会放到一个内存catch中,在他登出时会存入~/.bash_history中,用histroy  -c  命令可以清空这一命令缓存,也就是说,这一用户不想让人知道他做了些什么的话,只要在退出前执行history  -c就行了。

 

查看某个用户的history记录:

#history –w

#history或#more .bash_history

 

如何自动备份某个用户的history记录?

1.建立ericyi用户的history每日备份脚本

#cd tmp

#mkdir ericyi_history_backup

#chown root ericyi_history_backup/

#vi ericyihistorybackupdaily.sh

-----------------------------------------------------------------

#!/bin/bash                                                                                                                                                                                                                                                          

day=`date +%y-%m-%d`                                                                                                               

basedir="/tmp/ericyi_history_backup "                                                                                                 

cd /home/ericyi                                                                                                                    

history -w                                                                                                                         

cp .bash_history "$basedir"/history."$day" > /dev/null                                                                                  

-----------------------------------------------------------------

 

如何记录用户登陆后的命令操作和系统输出?

在/etc/profile中添加如下内容:

--------------------------------------------------------------------------------

# log user input except test

if [$user != "test" ]; then

        sshclientip=`echo $ssh_client | awk '{ print $1; }'`

        userlogfilename=/tmp/${user}_`date +"%y%m%d%h%m"`_from_${sshclientip}.log

        echo "ssh client info: "${ssh_client} >${userlogfilename}

        exec /usr/bin/script -a -f -q  ${userlogfilename}

fi

--------------------------------------------------------------------------------

 

019、linux下定期备份数据

# chmod 700 backupdaily.sh   //修改权限,root执行

# vi /etc/crontab   //任务计划

#加入下面内容

10 1 * * * root /etc/root/backupdaily.sh

注释:每天1点10分运行脚本

:wq保存退出。

 


020、linux下定期同步时间

#ntpdate 207.46.232.182

做个脚本,放在crontab中定期执行即可。

 

本文来自csdn博客,转载请标明出处:http://blog.csdn.net/cocis/archive/2009/04/13/4068367.aspx

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网