Linux

Introduction


This article mainly records the problems & solutions I’ve met when operating Linux systems


Common Commands


1. For a specific instruction please refer to here

CommandDescription
File Commands: 
lsDirectory listing
ls -alFormatted listing with hidden files
ls -ltSort the Formatted listing by time modification
cdChange to home directory
pwdShow current working directory
mkdir dirCreate a directory dir
cat >filePlaces the standard input into the file
more fileOutput the contents of the file
tail -f fileOutput the contents of file as it grows, starting with the last 10 lines
touch fileCreate or update file
rm -r dirDelete the directory recursively
rm -f fileForce to remove the file
rm -rf dirForce to remove the directory dir
cp file1 file2Copy the contents of file1 to file2
cp -r dir1 dir2Copy dir1 to dir2; create dir2 if not present
mv file1 file2Rename or move file1 to file2, if file2 is an existing directory
Process management: 
psTo display the currently working processes
topDisplay all running process
kill pidKill the process with given pid
killall procKill all the process named proc
bgList stopped or background jobs, resume a stopped job in the background
fg nBrings job n to the foreground
Searching: 
grep pattern fileSearch for pattern in file
grep -r pattern dirSearch recursively for pattern in dir
command | grep patternSearch pattern in the output of another command
locate fileFind all instances of file
find . -name filenameSearches in the directory (represented by “.”) and below it, for files and directories with names starting with filename
pgrep patternSearches for all the named processes, that matches with the pattern and, by default, returns their ID
System Info: 
wDisplay who is on line
whoamiWho you are logged in as
finger userDisplay information about user
uname -aShow kernel information
cat /proc/cpuinfoCPU information
cat /proc/meminfoMemory information
freeShow memory and swap usage
whereis appShow possible locations of app
which appShow which applications will be run by default
Compression: 
tar cf file.tar fileCreate tar named file.tar containing file
tar xf file.tarExtract the files from file.tar
tar czf file.tar.gz filesCreate a tar with Gzip compression
tar xzf file.tar.gzExtract a tar using Gzip
gzip fileCompresses file and renames it to file.gz
gzip -d file.gzDecompresses file.gz back to file
Network: 
pingPing host and output results
whois domainGet whois information for domains
dig domainGet DNS information for domain
wget fileDownload file
wget -c fileContinue a stopped download
Shortcuts: 
ctrl+zStops the current command, resume with fg in the foreground or bg in the background
ctrl+rType to bring up a recent command
Shutdown: 
shutdown -hShut down immediately after services are stopped
shutdown -rRestart after services are stopped


Vim


Vim is a popular and build-in text editor of Linux systems. In most of the cases I use it to modify configuration files of networking or apps

1. Three modes

  • Command mode: Default mode, can be used to move the cursor to view content
  • Insert mode: Press the “i” button to enter, and edit the text
  • Bottom-line mode: Press the “:” button to enter, and to save and exit
3modes


2. Commands in the bottom-line mode

CommandDescription
:wWrite to disk
:w!Force writing to disk when the file is read-only
:qLeave
:q!Force leaving without saving
:wqLeave after writing to disk
:wq!Leave after forcing writing to disk


Network Configuration


In case we have to construct a network includes Linux systems, usually we have to config static IP addresses for them. Here I use “Ubuntu 18.04” as an example (configurations on “CentOS” and “Fedora” are similar)

1. System UI

l2 l3 l4


2. CommandLine

  • Use ifconfig to check the name of targeted network card
l5
  • Use sudo vi /etc/network/interfaces or sudo gedit /etc/network/interfaces to open the configuration file
l6
  • Insert your modification based on the following template, and save the changes
auto ens33
iface ens33 inet static
address 192.168.4.200
netmask 255.255.255.0
gateway 192.168.4.1
l7
  • Use one of the following commands to restart the network or simply restart the system to activate the new configuration:
/etc/init.d/networking restart
sudo service network-manager restart
sudo service networking restart


Change Permission


1. Problem

  • I used to install the SysmonForLinux on my ubuntu18, and the system logs that Sysmon generates are stored in the “/var/log/syslog”. Later I wanted the transmission tool Nxlog to transmit those logs to my ELK mainframe. However, the permission of “syslog” is read-only, Nxlog cannot retrieve any data from it

2. Rationale

  • Insufficient privilege

3. Solution1

  • A common thought to address the problem aforementioned is to change the permission of the targeted file using sudo chmod 777 /var/log/syslog . It does work at first, nevertheless, I find that the permission of “syslog” will turn back to read-only each time I restart the system

4. Solution2

  • Another way to solve the problem is to grant the application a higher permission to access the target. Using sudo gedit /etc/passwd we can not only grant apps but also users. To do this, simply change the corresponding number after the “x:” to “0”
l8
  • In the highlighted part of the pic above, I’ve granted Nxlog the highest permission as the changed number is actually the UID, and the UID of root user is “0”


Remove File Lock


1. Problem

  • The error below often occurs when installing applications using the apt command on Ubuntu:
l9

2. Rationale

  • File locks are used to prevent two or more processes from using the same data. When apt or apt-get commands are running, they create lock files in a few places. If the previous apt command was not terminated properly, the lock files are not deleted and hence they prevent any new instances of apt-get or apt commands

3. Solution

  • Use ps -e | grep apt to find out related processes
  • Use sudo kill PID to terminate processes if returned
  • Use the following commands to safely remove the locks:
sudo rm /var/lib/apt/lists/lock
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock