· Linux sysad

Commands for a System Administrator Part 2

Commands for a System Administrator, Part 1 Commands for a System Administrator, Part 3

1. cd

The most basic of commands, change directory. Needed in order to move around the Linux filesystem

    [[email protected] ~]# cd /var/log

2. rsync

Like cp/scp but more options, including resuming stopped copies. The below example, I am Recursively, in verbose mode, in human readable mode copying ‘thisfolder’ to ‘tothisplace. The -P is –partial and –progess so Partial files are saved and I get a nice progress bar. These options are the most commonly used in my option.

    [[email protected] ~]# rsync -rvhP thisfolder tothisplace

3. find

find is an incredibly versatile and powerful tool. You can use it to find files and directories with specific criteria, by default it will show you the results, but with the exec flag you can then run commands based on the findings, mostly commonly with rm, although find as a delete flag as well.

    find /var/log/ -type f -iname “*.log” -mtime +30 -atime +30

4. grep

grep is used to print matching lines for a specified pattern using regex. You can grep a file directly, or commands piped to grep.

    grep user /var/log/secure
    last |grep user

5. man

Need to know how to use a command? Then the man pages are what you need, they are the reference pages for commands, man pages can be searched for with the -k flag

    [[email protected] ~]# man -k httpd
    apache_selinux (8) – Security Enhanced Linux Policy for the httpd processes
    CGI::Carp (3pm) – CGI routines for writing to the HTTPD (or other) error log
    httpd (8) – Apache Hypertext Transfer Protocol Server

6. ps

ps provides a snapshot of the current processes along with detailed information like user ID, CPU usage, memory usage, command name etc. It is the go to command to find information of any user or process and there are a large number of flags to run with this, see man ps for a lot more info.

    [[email protected] ~]# ps auxxx
    root 1 0.0 0.0 51048 8468 ? Ss Mar01 0:27 /usr/lib/systemd/systemd –switched-root –system –d
    root 2 0.0 0.0 0 0 ? S Mar01 0:00 [kthreadd]
    root 3 0.0 0.0 0 0 ? S Mar01 0:06 [ksoftirqd/0]

7. whereis, which

which will show you the path of a binary and whereis will search for the binary, source and man pages. Usually whereis shows too much and you should just stick with which. which can also be good in scripts that get distributed as the binaries may be installed in different places.

    [[email protected] ~]# which ps
    [[email protected] ] whereis ps
    ps: /usr/bin/ps /usr/share/man/man1/ps.1.gz

8. service

This command is run a SysV init script, these are stored in /etc/init.d/. Basic usage is stop, start and restart. In CentOS/RHEL7 this command is replaced with systemctl. This is whole different ball game and worth an article in itself.

    [[email protected]:/]$ service nginx restart
    Stopping nginx: [ OK ]
    Starting nginx: [ OK ]
    [[email protected]:/]$ 

9. alias

Aliases are essentially shortcuts. You can define an alias to a long command and run the alias rather than the command. There are some default aliases you’ll get with every system. Personally, I have really long commands to RDP to some servers with lots of arguments, rather than typing the whole thing each time, I run rdp-outlook.

    [[email protected] ~]# alias
    alias egrep=’egrep –color=auto’
    alias fgrep=’fgrep –color=auto’
    alias grep=’grep –color=auto’
    alias l.=’ls -d .* –color=auto’
    alias ll=’ls -l –color=auto’
    alias ls=’ls –color=auto’

10. df

df shows a report of the disk filesystem space usage. df on its own will how all mounted filesytems and their reported size, used space, available space and where its mounted. Add the partition you want to know about along with -h for human readable and -P for POSIX output if the filesystem names are long.

    [[email protected]:/]$ df /var/log/ -hP
    Filesystem Size Used Avail Use% Mounted on
    /dev/mapper/vg–data-lv–log 9.9G 192M 9.2G 3% /var/log 

11. du

du is the Disk usage utility. Most common and basic usage is the example below, I would recommend reading the man page for further information on this.

    [[email protected] test]# du ./* -sh
    2.1M ./file.1
    2.1M ./file.10
    2.1M ./file.100
    2.1M ./file.11

12. rm

This the is remove command, its how you delete files. Run on its own, you’ll be asked for approval before the actual deletion, the -f flag will force delete so you get asked. If you want to delete a directory and all is contents then use -r for recursive. rm -rf ./somedir – BEWARE ‘rm -rf /’ will delete EVERYTHING and you computer will require a re-installation.

    [[email protected] test]# rm file.1
    rm: remove regular file `file.1′? y

13. echo

echo simply displays a line of text to the terminal. Extremely useful in scripts to interact with the user and to display output based on input.

    [[email protected] test]#cat bashtest.sh
    echo “Please enter your name”
    read name
    echo $name

14. passwd

Used mainly to change your own or another users password, passwd has other functions as locking and unlocking an account and expiring the password, see ‘man passwd’ for more infomation.

    [[email protected] test]# passwd
    Changing password for user root.
    New password:

15. wget

The network downloader application, 95% of the time invoking wget on its on its own is all you need, sometimes you want to add the -O flag to save the download to a specific name. Other flags are –user/–ask-password or -q. See ‘man wget’ for more flags

    [[email protected] test]# wget http://wordpress.org/latest -O wordpress.tgz
    –2015-03-08 15:53:52– http://wordpress.org/latest

16. kill

The kill command will kill a process using the signal and PID given by the user. With no flags a SIGQUIT is sent (3) telling the process to finish up and quit nicely. Should the program not quit, ie its crashed then you want to send a SIGKILL (9) to it. Run “kill -l” to see all your supported signals

    [[email protected] ~]# kill 23482
    [[email protected] ~]# kill -923482

Look out for part three soon - update! Its here

  • LinkedIn
  • Tumblr
  • Reddit

Aaron Mehar

Berkshire, UK