Tag: linux

  • How to change home directory of a linux user

    How to change home directory of a linux user

    You can modify a user’s home directory using usermod command in Linux.

    The syntax for the usermod command is

    sudo usermod -d PATH_TO_HOME_DIR USER_NAME
    

    Example

    I have a user “sok_user1” with home directory /home/sok_user1

    root@ok:~# cat /etc/passwd | grep sok_user1
    sok_user1:x:1004:1005::/home/sok_user1:/bin/bash
    root@ok:~#
    

    To change the users home directory to /var/www/, we can use the command

    usermod -d /var/www sok_user1
    

    Example

    root@ok:~# usermod -d /var/www sok_user1
    root@ok:~# cat /etc/passwd | grep sok_user1
    sok_user1:x:1004:1005::/var/www:/bin/bash
    root@ok:~# 
    

    Back to Linux User

  • How to find all IP address of a Linux server

    How to find all IP address of a Linux server

    To list all configured IP addresses on a server, you can use the command

    hostname -I
    

    To find the main IP, use

    hostname -I | awk '{print $1}'
    

    You can also use the command “ip addr” to list all IP address.

    ip addr | grep "inet "
    

    Example

    root@server20 [~]# ip addr | grep "inet "
        inet 127.0.0.1/8 scope host lo
        inet 158.69.53.72/24 brd 158.69.53.255 scope global eth0
        inet 158.69.114.170/32 brd 158.69.114.170 scope global eth0:cp1
        inet 54.39.173.35/32 brd 54.39.173.35 scope global eth0:cp2
    root@server20 [~]# 
    
    
    Another command that lists IP address is ifconfig
    
    
    
    root@server20 [~]# ifconfig
    eth0: flags=4163  mtu 1500
            inet 158.69.53.72  netmask 255.255.255.0  broadcast 158.69.53.255
            inet6 fe80::ec4:7aff:fe69:9342  prefixlen 64  scopeid 0x20
            inet6 2607:5300:60:8248::  prefixlen 64  scopeid 0x0
            ether 0c:c4:7a:69:93:42  txqueuelen 1000  (Ethernet)
            RX packets 3423724119  bytes 670412248418 (624.3 GiB)
            RX errors 0  dropped 0  overruns 18691  frame 0
            TX packets 3843828868  bytes 3897383743022 (3.5 TiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
            device memory 0xfb120000-fb13ffff  
    
    eth0:cp1: flags=4163  mtu 1500
            inet 158.69.114.170  netmask 255.255.255.255  broadcast 158.69.114.170
            ether 0c:c4:7a:69:93:42  txqueuelen 1000  (Ethernet)
            device memory 0xfb120000-fb13ffff  
    
    eth0:cp2: flags=4163  mtu 1500
            inet 54.39.173.35  netmask 255.255.255.255  broadcast 54.39.173.35
            ether 0c:c4:7a:69:93:42  txqueuelen 1000  (Ethernet)
            device memory 0xfb120000-fb13ffff  
    
    lo: flags=73  mtu 65536
            inet 127.0.0.1  netmask 255.0.0.0
            inet6 ::1  prefixlen 128  scopeid 0x10
            loop  txqueuelen 1000  (Local Loopback)
            RX packets 3364258789  bytes 4360941211553 (3.9 TiB)
            RX errors 0  dropped 0  overruns 0  frame 0
            TX packets 3364258789  bytes 4360941211553 (3.9 TiB)
            TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
    
    root@server20 [~]# 
    
  • sleep-monitor system is about to suspend

    sleep-monitor system is about to suspend

    On a remote Linux server running XFCE desktop, the server goes down randomly, on monitoring /var/log/syslog, i found the following message

    Jan 18 05:43:24 sokdesk ModemManager[1300]:   [sleep-monitor] system is about to suspend
    Jan 18 05:43:24 sokdesk systemd[1]: Reached target Sleep.
    Jan 18 05:43:24 sokdesk systemd[1]: Starting Record successful boot for GRUB...
    Jan 18 05:43:24 sokdesk systemd[1]: Starting Suspend...
    Jan 18 05:43:24 sokdesk systemd-sleep[2574]: Suspending system...
    Jan 18 05:43:24 sokdesk kernel: [ 1217.283955] PM: suspend entry (s2idle)
    

    When this happens, the remote desktop and SSH connections to the server get disconnected. To get back to the server, I had to restart the server.

    The error is fixed by running the command

    systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
    
  • bash: /usr/bin/man: No such file or directory

    bash: /usr/bin/man: No such file or directory

    On Ubuntu, I get the following error

    root@vps1:~# man
    bash: /usr/bin/man: No such file or directory
    root@vps1:~#
    

    This is fixed by installing man with apt-get

    apt-get install man
    
  • RAID getting created as /dev/md127

    RAID getting created as /dev/md127

    When I create software RAID with mdadm, it gets created as /dev/md127, instead of /dev/md0 I specified while creating RAID.

    RAID /dev/md127

    To fix this, you need to add RAID definition in /etc/mdadm/mdadm.conf, this can be done with

    mdadm --detail --scan >> /etc/mdadm/mdadm.conf
    

    Now run command

    update-initramfs -u
    

    Reboot the server, after reboot, RAID device will be named properly.

  • How to find CPU of a Linux Server

    To find CPU used on a Linux system, run command

    cat /proc/cpuinfo
    

    Example

    boby@sok-01:~$ cat /proc/cpuinfo | grep -i "model name"
    model name	: AMD Ryzen 3 2200G with Radeon Vega Graphics
    model name	: AMD Ryzen 3 2200G with Radeon Vega Graphics
    model name	: AMD Ryzen 3 2200G with Radeon Vega Graphics
    model name	: AMD Ryzen 3 2200G with Radeon Vega Graphics
    boby@sok-01:~$ 
    
  • Linux Run Command as another user

    To run command as another user on Linux, you can use

    su USER_NAME_HERE -s /bin/bash -c COMMAND_HERE
    

    Or

    sudo -u USER_NAME_HERE COMMAND_HERE
    

    Or

    runuser -l  USER_NAME_HERE -c 'COMMAND_HERE'
    

    See sudo

  • ls

    ls command is used to list files in linux. It is same as dir on windows.

    List all files in long format (l), a is to show hidden files

    ls -la
    

    Show only file names

    ls -b
    

    Show only file names. One file per line

    ls -1
    
  • Server Load

    Here are some useful commands to check server load.

    iostat shows disk IO usage. %iowait shows how much time the process waits for disk operation. This should be 0. If you are on VPS, check for %steal to see if the host server is overloaded.

    iostat 1 10
    

    Checks for processes in ā€˜Dā€™ state

    for x in `seq 1 1 30`; do ps -eo state,pid,cmd | grep "^D"; echo "-"; sleep 2; done
    

    Top processes using CPU resources

    top -bn 1 | head -20
    

    Check Running Process

    ps aux
    top
    atop
    pstree
    

    Check For Bad Scripts

    WPRobot plugin for wordpress take lot of cpu usage, find sites using WPRobot

    find /home -name 'wprobot.php'